-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.py
49 lines (34 loc) · 1.03 KB
/
twitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import oauth2
import json
ckey =
csecret =
atoken =
asecret =
tweets = []
# file name that you want to open is the second argument
save_file = open('Twitter.json', 'a')
class listener (StreamListener):
def __init__(self):
self.num_tweets = 0
self.save_file = tweets
def on_data(self, data):
print(data)
# self.save_file.append(json.loads(data))
# save_file.write(str(data))
tweets.append(json.loads(data))
self.num_tweets += 1
if self.num_tweets < 100:
return True
else:
return False
def on_error(self, status):
print(status)
save_file.write(str(tweets))
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitts = twitterStream.filter(track=["a",'the',"for","Python", "to", "in","cloud"])