-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStreamer.py
45 lines (33 loc) · 1.36 KB
/
MyStreamer.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
import json
from twython import TwythonStreamer
from collections import Counter
# appending data to a global variable is bad
# but for simplicity
tweets = []
class MyStreamer(TwythonStreamer):
"""out own subclass of TwythonStreamer that specifies
how to interact with the stream"""
def on_success(self, data):
"""What do we do when twitter send us data?
here data will bea python dict representing a tweet"""
# Only want to collect English-language tweets
if data['lang'] == 'en':
tweets.append(data)
print("received # {}".format(len(tweets)))
#stop when we've collected enough
if len(tweets) >= 1000:
self.disconnect()
def on_error(self, status_code, data):
print("{}, {}".format(status_code, data))
self.disconnect()
def main():
with open("credentials.json") as f:
credentials = json.load(f)
stream = MyStreamer(credentials["consumer_key"], credentials["consumer_secret"],
credentials["access_token"], credentials["access_token_secret"])
stream.statuses.filter(track='data')
top_hashtags = Counter(hashtag['text'].lower()
for tweet in tweets
for hashtag in tweet['entities']['hashtags'])
print(top_hashtags.most_common(5))
if __name__ == "__main__": main()