-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweetStreamSentimentAnalyzer.py
More file actions
74 lines (67 loc) · 2.78 KB
/
TweetStreamSentimentAnalyzer.py
File metadata and controls
74 lines (67 loc) · 2.78 KB
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
from tweepy.streaming import StreamListener
import json
import sys
from textblob import TextBlob
import time
from tweepy import OAuthHandler
from tweepy import Stream
# Insert twitter api keys and secrets
consumer_key = 'consumer_key'
consumer_secret = 'consumer_secret'
access_token = 'access_token'
access_token_secret = 'access_token_secret'
class MyStreamListener(StreamListener):
def __init__(self, time_limit=60):
self.start_time = time.time()
self.limit = time_limit
self.totalSentiment = 0
self.totalSubjectivity = 0
self.totalTweets = 0
super(MyStreamListener, self).__init__()
def on_data(self, data):
if (time.time() - self.start_time) < self.limit:
jsonData = json.loads(data)
analysis = TextBlob(jsonData['text'])
self.totalSentiment += analysis.sentiment.polarity
self.totalSubjectivity += analysis.sentiment.subjectivity
self.totalTweets += 1
return True
else:
return False
if __name__ == '__main__':
while True:
print("*******************")
print(" Menu ")
print("*******************")
print("ID Selection")
print("1 Run Tweet Sentiment analysis")
print("2 Exit")
print("*******************")
selection = input("Select a menu option id: ")
while selection != "1" and selection != "2":
print("*******************")
print(" Menu ")
print("*******************")
print("ID Selection")
print("1 Run Tweet Sentiment analysis")
print("2 Exit")
print("*******************")
selection = input("Select a menu option id: ")
if selection == "1":
search = input("Enter search to track: ")
searchLength = input("Enter amount of time to track (minutes): ")
while not searchLength.isnumeric():
searchLength = input("Enter amount of time to track (minutes): ")
listener = MyStreamListener(60 * int(searchLength))
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
print('Listening for Tweets...')
stream = Stream(auth, listener)
stream.filter(track=[search])
print('Done')
print('*******************')
print("Total Tweets: ", listener.totalTweets)
print("Average sentiment: ", listener.totalSentiment / listener.totalTweets)
print("Average subjectivity: ", listener.totalSubjectivity / listener.totalTweets)
else:
sys.exit()