forked from mac389/Maui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-usertimeline.py
47 lines (36 loc) · 1.31 KB
/
process-usertimeline.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
import json, tweepy
filename = 'usertimeline.json'
READ = 'rb'
tweets = json.load(open(filename,READ))
#Which hashtags were used?
# Consumer keys and access tokens, used for OAuth
READ = 'rb'
WRITE = 'wb'
tokens = json.load(open('tokens.json',READ))
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(tokens['consumer_key'], tokens['consumer_secret'])
auth.set_access_token(tokens['access_token'], tokens['access_token_secret'])
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
def print_location(obj):
tweet = {}
tweet['location'] = obj.user.location.encode('utf8')
#tweet['time-zone'] = obj.user.time_zone
#tweet['isGeo'] = obj.geo
return tweet
TEXT=1
hashtags = [word for tweet in tweets for word in tweet['text'][TEXT].split() if '#' in word]
print hashtags
#Which tweet is most popular? (Which tweet has the most retweets?)
retweets = sorted(tweets,key = lambda tweet: tweet['retweet_count'],reverse=True)
#print retweets[0]
#Get location from tweet
locations = [tweet['location'] for tweet in tweets]
#print locations
#Get locations from those who retweeted the most popular tweet
most_popular_tweet = retweets[0]
try:
locations_of_retweeters = api.retweets(most_popular_tweet['id'])
except:
pass
print map(print_location,locations_of_retweeters)