-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.py
90 lines (76 loc) · 2.54 KB
/
stream.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from analysis import Analyzer
import tweepy
import pickle
import json
import time
import code
import analysis
CONSUMER_KEY = "7ZI3BzYpi1v0ZTJvsbjB696tN"
CONSUMER_SECRET = "vzwV9riclXjPYv1TqQUjN9UaoAAiet9EAHqjPYdWIOcQ1DQWYj"
ACCESS_TOKEN = "2982406223-rIgKwKlpduITV7hkrreYBkFQDw0AY7wiMDR79pr"
ACCESS_TOKEN_SECRET = "T2lzQA9YFWxuHeYmyLM6715iXlXUg8fl8TzWpv9PugRzj"
lim = 30
class StdOutListener(StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
tweets = []
count = 0
stop = False
def close(self):
self.stop = True
def on_data(self, data):
if self.stop:
print('Stream closed.')
return False
self.count += 1
d = json.loads(data)
self.tweets.append(d)
# overwriting the file
open('tweet_stream.pickle', 'w+').close()
if self.count >= lim:
with open('tweet_stream.pickle', 'wb') as f:
pickle.dump(self.tweets, f)
return False
return True
def on_error(self, status):
print(status)
def gather_tweets(api, auth, username=None, keyword=None, limit=30):
if username:
tweets = []
count = 0
for page in tweepy.Cursor(api.user_timeline, id=username).pages(10):
for tweet in page:
print(tweet.text.encode('utf-8'))
tweets.append(tweet._json)
count += 1
if count > limit:
break
return tweets
elif keyword:
l = StdOutListener()
stream = Stream(auth, l)
global lim
lim = limit
stream.filter(track=[keyword])
with open('tweet_stream.pickle', 'rb') as f:
return pickle.load(f)
else:
raise ValueError('Invalid Arguments. username and keyword both' +
'can\'t be None')
if __name__ == '__main__':
s = 'baltimore'
auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
# tweets = gather_tweets(username=s) # last 30 tweets
tweets = gather_tweets(keyword=s, limit=30)
# Create analyzer
analyzer = Analyzer(tweets, s)
avg = analyzer.calc_sentiment()
#keywrds = analyzer.get_keywords()
analyzer.save_sentiment_data()