-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_news.py
148 lines (131 loc) · 5.25 KB
/
twitter_news.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
import tweepy
import secrets
import argparse
import datetime
from ascii_graph import Pyasciigraph
import collections
from collections import OrderedDict
from ascii_graph.colors import *
from ascii_graph.colordata import vcolor
from ascii_graph.colordata import hcolor
from datadiff import diff
auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
auth.set_access_token(secrets.access_token, secrets.access_token_secret)
twitter_api = tweepy.API(auth)
parser = argparse.ArgumentParser()
parser.add_argument("-w", "--woeid",
help="the woeid of where trends should come from")
parser.add_argument("-t", "--top", default=10, type=int,
help="the number of trends to get: default is 10")
# parser.add_argument("-l", "--location", default='Philadelphia', type=str,
# help="the location of trends you would like to get")
parser.add_argument('-lol', '--listoflocations', nargs='*',
help='a list of locations')
parser.add_argument("-q", "--query",
help="gives tweets for a certain topic")
parser.add_argument("-c", "--compare", nargs='*',
help="compare trends from two different woeids")
args = parser.parse_args()
def get_woeids():
"""
make a dict of woeids and location names
"""
places = twitter_api.trends_available()
all_woeids = {place['name'].lower(): place['woeid'] for place in places}
# store the woeid in a dictionary with its correspoding name
return all_woeids
def trends_and_volumes(woeid):
"""
create a dict of trends from a certain woeid, and tweet_volume
:return: dict
"""
if args.woeid:
woeid = args.woeid
trending_topics = []
tweet_volume = []
woeid_trends = twitter_api.trends_place(woeid)
for woeid_trend in woeid_trends:
for x in range(args.top):
trending_topics.append(woeid_trend['trends'][x]['name'])
# if the trend volume is none, replace it with 0
# so that graphing the trend is easier
if woeid_trend['trends'][x]['tweet_volume'] is None:
woeid_trend['trends'][x]['tweet_volume'] = 0
tweet_volume.append(woeid_trend['trends'][x]['tweet_volume'])
tempdict = dict(zip(trending_topics, tweet_volume))
trend_volume_dict = OrderedDict(sorted(tempdict.items(),
key=lambda t: t[1]))
items = list(trend_volume_dict.items())
items.reverse()
trend_volume_dict = OrderedDict(items)
return trend_volume_dict
def location_to_woeid(location):
"""
if user inputs a location instead of woeid
translate that location to a valid woeid (if there is one)
"""
location = location.lower()
trends = twitter_api.trends_available()
all_woeids = get_woeids()
if location in all_woeids:
return all_woeids[location]
else:
# The location provided does not match any of the locations
# available for trends from Twitter
raise ValueError('This location cannot be resolved to a WOEID')
def plot_trends_list(locationlist):
"""
create a graph of trends and trend volumes, sorted by trend volume
"""
if args.listoflocations:
locationlist = list(args.listoflocations)
for loc in locationlist:
trends_plus_volumes = trends_and_volumes(location_to_woeid(loc))
pattern = [Gre, Yel, Red, Cya]
chart = trends_plus_volumes.items()
data = vcolor(chart, pattern)
graph = Pyasciigraph()
time = datetime.datetime.now().time()
for line in graph.graph("These are the current " +
"trends for {} as of {}".format(
loc.title(), time), data):
print(line)
def compare_trends(*woeids):
shared_trends = []
loc1trends = trends_and_volumes(location_to_woeid(args.compare[0]))
loc2trends = trends_and_volumes(location_to_woeid(args.compare[1]))
for k1, v1 in loc1trends.items():
for k2, v2 in loc2trends.items():
if k1 == k2:
shared_trends.append(k1)
shared_trends = ' '.join(shared_trends)
if len(shared_trends) == 0:
print("Sorry, there are no shared trends between " +
args.compare[0].title() + " and " + args.compare[1].title())
else:
print("The shared trends between " + args.compare[0].title() +
" and " + args.compare[1].title() + " are " + shared_trends)
def query_tweets(query):
"""
takes in a trend, and returns 5 popular tweets that contain that query
"""
# we don't want tweets with links, it looks nicer this way
query = query + " -filter:links"
tweets = twitter_api.search(query, result_type='popular',
lang='en', count=5)
if len(tweets) == 0:
print("There are no tweets that match your query, " +
"please try another query")
for tweet in tweets:
print(tweet.text)
def main():
time = datetime.datetime.now().time()
woeids = get_woeids()
if args.listoflocations:
plot_trends_list(args.listoflocations)
if args.query:
query_tweets(args.query)
if args.compare:
compare_trends(args.compare)
if __name__ == '__main__':
main()