-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
240 lines (178 loc) · 6.38 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# import required libraries
try:
import tweepy
from dotenv import load_dotenv
import os
from nltk.sentiment import SentimentIntensityAnalyzer
import matplotlib.pyplot as plt
import sys
import csv
from urllib3.exceptions import ProtocolError
from urllib3 import exceptions as ec
except Exception as e:
print("Error!")
print(e)
class Twitter():
def __init__(self) -> None:
load_dotenv()
api_key, api_secret = os.environ.get(
'API_KEY'), os.environ.get('API_SECRET_KEY')
access_token, access_token_secret = os.environ.get(
'ACCESS_TOKEN'), os.environ.get('ACCESS_TOKEN_SECRET')
# Auth
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
# Setting the API attribute for the class
self.api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
if self.api.verify_credentials():
pass
else:
sys.exit('Authentication Error')
def analyse(self, text):
sia = SentimentIntensityAnalyzer()
scores = sia.polarity_scores(text)
return scores['compound']*10
def historical(self, keyword, startDate, language='en'):
api = self.api
tweets = tweepy.Cursor(api.search, q=keyword,
lang=language, since=startDate, tweet_mode="extended").items()
toPlot = {}
for tweet in tweets:
# Analyse tweet
toPlot[tweet.created_at] = self.analyse(
tweet.full_text) # (Positivity/Negativity Score)
self.plot(toPlot, keyword)
return toPlot
def fetchPopular(self, keyword, num, language='en'):
api = self.api
tweets = tweepy.Cursor(api.search, q=keyword,
lang=language, tweet_mode='extended', result_type='popular').items(num)
toPlot = {}
for tweet in tweets:
# Analyse tweet
toPlot[tweet.created_at] = self.analyse(
tweet.full_text) # (Positivity/Negativity Score)
self.plot(toPlot, keyword)
return toPlot
def plot(self, toPlot, keyword):
x_axis = []
y_axis = []
for x in sorted(toPlot.keys()):
x_axis.append(x)
y_axis.append(toPlot[x])
# Plotting the graph
plt.plot(x_axis[::-1], y_axis[::-1])
plt.gcf().autofmt_xdate()
plt.xlabel("Time")
plt.ylabel("Positivity")
plt.title(f'Positivity graph for {keyword}')
plt.show()
class MaxListener(tweepy.StreamListener):
def on_status(self, status):
if not status.truncated:
tweet_text = status.text
else:
tweet_text = status.extended_tweet['full_text']
if not status.retweeted and 'RT @' not in tweet_text:
score = self.analyse(tweet_text)
toPlot[status.created_at] = score
print("Tweet created at", status.created_at,
'with a score of', score)
def on_error(self, error):
print(error)
return False
def analyse(self, text):
sia = SentimentIntensityAnalyzer()
scores = sia.polarity_scores(text)
return scores['compound']*10
class MaxStream():
def __init__(self, api, listener, keyword):
self.stream = tweepy.Stream(auth=api.auth, listener=listener)
self.keyword = [keyword]
def start(self):
keywords = self.keyword
print('Monitoring...', keywords)
print('Starting Stream')
global toPlot
toPlot = {}
while True:
try:
self.stream.filter(track=keywords)
except ProtocolError as pe:
print(pe)
continue
except ec.TimeoutError as te:
print(te)
continue
except ec.HTTPError as he:
print(he)
continue
except KeyboardInterrupt:
self.plot(toPlot)
break
def plot(self, toPlot):
"""A function to plot the real time data
Args:
toPlot (dictionary): A dictionary of tweets, and their respective scores
"""
x_axis = []
y_axis = []
for x in sorted(toPlot.keys()):
x_axis.append(x)
y_axis.append(toPlot[x])
# Plotting the graph
plt.plot(x_axis[::-1], y_axis[::-1])
plt.gcf().autofmt_xdate()
plt.xlabel("Time")
plt.ylabel("Positivity")
plt.title(f'Positivity graph for {self.keyword[0]}')
plt.show()
def startStream(keyword):
"""A function to start streaming of real time data
Args:
keyword (string): Keyword to be monitored by the bot
"""
# Authentication keys and tokens
API_KEY = os.environ.get('API_KEY')
API_SECRET_KEY = os.environ.get('API_SECRET_KEY')
ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN')
ACCESS_TOKEN_SECRET = os.environ.get('ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
# Starting the stream
listener = MaxListener(api)
stream = MaxStream(api, listener, keyword)
stream.start()
while True:
# User input
inp = input("""Enter your choice.
1. Get analysis for historical data
2. Get analysis for popular tweets
3. Start analysis of real time data
4. Quit
""")
# Twitter object
twitter = Twitter()
if inp.lower() == 'historical':
keyHist = input("""What keyword would you like to monitor?
""")
dateHist = input("""What date would you like the data to be collected from? Format = "YYYY-MM-DD"
""")
twitter.historical(keyHist, dateHist)
elif inp.lower() == 'popular':
keyPopular = input("""What keyword would you like to monitor?
""")
numPopular = int(input("""What amount of popular tweets would you like to analyse?
"""))
twitter.fetchPopular(keyPopular, numPopular)
elif inp.lower() == 'real time':
streamKey = input("""What keyword would you like to monitor?
""")
startStream(streamKey)
elif inp.lower() == 'quit':
break
else:
print('Enter a valid response')