-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcollect_tweets_notebook.py
343 lines (282 loc) · 12.5 KB
/
collect_tweets_notebook.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Sample script that collects tweets matching a string.
'''Collect tweets matching a text pattern and store them
continuously in JSON-formatted lines of a local file.'''
__author__ = 'Giorgos Keramidas <[email protected]>'
__moderator__ = '[email protected]'
import argparse
import errno
import json
import os
import sys
import twitter ##pip install --user python-twitter
import ast
import time
import pickle
import glob
import fnmatch
import os
class UserAuth(object):
"""docstring for UserAuth
Create authentication for Twitter Api
creddir: directory containing the credentials for login default /credentials
auth_cr: a tuple with credentials (CONSUMER_KEY,CONSUMER_SECRET,OAUTH_TOKEN,OAUTH_TOKEN_SECRET)
auth_file: filename of the txt file that has stored the credentials
auth_dict: filename of the credential file that has the dictionary of credentials stored
"""
def __init__(self,auth_cr=(),auth_file=None,auth_dict=None, creddir=None,working_path=None):
# self.args = arg
if working_path==None:
self.working_path=os.getcwd()
else:
self.working_path=working_path
if creddir ==None:
self.creddir=os.path.join('%s' % self.working_path,'credentials/')
else:
self.creddir=os.path.join('%s' % self.working_path,creddir)
try:
os.stat(self.creddir)
except:
os.mkdir(self.creddir)
self.auth_cr=auth_cr
self.auth_file=auth_file
self.auth_dict=auth_dict
self.credentials={}#load_login_cred()
self.auth=None
def get_auth(self):
return self.auth
def check_login(self):
print(self.auth.VerifyCredentials())
def load_login_cred(self):#.auth,args.auth_dict):
'''Loads the credentials for Twitter api from a file if exists or create a new file'''
if len(self.auth_cr)==4:
# cons_key,cons_sec,oa_tok,oa_tok_sec=self.auth_cr
self.credentials['CONSUMER_KEY']=self.auth_cr[0]
self.credentials['CONSUMER_SECRET']=self.auth_cr[1]
self.credentials['OAUTH_TOKEN']=self.auth_cr[2]
self.credentials['OAUTH_TOKEN_SECRET']=self.auth_cr[3]
out_file_name=os.path.join('%s' % self.creddir,'auth_cred.txt')
f=open(out_file_name,'w')
for i in self.credentials:
f.write(i+' , '+str(self.credentials[i])+'\n')
f.close()
if self.auth_file!=None:
f=open(self.auth_file)
for li in f:
li=li.strip()
lil=li.split(' , ')
self.credentials[lil[0]]=lil[1]
f.close()
elif self.auth_dict!=None:
f=open(self.auth_dict)
self.credentials=pickle.load(f)
f.close()
else:
print 'Go to http://twitter.com/apps/new to create an app and get these items.'
'Consumer key, Consumer secret, Access token, Access token secret'
self.credentials['CONSUMER_KEY']=raw_input('Give me the Consumer key: ')
self.credentials['CONSUMER_SECRET']=raw_input('Give me the Consumer secret: ')
self.credentials['OAUTH_TOKEN']=raw_input('Give me the Access token: ')
self.credentials['OAUTH_TOKEN_SECRET']=raw_input('Give me the Access token secret: ')
out_file_name=os.path.join('%s' % self.creddir,'auth_cred.txt')
f=open(out_file_name,'w')
for i in self.credentials:
f.write(i+' , '+str(self.credentials[i])+'\n')
f.close()
def login(self):
if len(self.credentials)==0:
self.load_login_cred()
self.auth = twitter.Api(consumer_key=self.credentials['CONSUMER_KEY'], \
consumer_secret=self.credentials['CONSUMER_SECRET'], \
access_token_key=self.credentials['OAUTH_TOKEN'],\
access_token_secret=self.credentials['OAUTH_TOKEN_SECRET'])
class TwitterSearch(object):
"""docstring for TwitterSearch
"""
def __init__(self, auth,search_text='',working_path=None,out_file_dir=None,max_pages=10,results_per_page=100,sin_id=None,max_id=None,verbose=False):
self.search_text=unicode(search_text,'utf-8')
self.auth=auth
if working_path==None:
self.working_path=os.getcwd()
else:
self.working_path=working_path
if out_file_dir==None:
self.out_file_dir=os.path.join('%s' % self.working_path,'Output')
else:
self.out_file_dir=os.path.join('%s' % self.working_path,out_file_dir)
try:
os.stat(self.out_file_dir)
except:
os.mkdir(self.out_file_dir)
filename=''
for term in search_text.split():
filename+=term+'_'
self.out_file_name=os.path.join('%s' % self.out_file_dir,filename[:-1]+'.json')
self.out_file_name_ids=os.path.join('%s' % self.out_file_dir,filename[:-1]+'.ids')
# self.seen=preload_tweets()
self.max_pages=max_pages
self.results_per_page=results_per_page
self.sin_id=sin_id
self.max_id=max_id
self.verb=verbose
def get_max_id(self):
return self.max_id
def set_max_id(self,maxid):
self.max_id=maxid
def get_search_text(self):
return self.search_text
def set_search_text(self,term):
self.search_text=unicode(term,'utf-8')
def preload_tweets(self):
"""Preload previously seen tweets from a text file.
Returns:
A set() containing all the numeric 'id' attributes of tweets we have
already seen.
"""
if not os.path.isfile(self.out_file_name_ids):
return set()
else:
try:
seen = set()
filename_o=open(self.out_file_name_ids)
# print filename
for k in filename_o:
# print k
try:
kk=json.loads(k)
seen.add(kk)
except Exception, e:
if self.verb:
print e
print 'Minor Error on line {}'.format(sys.exc_info()[-1].tb_lineno)
continue
except Exception, e:
seen = set() # Avoid returning partial results on error
print len(seen),e
print 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno)
filename_o.close()
return seen
def search(self):
"""Generator for searching 'text' in Twitter content
Search the public Twitter timeline for tweets matching a 'text' string,
which can also be a hash tag, and yield a batch of matched tweets every
time we have some results.
Returns:
An array of dicts. Every dict in the returned array is a 'result' from
twitter.Twitter.search and represents a single tweet.
"""
while True:
for page in range(1, self.max_pages + 1):
if self.verb:
print self.get_max_id(),'================'
yield self.auth.GetSearch(term=self.search_text,until=self.sin_id,
count=self.results_per_page,max_id=self.max_id)
# def get_max_id(self):
# return self.max_id
# def set_max_id(self,maxid):
# self.max_id=maxid
# def get_search_term(self):
# return self.search_text
# def set_search_term(self,term):
# self.search_text=unicode(term,'utf-8')
def streamsearch(self):#ofile, text,args, max_pages=2000, results_per_page=200,from_date=FROM_DATE):
"""Stream the results of searching for 'text' to the 'ofile' output file
Args:
ofile str, the name of a file where we will write any tweets
we find. Tweets are written in JSON format, with every
tweet being stored in a separate line as a Python dict.
text str, the text to search for in Twitter. This can
be a plain text string or a '#hashtag' to look
for tweets of this topic only.
max_pages int, maximum number of result 'pages' to obtain
from Twitter's backlog of archived tweets. When
not specified, default to 10 pages.
results_per_page int, maximum number of results per page to fetch
from Twitter's backlog of archived tweets. When
not specified, default to 100 tweets per page.
Returns:
None
"""
# Load the id of already seen tweets, if there are any.
seen= self.preload_tweets()
# global self.max_id
# self.out_file_name = ofile or 'standard output'
# seen = ofile+'.ids' and preload_tweets(ofile+'.ids') or set()
# from_date=FROM_DATE
if seen:
print '%d tweets preloaded from %s' %(len(seen), self.out_file_name_ids)
file_seen=open(self.out_file_name_ids,'a+')
file_json=open(self.out_file_name,'a+')
u=0
try:
# file_json = ofile and file(ofile, 'a+') or sys.stdout
# fop=open( ofile+'.ids', 'a+')
# u=0
for matches in self.search():#,args.auth,args.auth_dict):
u+=1
newmatches = 0
uu=0
for tweet in matches:
# print uu
uu+=1
# print type(tweet)
# print dir(tweet)
# print dir(tweet.AsDict())
# print type(tweet.AsDict())
(tid, tuser, text, cr_at) = (tweet.AsDict()['id'] ,tweet.AsDict()['user'],
tweet.AsDict()['text'], tweet.AsDict()['created_at']) #['id'], ['from_user'] ['text']
# return tweet
# (tid, tuser, text, cr_at) = (tweet.GetId() ,tweet.GetUser(),
# tweet.GetText(), tweet.GetCreatedAt()) #['id'], ['from_user'] ['text']
tweet=tweet.AsDict()
# print tid,cr_at
if not tid in seen:
newmatches += 1
seen.add(tid)
# json.dumps()
print >> file_json, json.dumps(tweet)
print >> file_seen, json.dumps(tid)
if self.verb and newmatches > 0:
print '%d new tweets logged at %s' %(newmatches, self.out_file_name)
print u,len(matches),'aa',self.max_id,cr_at
self.set_max_id(tid)
file_json.close()
file_seen.close()
except IOError, e:
if file_json and file_json != sys.stdout:
file_json.close()
print 'Error writing at file "%s". %s' %(self.out_file_name, e)
print 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno)
# if __name__ == '__main__':
# parser = argparse.ArgumentParser(description=(
# 'Collect tweets matching a text pattern and store them'
# 'continuously in JSON-formatted lines of a local file.'))
# parser.add_argument('-o', '--output',nargs='?', metavar='Output FILE', type=str,
# default='Out_json', help='output folder name')
# parser.add_argument('-s', '--search',nargs='?', metavar='Search term', type=str,
# default=None, help='Search term')
# parser.add_argument('-c', '--auth_dict',nargs='?', metavar='credentials dict', type=str,
# default=None, help='credentials dictionary file name')
# parser.add_argument('-d', '--auth',nargs='?', metavar='credentials file', type=str,
# default=None, help='credentials file name')
# argsr = parser.parse_args()
# filedir=argsr.output
# try:
# os.stat(filedir)
# except:
# os.mkdir(filedir)
# filenam=argsr.search
# filename=''
# for l in filenam.split():
# filename+=l+'_'
# json_filename = filename[:-1]+'.json' # Where to store matching tweets
# lookup_text = unicode(argsr.search,'utf-8')# Text to search for
# outfile_name = os.path.join('%s' % filedir,json_filename)
# while True:
# try:
# streamsearch(outfile_name, lookup_text,argsr)#,maid_id=maid)
# except twitter.TwitterError, e:
# print 'Skipping HTTP error %s [...]' %str(e).split('\n')[0]
# time.sleep(900)
# pass