-
Notifications
You must be signed in to change notification settings - Fork 5
/
twitter_python_version.py
73 lines (56 loc) · 2.33 KB
/
twitter_python_version.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
import twitter
from textblob import TextBlob
from datetime import date, timedelta
import math
api = twitter.Api(consumer_key='TErus3u1UOUgdR6AKhIaaE9Wg',
consumer_secret='UI8yW0epbpuX9CxNl91eSVCGtmDh9IBkUvopH2TvPIaLjVgOCy',
access_token_key='1234781863597760512-SAmWryVIvcqjHM44kTbHhTS0SFExu6',
access_token_secret='QPigS2G8RF6wtxiVROnwTAzduh16CuewaBtgOgQdAa5Zv',
sleep_on_rate_limit=True,
tweet_mode='extended')
dates = []
avg_polarities = []
big_opinions = []
topic = 'coronavirus'
for i in range(7):
polarities = []
max_score = 0
max_opinion = ''
today = (date.today() + timedelta(days=-i)).strftime("%Y-%m-%d")
yesterday = (date.today() + timedelta(days=-i-1)).strftime("%Y-%m-%d")
results = api.GetSearch(term=topic, lang='en', since=yesterday, until=today, count=100) + api.GetSearch(term='#'+topic, since=yesterday, until=today, lang='en', count=100)
for result in results:
text = result.full_text.replace('&', '&')
polarity = TextBlob(text).sentiment.polarity * math.log(1 + result.favorite_count)
polarities.append(polarity)
if abs(polarity) > max_score:
max_score = abs(polarity)
max_opinion = text
dates.append(today)
if len(polarities) > 0:
avg_polarities.append((sum(polarities) / len(polarities)))
else:
avg_polarities.append(0)
big_opinions.append(max_opinion)
avg_polarities = [x for x in avg_polarities if x != 0][::-1]
dates = dates[:len(avg_polarities)][::-1]
big_opinions = big_opinions[:len(avg_polarities)][::-1]
for bo in big_opinions:
print(bo, "\n")
import matplotlib.pyplot as plt
from datetime import datetime
#Examples
#x = ['2020-02-24', '2020-02-25', '2020-02-26', '2020-02-27', '2020-02-28', '2020-02-29', '2020-03-01', '2020-03-02', '2020-03-03', '2020-03-04']
#y = [-10.532258804716355, 302.33430923829405, 46.625526925121406, 795.4368023574394, 432.7554295235555, 1480.4161395262813, -161.16961558096256, 194.4875581435194, 165.01653453626625, -22.052069277284346]
x = dates
y = avg_polarities
xtick_labels = [d[-5:] for d in x]
for i in range(len(x)):
x[i] = datetime.strptime(x[i], "%Y-%m-%d")
#ax = plt.subplot(111)
#ax.bar(x, y, width=0.3)
#ax.xaxis_date()
#ax.set_xticklabels(xtick_labels)
plt.plot(xtick_labels, y)
plt.axhline(0, color='gray', dashes=(3, 3))
plt.show()