-
Notifications
You must be signed in to change notification settings - Fork 5
/
Twitter_Counter.py
152 lines (104 loc) · 3.65 KB
/
Twitter_Counter.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
149
150
151
###################################
### ###
### Joshua G. Mausolf ###
### Department of Sociology ###
### Computation Institute ###
### University of Chicago ###
### ###
###################################
import re, textwrap
import pandas as pd
import numpy as np
import argparse
#NOTE: Must have TwitterAPI Installed
from TwitterAPI import TwitterAPI
from TwitterAPI import TwitterRestPager
#Insert Your Twitter Credentials Here
from mycredentials import *
#API User Authorization
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
def remove_non_ascii_2(text):
return re.sub(r'[^\x00-\x7F]+', "'", text)
def extract_tweet_info(item, count, df, hashtag):
"""
Utility to Prevent Code Duplication in Counter
"""
n = len(df.index)
tweet_raw = item['text']
tweet = remove_non_ascii_2(tweet_raw)
#Clean up date and time
date_raw = item['created_at'].split(' ')
date = date_raw[1]+" "+date_raw[2]+", "+date_raw[5]
time = date_raw[3]
#Add Row to Data Frame
df.loc[n] = 0
df.ix[n, "DATE"] = date
df.ix[n, "TIME"] = time
df.ix[n, "COUNT"] = count
df.ix[n, "HASHTAG"] = hashtag
df.ix[n, "TWEET"] = tweet
def counter(hashtag, df, limit=None):
count = 0
#Initialize Twitter Rest Pager
r = TwitterRestPager(api, 'search/tweets', {'q':hashtag, 'count':100})
#Limit Option
if limit is not None:
print("requested tweets for hashtag is limited to {} tweets".format(limit))
for item in r.get_iterator(wait=6):
if 'text' in item:
if count <= limit:
print("collecting tweet {} of {}...".format(count, limit))
count += 1
#Extract Tweet Info
extract_tweet_info(item, count, df, hashtag)
else:
print("requested tweet limit reached...")
print("ending query for hashtag...")
return
elif 'message' in item and item['code'] == 88:
print('SUSPEND, RATE LIMIT EXCEEDED: %s' % item['message'])
break
#No Limit
else:
for item in r.get_iterator(wait=6):
if 'text' in item:
print("collecting tweet {} of all available tweets...".format(count))
count += 1
#Extract Tweet Info
extract_tweet_info(item, count, df, hashtag)
elif 'message' in item and item['code'] == 88:
print('SUSPEND, RATE LIMIT EXCEEDED: %s' % item['message'])
break
def collect_tweets_for_hashtags(hashtags, limit):
for hashtag in hashtags:
print ("Collecting tweets for {} ...".format(hashtag))
#Setup Initial Data Frame
header = ["DATE", "TIME", "COUNT", "HASHTAG", "TWEET"]
index = np.arange(0)
df = pd.DataFrame(columns=header, index = index)
#Count Tweets
counter(hashtag, df, limit)
#Save the Results
file_name = hashtag.replace('#', '')+"_Tweets.csv"
print("saving results for {} to {}...".format(hashtag, file_name))
df.to_csv(file_name, encoding='utf-8')
#INPUT YOUR DESIRED HASHTAGS INTO THE ACTIVE LIST BELOW.
#Or Run from the Command Line
#hashtags = ["#Obama", "#Hilary"]
#collect_tweets_for_hashtags(hashtags)
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Prepare input file',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('hashtags', nargs='+', type=str,
help=textwrap.dedent("""\
Input a single hashtag or multiple hashtags, e.g.
'#Hilary'
or
'#Hilary' '#Obama'
The final command takes the following format:
python Twitter_Counter.py '#Hilary' '#Obama' '#Biden'
"""
))
parser.add_argument('-l', '--limit', default=None, type=int, help="max desired number of results per hashtag, default=None")
args = parser.parse_args()
collect_tweets_for_hashtags(args.hashtags, args.limit)