-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPySentiBot.py
182 lines (122 loc) · 4.89 KB
/
PySentiBot.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
# coding: utf-8
# In[ ]:
# Dependencies
import tweepy
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import os
import time
plt.style.use('fivethirtyeight')
# Import and Initialize Sentiment Analyzer
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
consumer_key = os.getenv("bot_consumer_key")
consumer_secret = os.getenv("bot_consumer_secret")
access_token = os.getenv("bot_access_token")
access_token_secret = os.getenv("bot_access_token_secret")
# Setup Tweepy API Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
# In[ ]:
def parse_requests(tweet, tweet_dict=dict()):
tweet_data = []
tweet_id = tweet["id"]
tweet_user = tweet["user"]["screen_name"]
tweet_requests = []
print(tweet_id)
for mentions in tweet["entities"]["user_mentions"]:
if (mentions["screen_name"] != "PySentimentBot"):
tweet_requests.append(mentions["screen_name"])
tweet_dict = {"id":tweet_id,"user":tweet_user,"analysis_requests":tweet_requests}
return tweet_dict
# In[ ]:
def analyze_sentiments(recent_tweets, sentiment_results=list()):
sentiment_results = []
for tweet in recent_tweets:
new_tweet = cleanse_tweet(tweet)
sentiment_result = analyzer.polarity_scores(new_tweet["text"])
sentiment_result.update({"tweet_id":new_tweet["id"]})
sentiment_results.append(sentiment_result)
return sentiment_results
# In[ ]:
def remove_noise(tweet, category, key, result_tweet=dict()):
try:
result_tweet = tweet
tweet_text = tweet.get("text")
tweet_items = tweet.get("entities").get(category)
for item in tweet_items:
replace_str = item[key]
tweet_text = tweet_text.replace(replace_str," ")
result_tweet["text"] = tweet_text
except TypeError:
pass
return result_tweet
def cleanse_tweet(tweet,result_tweet=dict()):
result_tweet = tweet
result_tweet = remove_noise(result_tweet,"user_mentions","screen_name")
result_tweet = remove_noise(result_tweet,"urls","url")
result_tweet = remove_noise(result_tweet,"media","url")
result_tweet["text"] = result_tweet["text"].replace("@","")
return result_tweet
# In[ ]:
def color_map(value):
if(value >= 0):
return 'g'
else:
return 'r'
def plot_sentiments(title,sentiments):
df = pd.DataFrame(sentiments)
df = df.reset_index()
# df.plot(kind="scatter",x="index",y="compound",marker="o")
df.plot( 'index', 'compound', linestyle='-', marker='o',alpha=0.75)
plt.ylabel("Sentiment score")
plt.xlabel("Tweets")
plt.title(title)
filename = "SentimentAnalysis_of_"+title+".png"
plt.savefig(filename)
plt.show()
return filename
# In[ ]:
def scan_for_requests(since_tweet_id):
total_tweets_so_far = 0
last_tweet_id = since_tweet_id
search_handle = "@PySentimentBot"
results = api.mentions_timeline(since_tweet_id)
print(f"Total results retrieved - {len(results)}")
if(len(results) > 0):
tweet_data = []
print(results)
for tweet in results:
parsed_tweet = parse_requests(tweet)
last_tweet_id = tweet['id']
print(f"Parsed tweet - {tweet}")
tweet_data.append(parsed_tweet)
print(f"tweet data - {tweet_data}")
for item in tweet_data:
recent_tweets = []
for analyze_request in item["analysis_requests"]:
recent_tweets = api.user_timeline(analyze_request,count=200)
print(f"{analyze_request} - {len(recent_tweets)}")
if(len(recent_tweets) > 0):
sentiments = analyze_sentiments(recent_tweets)
print(sentiments)
sentiment_fig = plot_sentiments(analyze_request,sentiments)
text_status = f"{datetime.now()} - Thank you for your tweet @{item['user']}! Here is the sentiment analysis of {analyze_request}!"
api.update_with_media(filename=sentiment_fig,status=text_status,in_reply_to_status_id=item["id"])
else:
text_status = f"{datetime.now()} - Thank you for your tweet @{item['user']}! Sorry, {analyze_request} has no tweets!"
api.update_status(text_status)
total_tweets_so_far = total_tweets_so_far + 1
plt.show()
return last_tweet_id
else:
return last_tweet_id
# In[ ]:
since_tweet_id = 937422923572400129
while True:
time.sleep(30)
since_tweet_id = scan_for_requests(since_tweet_id)