-
Notifications
You must be signed in to change notification settings - Fork 3
/
twitter.py
387 lines (327 loc) · 10.6 KB
/
twitter.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
__author__ = 'dreyco676'
import csv
import re
from twython import Twython
import json
import time
import sys
import datetime
import math
import os.path
class TwitterUser:
def __init__(self):
self._app_key = None
self._app_secret = None
self._oauth_token = None
self._oauth_token_secret = None
#APP_KEY
@property
def app_key(self):
return self._app_key
@app_key.setter
def app_key(self, value):
self._app_key = value
@app_key.deleter
def app_key(self):
del self._app_key
#APP_SECRET
@property
def app_secret(self):
return self._app_secret
@app_secret.setter
def app_secret(self, value):
self._app_secret = value
@app_secret.deleter
def app_secret(self):
del self._app_secret
#OAUTH_TOKEN
@property
def oauth_token(self):
return self._oauth_token
@oauth_token.setter
def oauth_token(self, value):
self._oauth_token = value
@oauth_token.deleter
def oauth_token(self):
del self._oauth_token
#OAUTH_TOKEN_SECRET
@property
def oauth_token_secret(self):
return self._oauth_token_secret
@oauth_token_secret.setter
def oauth_token_secret(self, value):
self._oauth_token_secret = value
@oauth_token_secret.deleter
def oauth_token_secret(self):
del self._oauth_token_secret
def read_app_json(self):
#read the application credentials
json_data = open('app_auth.json')
data = json.load(json_data)
self.app_key = data["app_key"]
self.app_secret = data["app_secret"]
json_data.close()
def auth_url(self):
self.read_app_json()
#get auth url from twitter
twitter = Twython(self.app_key, self.app_secret)
auth = twitter.get_authentication_tokens()
#set oauth token
self.oauth_token = auth["oauth_token"]
self.oauth_token_secret = auth["oauth_token_secret"]
url = auth['auth_url']
return url
def save_user(self, oauth_verifier):
twitter = self.auth()
user_cred = twitter.get_authorized_tokens(oauth_verifier)
with open('user_auth.json', 'w+') as outfile:
json.dump(user_cred, outfile)
def read_user_json(self):
json_data = open('user_auth.json')
data = json.load(json_data)
self.oauth_token = data["oauth_token"]
self.oauth_token_secret = data["oauth_token_secret"]
json_data.close()
def new_user(self):
if os.path.isfile('user_auth.json'):
return False
else:
return True
def auth(self):
self.read_app_json()
twitter_auth = Twython(self.app_key, self.app_secret, self.oauth_token, self.oauth_token_secret)
return twitter_auth
class TwitterOutput(object):
def __init__(self):
#twitter documentation
#https://dev.twitter.com/rest/reference/get/search/tweets
self._file_name = 'output.tsv'
self._result_set = None
#RESULT_SET
@property
def result_set(self):
return self._result_set
@result_set.setter
def result_set(self,value):
self._result_set = value
@result_set.deleter
def result_set(self):
del self._result_set
#FILE_NAME
@property
def file_name(self):
return self._file_name
@file_name.setter
def file_name(self,value):
if value is None:
self._file_name = 'Output.tsv'
else:
self._file_name = value
@file_name.deleter
def file_name(self):
del self._file_name
def json_output(self):
name = self.file_name
f = open(name, "w+")
json.dump(self.result_set, f, indent=4)
f.close()
def tsv_output(self):
name = self.file_name
with open(name, "w+",encoding='utf-8') as the_file:
writer = csv.writer(the_file, delimiter='\t', lineterminator='\n')
header = ['tweetID','timeCreatedUTC','timeOffset','tweetText','favoriteCount','retweetCount','language',
'place','twitterHandle','accountLocation','accountName','source','geoCoords']
writer.writerow(header)
data = self.result_set
for tweetObject in data:
try:
timeCreatedUTC = tweetObject["created_at"]
except:
timeCreatedUTC = None
try:
favoriteCount = tweetObject["favorite_count"]
except:
favoriteCount = 0
try:
geoCoords = tweetObject["coordinates"]
except:
geoCoords = None
try:
tweetID = tweetObject["id"]
except:
tweetID = None
try:
retweetCount = tweetObject["retweet_count"]
except:
retweetCount = 0
try:
source = tweetObject["source"]
except:
source = None
try:
tweetText = re.sub('\s+',' ',tweetObject["text"])
except:
tweetText = None
try:
language = tweetObject["lang"]
except:
language = None
try:
place = tweetObject["place"]
except:
place = None
try:
timeOffset = tweetObject["user"]["utc_offset"]
except:
timeOffset = None
try:
twitterHandle = tweetObject["user"]["screen_name"]
except:
twitterHandle = None
try:
accountLocation = tweetObject["user"]["lang"]
except:
accountLocation = None
try:
accountName = re.sub('\s+',' ',tweetObject["user"]["name"])
except:
accountName = None
tweetData = [tweetID,timeCreatedUTC,timeOffset,tweetText,favoriteCount,retweetCount,language,
place,twitterHandle,accountLocation,accountName,source,geoCoords]
writer.writerow(tweetData)
class TwitterRequest(object):
def __init__(self):
self._keyword = None
self._geocode = None
self._lang = None
self._result_type = None
self._max_results = None
self._max_date = None
self._session = None
#twitter documentation
#https://dev.twitter.com/rest/reference/get/search/tweets
#KEYWORD
@property
def keyword(self):
return self._keyword
@keyword.setter
def keyword(self,value):
self._keyword = value
@keyword.deleter
def keyword(self):
del self._keyword
#GEOCODE
@property
def geocode(self):
return self._geocode
@geocode.setter
def geocode(self,value):
if len(value) == 0:
self._geocode = None
else:
self._geocode = value
@geocode.deleter
def geocode(self):
del self._geocode
#LANG
@property
def lang(self):
return self._lang
@lang.setter
def lang(self,value):
if value == 'None':
self._lang = None
else:
self._lang = value
@lang.deleter
def lang(self):
del self._lang
#RESULT_TYPE
@property
def result_type(self):
return self._result_type
@result_type.setter
def result_type(self,value):
self._result_type = value
@result_type.deleter
def result_type(self):
del self._result_type
#MAX_RESULTS
@property
def max_results(self):
return self._max_results
@max_results.setter
def max_results(self,value):
try:
self._max_results = int(value)
except:
self._max_results = 15
@max_results.deleter
def max_results(self):
del self._max_results
#MAX_DATE
@property
def max_date(self):
return self._max_date
@max_date.setter
def max_date(self,value):
if len(value) == 0:
self._max_date = None
else:
self._max_date = value
@max_date.deleter
def max_date(self):
del self._max_date
#SESSION
@property
def session(self):
return self._session
@session.setter
def session(self,value):
self._session = value
@session.deleter
def session(self):
del self._session
def make_request(self):
#for paging read tweets older than max_id
max_id = None
data = {}
if self.max_results > 100:
while self.max_results > 100:
start_time = datetime.datetime.now()
request = self.session.search(q=self.keyword, geocode=self.geocode, lang=self.lang,
result_type=self.result_type, count=100, until=self.max_date, max_id=max_id)
#add request results to output
if max_id is None:
data = request["statuses"]
else:
data.extend(request["statuses"])
#read the results to get the last id and pass as max_id
count_returned = request["search_metadata"]["count"]
max_id = request["search_metadata"]["max_id"]
#if no more tweets break out of loop
if count_returned == 0:
count_returned = sys.maxsize
self.max_results -= count_returned
#Rate Handling
#twitter rate limits at 180/15min = 1/5sec
end_time = datetime.datetime.now()
elapsed_sec = (end_time - start_time).total_seconds()
time.sleep(5 - elapsed_sec)
#make request that is >=100
request = self.session.search(q=self.keyword, geocode=self.geocode, lang=self.lang,
result_type=self.result_type, count=self.max_results, until=self.max_date, max_id=max_id)
#add request results to output
if max_id is None:
data = request["statuses"]
else:
data.extend(request["statuses"])
return data
def estimate_time(self):
#see twitter rate handling for more info
#last update 2014-11-28
max_call_results = 100
num_requests = math.ceil(self.max_results / max_call_results)
api_rate = 5
estimate_completion = num_requests * api_rate
return estimate_completion