-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlotBot.py
More file actions
181 lines (127 loc) · 4.47 KB
/
PlotBot.py
File metadata and controls
181 lines (127 loc) · 4.47 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
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[1]:
#Depenencies, initialize api and analyzer
import matplotlib
matplotlib.use('Agg')
import tweepy
import json
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
import tconfig_h
import time
api = tconfig_h.auth()
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
# ### Functions
# In[2]:
#Create list of compound sentiment scores for user
def score_list(target_user):
sentiment_list = []
# Loop through 25 pages of tweets (total 500 tweets)
for x in range(25):
# Get all tweets from target user's feed
public_tweets = api.user_timeline(target_user, page=x)
if len(public_tweets)==0:
break
for tweet in public_tweets:
# Run Vader Analysis on each tweet
text = tweet["text"]
score = analyzer.polarity_scores(text)["compound"]
sentiment_list.append(score)
return sentiment_list
# In[3]:
def imgpath(user):
return f"{user}_plot.png"
# In[4]:
#Plot sentiments over time, save image
def plot_sentiment(user, slist):
td=datetime.today()
date=f"{td.month}/{td.day}/{td.year%100}"
x_axis = np.arange(len(slist))
plt.figure(figsize=[10,6])
plt.plot(x_axis,slist,alpha=.8,linewidth=.5,marker="o",mew=0,label="@"+user,)
plt.xlabel("Tweets Ago",fontsize=13)
plt.ylabel("Tweet Polarity",fontsize=13)
plt.title(f"Sentiment Analysis of Tweets ({date})",fontsize=14,)
plt.legend(loc=6,bbox_to_anchor=(1,0.5),fontsize=12)
plt.grid(alpha=0.3)
plt.savefig(imgpath(user),pad_inches=0.1,bbox_inches="tight")
plt.show()
# In[5]:
#Tweet out saved plot
def tweet_plot(user, requester):
text = f"New Tweet Analysis: @{user} (Requested by @{requester})"
api.update_with_media(imgpath(user),text)
# In[6]:
#Returns viable username from beginning of a string
def get_handle(req):
req=req.strip("@")
ind = None
for i in range(len(req)):
char=req[i]
if not (char.isalnum() or char=="_"):
ind = i
break
return req[:ind]
# In[27]:
#Get a list of most recent plotbot requests
def get_latest_requests(recent_id, max_requests=10):
# "Real Person" Filters
min_tweets = 5
max_tweets = 10000
max_followers = 2500
max_following = 2500
req_list=[]
# Retrieve 100 tweets
searched_tweets = api.search("@PlotBot", count=max_requests, result_type="recent",since_id=recent_id)
for tweet in searched_tweets["statuses"]:
#Filter out possible bots
if not (tweet["user"]["followers_count"] < max_followers and
tweet["user"]["statuses_count"] > min_tweets and
tweet["user"]["statuses_count"] < max_tweets and
tweet["user"]["friends_count"] < max_following):
continue
if tweet["text"].lower().startswith("@plotbot analyze:"):
req_text = tweet["text"].lower().replace("@plotbot analyze:","").strip(" ")
if req_text.startswith("@"):
target_user = get_handle(req_text)
requester=tweet["user"]["screen_name"]
req_id=tweet["id"]
req_list.append((target_user,requester,req_id))
else: print("Not a handle:", req_text)
#else: print(tweet["text"])
return req_list
# In[26]:
#Here we go!
reqs=get_latest_requests("",max_requests=20)
stale_targets = ["plotbot5","khmccurdy","uwebollraw","muffiniffum","cnn","cloud_chaoszero"]
while True:
for t in reqs:
target_user = t[0]
requester = t[1]
recent_id = t[2]
if target_user in stale_targets:
print("Stale target:", target_user)
continue
print("----")
print(f"Preparing to analyze @{target_user}...")
try:
sl=score_list(target_user)
print(f"Obtained scores for @{target_user}.")
except tweepy.error.TweepError as e:
print(e, "For username:", target_user)
continue
plot_sentiment(target_user,sl)
print(f"Created sentiment plot for @{target_user}.")
print(imgpath(target_user))
try:
tweet_plot(target_user,requester)
print("Successfully tweeted!")
except tweepy.error.TweepError as e:
print(e)
stale_targets.append(target_user)
print("Done for now, see you in 5 minutes.")
#break
time.sleep(300)
reqs=get_latest_requests(recent_id)