-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.py
272 lines (234 loc) · 7.39 KB
/
timeline.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# I find it hard to write clean looking code.
# If you think this is clean, please thank this Pretty Formatter https://pythoniter.appspot.com
# Import all tweets from a given list of authors
# Specify a file name containing one author id per row on the command line, with maxID tuple
# authorfile = main.txt
from twython import Twython
from twython.exceptions import TwythonRateLimitError, TwythonError
from time import gmtime, strftime, sleep
from os import path
from datetime import datetime
from bs4 import BeautifulSoup
import signal
import sys
import demjson
import json
import re
import requests
if len(sys.argv) != 3:
print("Usage:", sys.argv[0], "<authorFile> <keyFile>")
exit(1)
else:
if path.isfile(sys.argv[1]) and path.isfile(sys.argv[2]):
input_file_name = sys.argv[1]
keyfile= sys.argv[2]
print("Input file:", input_file_name)
else:
print("authorFile or keyFile not found")
exit(0)
current_time = strftime("%Y%m%d_%H%M%S", gmtime())
output_file_name = 'authors.output.' + input_file_name + "."+ current_time+".json"
output_file_name_authors_explored = 'authors_id_explored.output.' + input_file_name + "." + current_time
output_file = open(output_file_name, mode='a')
output_file_authors_explored = open(output_file_name_authors_explored, mode='a')
kf = open(keyfile, 'r+')
keys=kf.readlines()[1:]
kf.close()
(nKey, kCount) = (len(keys), 0) # Number of keys
twitter = False
input_file = open(input_file_name, mode='r')
nb = 0
def changeTwitterKey():
global kCount, twitter
key = keys[kCount].strip().split(',')
twitter = Twython(*key)
kCount += 1
if kCount == nKey:
kCount = 0
print(twitter)
def checkKeyReplacementTwitter():
rate_url ='https://api.twitter.com/1.1/application/rate_limit_status.json'
constructed_url = twitter.construct_api_url(rate_url,resources='search,statuses,application')
rate_result = twitter.request(constructed_url, method='GET')['resources']
f=open("rate.json","w+")
f.write(json.dumps(rate_result,indent=2))
f.close()
rem = min(rate_result['statuses']['/statuses/user_timeline']['remaining'],rate_result['application']['/application/rate_limit_status']['remaining'])
print("Remaining: ",str(rem))
if rem < 2:
print('Time to change')
changeTwitterKey()
def retrieveURL(tweet):
global filt
print("Filter: ",filt)
urls=re.findall(r'(https?://\S+)', tweet)
for url in urls:
for f in filt:
if f in url:
return url
return False
def getFSQData(url):
r=requests.get(url)
print(url)
bs=BeautifulSoup(r.text,"html.parser")
data=False
for line in bs.find_all('script',attrs={"type" : "text/javascript"}):
data=line.text if "SwarmCheckinDetail" in line.text[:100] else ""
if data:
try:
data=data.split("SwarmCheckinDetail.init")[1][1:-2].replace("$('body')", '"body"',1)
# Debugging Actions
temp=open("temp.json","w+")
data=demjson.decode(data)
temp.write(str(data))
temp.close()
# data=json.loads(data)
user,venue={"id":"","firstName":""},{"lng":"","lat":""}
for key in user: user[key]=data["checkin"]["user"][key]
for key in venue: venue[key]=data["venue"]["location"][key]
venue["id"]=data["venue"]["id"]
print(user)
print(venue)
return (user["id"],venue["id"])
except Exception as e:
print "Exception in getting checkin data: " + url
return (False,False)
# sys.exit(1)
else:
return (False, False)
def terminate(signal, frame):
print("Ctrl + C handler: close file!")
output_file.close()
output_file_authors_explored.close()
print("File closed with about " + str(nb) + " lines")
exit(0)
signal.signal(signal.SIGINT, terminate)
# INITIALIZE TWITTER
changeTwitterKey()
print(twitter)
nb_tweets = 0
x=input_file.readlines()
authors=[]
for i in x:
aut=i.strip()
authors.append(aut)
# print(authors)
# authors = input_file.readlines()
nb_authors = len(authors)
author_id_nb = 0
# print(authors)
dateFormat = "%a %b %d %H:%M:%S +0000 %Y"
filt=["swarmapp.com/","4sq.com/","foursquare.com/"]
fsqUserID="804341497441255424"
for author_id in authors:
# author_id = author_id[:-1]
try:
temp=author_id.split()
print "temp "+str(temp)
auth,sinceId=temp[0],int(temp[1])
print "Start: "+auth
last_id = -1
author_id_nb_tweets = 0
author_id_nb += 1
flag=True
ctr=0
while flag==True:
checkKeyReplacementTwitter()
if auth == fsqUserID:
# Foursquare's user ID. Just in case that shows up we don't need links from there as they might not
# flag=False
break
try:
if last_id == -1:
user_tweets = twitter.get_user_timeline(user_id=auth, include_rts='0', count=200,since_id=sinceId)
elif last_id>sinceId:
user_tweets = twitter.get_user_timeline(user_id=auth, include_rts='0', count=200, max_id=last_id-1)
else:
print "found all tweets"
break
ctr+=1
print("Try #"+str(ctr)+" Author_ID: "+str(auth))
except TwythonRateLimitError:
# print("TwythonRateLimitError: sleep 30 seconds...")
# sleep(30)
changeTwitterKey()
continue
except TwythonError as e:
print "TwythonError:", e.msg
if e.msg[2:21] == 'Connection aborted.':
print "Try to get your connection back. Or Ctrl + C to stop with", nb, "tweets from about", author_id_nb, "authors. Sleep 30 seconds..."
sleep(30)
continue
else:
print "Maybe Protected. Maybe a problem with author", auth
break
print "Number of tweets fetched" ,len(user_tweets)
if len(user_tweets)>0:
x=user_tweets[0]["id"]
y=user_tweets[-1]["id"]
print "Start ID: ",x,"End ID: ",y
if len(user_tweets) == 0:
print "Author", author_id, "(", author_id_nb, "/", nb_authors, ") :", author_id_nb_tweets, "tweets. Total:",nb, "tweets."
break
for tweet in user_tweets:
# print("WHAT")
last_id = tweet["id"]
last_date = tweet["created_at"]
date=datetime.strptime(tweet["created_at"],dateFormat)
nb += 1
author_id_nb_tweets += 1
# checkin=True
# if int(last_id)<=int(sinceId):
# break
# checkin=False
# log=open("log.log","a+")
# log.write(str(last_id)+"\n")
# log.close()
# for Filter in filt:
# if Filter in tweet["text"]:
# checkin=True
# break
# if checkin==False:
# print("checkin found")
# debug=open("debug.txt","a+")
# debug.write("Twitter ID: "+str(last_id)+"\n")
# debug.write("Date: "+str(last_date)+"\n")
# json.dump(tweet, debug,indent=2)
# debug.write("\n\n")
# debug.close()
# print(tweet)
# sys.exit(0)
# output_file.write(str(tweet))
print last_id,last_date
if tweet["entities"]["urls"]:
url=retrieveURL(tweet["entities"]["urls"][0]["expanded_url"])
else:
url=retrieveURL(tweet["text"])
print("url: ",url)
if url:
user,venue=getFSQData(url)
else:
continue
if (not user) or (not venue):
continue
json.dump(tweet, output_file,indent=2)
output_file.write('\n')
# checkins.write()
# url=tweet['entities']['urls'][0]['expanded_url']
print(last_date,url)
dup=[str(auth),str(last_id),str(user),str(venue),str(last_date),str(url)]
checkins=open("checkins.log","a+")
checkins.write(' '.join(dup))
checkins.write('\n')
checkins.close()
print("written to checkins!")
dup=[]
# print(output_file)
# print(author_id, last_id, last_date, nb)
output_file_authors_explored.write(author_id + '\n')
print("End: "+author_id)
except Exception as e:
print e,author_id
terminate(None, None)