-
Notifications
You must be signed in to change notification settings - Fork 6
/
tweetStatus.py
78 lines (63 loc) · 2.42 KB
/
tweetStatus.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
import csv
from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream
import ConfigParser
import pprint
import time
pp = pprint.PrettyPrinter(indent=4)
config = ConfigParser.ConfigParser()
config.read('credentials.txt')
consumer_key = config.get("TWITTER","consumer_key")
consumer_secret = config.get("TWITTER","consumer_secret")
access_token = config.get("TWITTER","access_token")
access_token_secret = config.get("TWITTER","access_token_secret")
oauth = OAuth(access_token, access_token_secret,consumer_key, consumer_secret)
twitter = Twitter(auth=oauth)
#tweets = twitter.statuses.user_timeline(screen_name="Adele", count = 2)
def getData(usernames,counter):
for username in usernames:
if username.startswith('"') and username.endswith('"'): username = username[1:-1]
if counter == 850:
time.sleep(1000)
counter=0
try:
tweets = twitter.statuses.user_timeline(screen_name=username, count = 100)
counter+=1
#pp.pprint(tweets)
print len(tweets)
data = dataFormat(tweets,username)
with open('test_tweets.csv','a') as c:
wr = csv.writer(c, quoting=csv.QUOTE_ALL)
wr.writerow(data)
c.close()
except Exception as e:
print username,e
print 'done'
def dataFormat(tweets,username):
created_at =[]
texts = []
in_reply = 0
total_usrmention = 0
fav_count = 0
retweet_count = 0
for tweet in tweets:
if 'created_at' in tweet:
created_at.append(''.join(tweet['created_at']).encode('utf-8').strip())
if (('in_reply_to_screen_name' in tweet) and (tweet['in_reply_to_screen_name'] is not None)):
in_reply += 1
if 'text' in tweet:
texts.append(''.join(tweet['text']).encode('utf-8').strip())
if 'entities' in tweet and tweet['entities']['user_mentions']:
total_usrmention +=len(tweet['entities']['user_mentions'])
if 'favorite_count' in tweet and tweet['favorite_count'] > 0:
fav_count +=1
if 'retweet_count' in tweet and tweet['retweet_count'] > 0:
retweet_count +=1
data = [username,in_reply,retweet_count,fav_count,total_usrmention,created_at,texts]
return data
userList = []
with open('usertext.txt','rU') as f:
for line in f:
userList += line.strip(),
print userList
counter = 0
getData(userList,counter)