-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.py
318 lines (263 loc) · 10.9 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
# Import modules
import TwitterAPI
from TwitterAPI import TwitterAPI
from TwitterAPI import TwitterRestPager
import urllib.request
from tabulate import tabulate
import os
import time
import dbTest
def loadSettings():
credentials = {}
with open("settings/authSettings.txt", 'r') as f:
for line in f:
items = line.split()
(key, values) = (items[0], items[1])
credentials[key] = values
return credentials
def postTweet(apiKey, apiSecret, accessToken, accessTokenSecret, text):
api = TwitterAPI(apiKey, apiSecret, accessToken, accessTokenSecret)
r = api.request('statuses/update', {'status': text})
print('SUCCESS' if r.status_code == 200 else 'FAILURE')
def streamTweets(apiKey, apiSecret, accessToken, accessTokenSecret,
trackTerm, lang='-', nTweets=100, nFlush=100, media='-'): #,
#mSize='medium', saveMedia=False, viewMedia=True,
#workDir='cache', saveLog=True, logName='log.txt'):
api = TwitterAPI(apiKey, apiSecret, accessToken, accessTokenSecret)
# Create directories and files, etc.
#curTime = time.strftime("%d_%b_%Y_%H.%M.%S")
#if not saveMedia:
# workDir = "cache"
#if viewMedia:
# if not os.path.exists(workDir):
# os.makedirs(workDir)
# os.chdir(workDir)
#if saveLog:
# f = open(logName, "w")
#print("Started fetching will following parameters:")
#print("trackTerm: ", trackTerm)
#print("lang: ", lang)
#print("nTweets: ", nTweets)
#print("nFlush: ", nFlush)
#print("media: ", media)
#print("mSize: ", mSize)
#print("viewMedia: ", viewMedia)
#print("saveMedia: ", saveMedia)
#print("workDir: ", workDir)
#print("saveLog: ", saveLog)
#print("logName: ", logName)
# Create counters
current = 0
total = 0
data = []
stream = []
# Create table header for printing
tableHeader=["Name", "Handle", "Text", "Time stamp", "Hashtags",
"Retweets", "Favorites", "Media", "Language", "Img Path"]
keys = ["name", "handle", "content", "time", "hashtags", "rtCount",
"favCount", "media", "lang", "imgName"]
# Search
r = api.request('statuses/filter', {'track': trackTerm})
# For each tweet
for tweet in r.get_iterator():
if 'text' in tweet: # if it's really a tweet and not something else
#print(tweet)
#break
# Check if it fits the media requirements (yes, no, don't care)
if media != "-":
cMedia = True if media == True else False
if ('media' not in tweet['entities']) & (cMedia == True):
continue
elif ('media' in tweet['entities']) & (cMedia == False):
continue
else:
if 'media' in tweet['entities']:
cMedia = True
else:
cMedia = False
# Check if it fits the language requirements (anything or specific)
if lang != "-":
tLang = lang
if tweet['metadata'] != tLang:
continue
else:
tLang = tweet['lang']
# If no hashtags
if tweet['entities']['hashtags']:
hashtags = ""
for tag in tweet['entities']['hashtags']:
hashtags = hashtags + tag['text'] + ", "
else:
hashtags = None
fileName = None
#if cMedia & viewMedia:
# cMedia += len(tweet['entities']['media'])
# mediaURL = tweet['entities']['media'][0]['media_url']
# fileName = str(total)+mediaURL[-4:] # last 4 are extension
# urllib.request.urlretrieve(mediaURL+":"+mSize, fileName)
# Push the tweet onto the stream stack
stream.append([tweet['user']['name'], tweet['user']['screen_name'],
tweet['text'].replace('\n', ' '), tweet['created_at'],
hashtags, tweet['retweet_count'], tweet['favorite_count'],
cMedia, tLang, fileName])
# increment the counters
current += 1
total += 1
# every 100 tweets, flush the stream to improve performance and add to a big stream
if current == nFlush:
dictList = []
for i in range(current):
dictList.append(dict(zip(keys, stream[i])))
print("Updating database. Total = ", total)
dbTest.updateDB(dictList)
stream = [] # empty stack
current = 0 # reset counter
# max number of tweets
if total >= nTweets:
dictList = []
for i in range(current):
dictList.append(dict(zip(keys, stream[i])))
print("Updating database. Total = ", total)
dbTest.updateDB(dictList)
break
# this should not trigger, but just in case
# this handles an exception triggered when we send more than 1 request every 5 seconds
# this would result in a 15 minute block
elif 'message' in item and item['code'] == 88:
print('SUSPEND, RATE LIMIT EXCEEDED: %s' % item['message'])
break
#print(count)
# print table
#if saveLog:
# table = tabulate(data, headers=tableHeader, tablefmt='fancy_grid')
# f.write(table)
#for i in range(total):
# print(data[i])
#os.chdir("..")
print("Done Streaming!")
def fetchTweets(apiKey, apiSecret, accessToken, accessTokenSecret,
query, lang='-', nTweets=100, nFlush=100, media='-',
mSize='medium', saveMedia=False, viewMedia=True,
workDir='cache', saveLog=True, logName='log.txt'):
from TwitterAPI import TwitterAPI
from TwitterAPI import TwitterRestPager
api = TwitterAPI(apiKey, apiSecret, accessToken, accessTokenSecret)
# Create directories and files, etc.
curTime = time.strftime("%d_%b_%Y_%H.%M.%S")
if not saveMedia:
workDir = "cache"
if viewMedia:
if not os.path.exists(workDir):
os.makedirs(workDir)
os.chdir(workDir)
if saveLog:
f = open(logName, "w")
print("Started fetching will following parameters:")
print("query: ", query)
print("lang: ", lang)
print("nTweets: ", nTweets)
print("nFlush: ", nFlush)
print("media: ", media)
print("mSize: ", mSize)
print("viewMedia: ", viewMedia)
print("saveMedia: ", saveMedia)
print("workDir: ", workDir)
print("saveLog: ", saveLog)
print("logName: ", logName)
# Create counters
current = 0
total = 0
data = []
stream = []
# Create table header for printing
tableHeader=["Name", "Handle", "Text", "Time stamp", "Hashtags",
"Retweets", "Favorites", "Media", "Language", "Img Path"]
keys = ["name", "handle", "content", "time", "hashtags", "rtCount",
"favCount", "media", "lang", "imgName"]
# tableHeader=["Handle", "Name", "Text", "Time stamp", "Retweets", "Favorites"]
# Search
r = TwitterRestPager(api, 'search/tweets', {'q':query, 'count':100})
# For each tweet
for tweet in r.get_iterator():
if 'text' in tweet: # if it's really a tweet and not something else
# Check if it fits the media requirements (yes, no, don't care)
if media != "-":
cMedia = True if media == True else False
if ('media' not in tweet['entities']) & (cMedia == True):
continue
elif ('media' in tweet['entities']) & (cMedia == False):
continue
else:
if 'media' in tweet['entities']:
cMedia = True
else:
cMedia = False
# Check if it fits the language requirements (anything or specific)
if lang != "-":
tLang = lang
if tweet['metadata']['iso_language_code'] != tLang:
continue
else:
tLang = tweet['metadata']['iso_language_code']
# If no hashtags
if tweet['entities']['hashtags']:
hashtags = ""
for tag in tweet['entities']['hashtags']:
hashtags = hashtags + tag['text'] + ", "
else:
hashtags = None
fileName = None
if cMedia & viewMedia:
cMedia += len(tweet['entities']['media'])
mediaURL = tweet['entities']['media'][0]['media_url']
fileName = str(total)+mediaURL[-4:] # last 4 are extension
urllib.request.urlretrieve(mediaURL+":"+mSize, fileName)
# Push the tweet onto the stream stack
stream.append([tweet['user']['name'], tweet['user']['screen_name'],
tweet['text'].replace('\n', ' '), tweet['created_at'],
hashtags, tweet['retweet_count'], tweet['favorite_count'],
cMedia, tLang, fileName])
# increment the counters
current += 1
total += 1
# every 100 tweets, flush the stream to improve performance and add to a big stream
if current == nFlush:
data.extend(stream) # concatenate
stream = [] # empty stack
current = 0 # reset counter
# max number of tweets
if total >= nTweets:
data.extend(stream) # concatenate
break
# this should not trigger, but just in case
# this handles an exception triggered when we send more than 1 request every 5 seconds
# this would result in a 15 minute block
elif 'message' in item and item['code'] == 88:
print('SUSPEND, RATE LIMIT EXCEEDED: %s' % item['message'])
break
#print(count)
# print table
table = ""
if saveLog:
table = tabulate(data, headers=tableHeader, tablefmt='fancy_grid')
f.write(table)
result = []
dictList = []
for i in range(total):
dictList.append(dict(zip(keys, data[i])))
result = [dictList, total, workDir, table]
os.chdir("..")
print("Done Fetching!")
return result
def main():
print("Running!")
#credentials = loadSettings()
#postTweet(credentials['apiKey'], credentials['apiSecret'],
# credentials['accessToken'], credentials['accessTokenSecret'],
# "This is a test tweet from python.")
#streamTweets(credentials['apiKey'], credentials['apiSecret'],
# credentials['accessToken'], credentials['accessTokenSecret'],
# "boobs", nTweets=50, nFlush=10)
#dbTest.updateDB(tweets[0])
if __name__ == "__main__":
main()