-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweets_app.py
More file actions
145 lines (125 loc) · 6.14 KB
/
Copy pathtweets_app.py
File metadata and controls
145 lines (125 loc) · 6.14 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
from spyre import server
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import tweepy
import cnfg
import datetime
from datetime import date
from pytz import reference
from pattern.en import parse, tag, sentiment, modality, Sentence
plt.style.use('fivethirtyeight')
pd.set_option('display.max_colwidth', -1) #displays full tweet text
config = cnfg.load(".twitter_config")
auth = tweepy.OAuthHandler(config["consumer_key"],
config["consumer_secret"])
auth.set_access_token(config["access_token"],
config["access_token_secret"])
api = tweepy.API(auth)
candidates = [{'name': 'Jeb Bush', 'party': 'R', 'user': 'JebBush'},
{'name': 'Hillary Clinton', 'party': 'D', 'user': 'HillaryClinton'},
{'name': 'Ted Cruz', 'party': 'R', 'user': 'tedcruz'},
{'name': 'Marco Rubio', 'party': 'R', 'user': 'marcorubio'},
{'name': 'Scott Walker', 'party': 'R', 'user': 'ScottWalker'},
{'name': 'Bernie Sanders', 'party': 'D', 'user': 'BernieSanders'},
{'name': 'Rick Perry', 'party': 'R', 'user': 'TeamRickPerry'},
{'name': 'Chris Christie', 'party': 'R', 'user': 'ChrisChristie'},
{'name': 'Rand Paul', 'party': 'R', 'user': 'RandPaul'},
{'name': 'John Kasich', 'party': 'R', 'user': 'JohnKasich'},
{'name': 'Ben Carson', 'party': 'R', 'user': 'RealBenCarson'},
{'name': 'Bobby Jindal', 'party': 'R', 'user': 'BobbyJindal'},
{'name': 'Lindsey Graham', 'party': 'R', 'user': 'LindseyGrahamSC'},
{'name': 'Mike Huckabee', 'party': 'R', 'user': 'GovMikeHuckabee'},
{'name': 'Carly Fiorina', 'party': 'R', 'user': 'CarlyFiorina'},
{'name': "Martin O'Malley", 'party': 'D', 'user': 'MartinOMalley'},
{'name': 'Donald Trump', 'party': 'R', 'user': 'realDonaldTrump'},
{'name': 'George Pataki', 'party': 'R', 'user': 'GovernorPataki'},
{'name': 'Rick Santorum', 'party': 'R', 'user': 'RickSantorum'},
{'name': 'Lincoln Chafee', 'party': 'D', 'user': 'LincolnChafee'},
{'name': 'Jim Webb', 'party': 'D', 'user': 'JimWebbUSA'},
{'name': 'Jill Stein', 'party': 'G', 'user': 'DrJillStein'}
]
class TweetApp(server.App):
def __init__(self):
# caches the data to avoid multiple calls to the twitter API
self.data_cache = None
self.today_cache = None
self.now_cache = None
title = 'Tweets of Presidential Candidates'
inputs =[{ "type":'radiobuttons',
"options": [{"label": "Average Favorites", "value": "Favorites"},
{"label": "Average Retweets", "value": "Retweets"},
{"label": "Average Polarity", "value": "Polarity"},
{"label": "Average Subjectivity", "value": "Subjectivity"},
{"label": "Average Certainty", "value": "Certainty"}
],
"value": 'Favorites',
"key": 'barchart',
"action_id": 'update_data'}]
controls = [{ 'type': "hidden",
'id': 'update_data'}]
tabs = ['Chart', 'Table']
outputs = [{ "type" : "plot",
"id" : "plot_id",
'control_id': 'update_data',
'tab': 'Chart'},
{ 'type': 'table',
'id': 'table_id',
'control_id': 'update_data',
'tab': 'Table',
'sortable': True}]
def getData(self, params):
if self.now_cache is not None:
if (self.now_cache + datetime.timedelta(minutes=5)) < datetime.datetime.now():
self.data_cache = None
self.today_cache = None
self.now_cache = None
if self.data_cache is None:
tweets = []
for cand in candidates:
tweets.append({'tweets': api.user_timeline(cand['user'], count=20),
'name': cand['name'],
'party': cand['party']})
all_tweets = []
for tweet_data in tweets:
name = tweet_data['name']
party = tweet_data['party']
for tweet in tweet_data['tweets']:
all_tweets.append( {'Name': name,
'Tweet': tweet.text,
'Favorites': tweet.favorite_count,
'Retweets': tweet.retweet_count} )
dfs = pd.DataFrame(all_tweets)
sentiments = [sentiment(tweet) for tweet in dfs['Tweet']]
dfs['Polarity'] = [sent[0] for sent in sentiments]
dfs['Subjectivity'] = [sent[1] for sent in sentiments]
modal = [modality(Sentence(parse(tweet, lemmata=True))) for tweet in dfs['Tweet']]
dfs['Certainty'] = modal
today = date.strftime(datetime.datetime.now(), format='%m/%d/%Y, %H:%M')
now = datetime.datetime.now()
self.data_cache = dfs
self.today_cache = today
self.now_cache = now
return self.data_cache
def getPlot(self, params):
df = self.getData(params)
col = params['barchart']
df = pd.DataFrame(df.groupby(['Name'])[col].mean())
df = df.sort(columns=col)
localtime = reference.LocalTimezone()
tz = localtime.tzname(datetime.datetime.now())
plt_obj = df.plot(kind='barh', legend=False)
plt_obj.set_ylabel('')
plt_obj.set_xlabel('Average ' + col)
plt_obj.set_title('20 Most Recent Tweets (' + self.today_cache + ' ' + tz + ')')
# set xlims for specific columns
if col == 'Polarity' or col == 'Certainty':
x1, x2, y1, y2 = plt_obj.axis()
plt_obj.axis((x1, 1.0, y1, y2))
elif col == 'Subjectivity':
plt_obj.set_xlim([0, 1.0])
return plt_obj
if __name__ == '__main__':
app = TweetApp()
app.launch(host='0.0.0.0', port=int(os.environ.get('PORT', '5000')))