-
Notifications
You must be signed in to change notification settings - Fork 13
/
test.py
50 lines (38 loc) · 1.73 KB
/
test.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
import tweepy, io, json
from getpass import getpass
def serialize_tweepy_object(obj):
print obj.keys,'.......'
tweet = {}
tweet['author-name'] = obj.author.name.encode('utf8')
tweet['screen-name'] = obj.author.screen_name.encode('utf8')
tweet['created-at'] = obj.created_at.strftime('%m-%d-%Y')
tweet['text'] = "Tweet:", obj.text.encode('utf8')
text = obj.text.encode('utf8')
words = ''.join(c if c.isalnum() else ' ' for c in text).split()
tweet['analysis'] = {}
tweet['analysis']['tweet_length'] = len(text)
tweet['analysis']['word-count'] = len(words)
tweet['location'] = obj.user.location.encode('utf8')
tweet['time-zone'] = obj.user.time_zone
tweet['isGeo'] = obj.geo
return tweet
# 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)
for tweet in tweepy.Cursor(api.search, q=('#med2'), since='2014-11-05', until='2014-11-11').items(3):
print '%s --- aka @ %s --- at %s' % (tweet.author.name.encode('utf8'), tweet.author.screen_name.encode('utf8'), tweet.created_at)
print "\n"
print "Tweet:", tweet.text.encode('utf8')
print "\n"
print 'Retweeted? %s Favorited? %s' % (tweet.retweeted, tweet.favorited)
print "\n"
print 'Location? %s Time Zone? %s Geo-coded? %s' % (tweet.user.location.encode('utf8'), tweet.user.time_zone, tweet.geo)
print "\n"
print "-------------------------------"
print "\n"