-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhelpers.py
66 lines (59 loc) · 2.27 KB
/
helpers.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
import html
import os
import plotly
import socket
from twython import Twython
from twython import TwythonAuthError, TwythonError, TwythonRateLimitError
def chart(positive, negative, neutral):
"""Return a pie chart for specified sentiments as HTML."""
# offline plot
# https://plot.ly/python/pie-charts/
# https://plot.ly/python/reference/#pie
figure = {
"data": [
{
"labels": ["positive", "negative", "neutral"],
"hoverinfo": "none",
"marker": {
"colors": [
"rgb(0,255,00)",
"rgb(255,0,0)",
"rgb(255,255,0)"
]
},
"type": "pie",
"values": [positive, negative, neutral]
}
],
"layout": {
"showlegend": True
}
}
return plotly.offline.plot(figure, output_type="div", show_link=False, link_text=False)
def get_user_timeline(screen_name, count=200):
"""Return list of most recent tweets posted by screen_name."""
# ensure count is valid
if count < 1 or count > 200:
raise RuntimeError("invalid count")
# ensure environment variables are set
if not os.environ.get("API_KEY"):
raise RuntimeError("API_KEY not set")
if not os.environ.get("API_SECRET"):
raise RuntimeError("API_SECRET not set")
# get screen_name's most recent tweets
# https://dev.twitter.com/rest/reference/get/users/lookup
# https://dev.twitter.com/rest/reference/get/statuses/user_timeline
# https://github.com/ryanmcgrath/twython/blob/master/twython/endpoints.py
try:
twitter = Twython(os.environ.get("API_KEY"), os.environ.get("API_SECRET"))
user = twitter.lookup_user(screen_name=screen_name)
if user[0]["protected"]:
return None
tweets = twitter.get_user_timeline(screen_name=screen_name, count=count)
return [html.unescape(tweet["text"].replace("\n", " ")) for tweet in tweets]
except TwythonAuthError:
raise RuntimeError("invalid API_KEY and/or API_SECRET") from None
except TwythonRateLimitError:
raise RuntimeError("you've hit a rate limit") from None
except TwythonError:
return None