-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetBTwDates2.py
executable file
·392 lines (323 loc) · 12.3 KB
/
tweetBTwDates2.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
388
389
390
391
392
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time, sys, datetime
import bs4, requests, re
import csv
import multiprocessing
from sortedcontainers import SortedDict
# Regex for tweet date
tweetNewDateRegex = re.compile(r'''(
\s? # separator
(?P<hour>\d{1,2}) # hour
: # : symbol
(?P<minute>\d{1,2}) # minute
\s{1} # separator
(?P<meridiem>\D{2}) # AM or PM
\s{1} # separator
- # - symbol
\s{1} # separator
(?P<day>\d{1,2}) # day
\s{1} # separator
(?P<month>\w{3,4}) # month
\s{1} # separator
(?P<year>\d{4}) # year
\s? # separator
)''', re.VERBOSE)
# Regex for tweet date
tweetDateRegex = re.compile(r'''(
\s? # separator
(?P<hour>\d{1,2}) # hour
: # : symbol
(?P<minute>\d{1,2}) # minute
\s{1} # separator
- # - symbol
\s{1} # separator
(?P<day>\d{1,2}) # day
\s{1} # separator
(?P<month>\w{3,4}) # month
\.{1} # . symbol
\s{1} # separator
(?P<year>\d{4}) # year
\s? # separator
)''', re.VERBOSE)
def monthtoNumber(month):
switcher = {
"ene": 1,
"feb": 2,
"mar": 3,
"abr": 4,
"may": 5,
"jun": 6,
"jul": 7,
"ago": 8,
"sept": 9,
"oct": 10,
"nov": 11,
"dic": 12
}
return switcher.get(month)
def NewMonthtoNumber(month):
switcher = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"May": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sept": 9,
"Oct": 10,
"Nov": 11,
"Dec": 12
}
return switcher.get(month)
# since: datetime object
# until : datetime object
# since : string
def getUrl(user, since, until):
since = since.strftime('%Y-%m-%d')
until = until.strftime('%Y-%m-%d')
p1 = 'https://twitter.com/search?f=tweets&vertical=default&q=from%3A'
p2 = user + '%20since%3A' + since + '%20until%3A' + until + '&src=typd'
return p1 + p2
# Return date's tweet given by url
def getTweetDate(urlTweet):
try:
res = requests.get(urlTweet)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, features = "html.parser")
dateElem = soup.select(".client-and-actions")
print("DateElem:")
print(dateElem)
dateText = dateElem[0].span.text
matchDate = re.match(tweetDateRegex, dateText)
newRegexUsed = False
if (matchDate is None):
print("Using new regex..")
newRegexUsed = True
matchDate = re.match(tweetNewDateRegex, dateText)
minute = int(matchDate.group("minute"))
hour = int(matchDate.group("hour"))
day = int(matchDate.group("day"))
year = int(matchDate.group("year"))
if (newRegexUsed):
print("Calculando nuevo mes...")
month = NewMonthtoNumber(matchDate.group("month"))
meridiem = str(matchDate.group("meridiem"))
if (meridiem == 'PM'):
print("Sumando 12...")
hour += 12
else:
month = monthtoNumber(matchDate.group("month"))
print("Year %d, month: %d, day: %d, hour: %d, minute: %d" % (year,month,day,hour,minute))
dt = datetime.datetime(year,month,day, hour, minute)
return dt
except requests.exceptions.RequestException as e:
print(e)
dt = datetime.datetime(1970,1,1,1,0,0)
return dt
# since: datetime object
# until : datetime object
# since : string
def InnerGetTweets(user, since, until, queue):
# Set Firefox headlessly to save resources
options = Options()
options.headless = False
# Open browser
browser = webdriver.Firefox(options=options)
# Get url to start
oneYear = datetime.timedelta(days=365)
url = getUrl(user, since, until)
browser.get(url)
# Get all tweets links
# Tweets found with selenium selector
tweets = []
# url's tweets stored
tweetLinks = set()
htmlElem = browser.find_element_by_tag_name('html')
endStream = False
lastRound = 0
MAX_COUNTER = 4
counter = 0
lastIteration = False
while not endStream or lastIteration:
# You can get random exception using a proxy, no idea why
try:
# Scroll down and update list
# All tweet links contains "status", find them with a CSS selector
tweets = browser.find_elements_by_css_selector("a[href*='status']")
"""
print("Current tweets stored: " + str(len(tweets)))
print("Total tweets found: " + str(len(tweets) + len(tweetLinks)))
"""
if(len(tweets) == 0):
print("0 tweets found, continue? ")
# It's not necessary, but we can use time.sleep to save cpu cycles
# while page is loading after scroll down
htmlElem.send_keys(Keys.END)
time.sleep(1)
#Check if number of tweets stored is the same as last iteration
currentRound = len(tweets)
if(currentRound == lastRound):
counter += 1
lastRound = currentRound
# Store tweets always
if(len(tweets) > 0):
tweets.pop()
# Store tweet links in a set structure (to avoid possible repeated tweets)
topLimit = len(tweets)
urlPrefix = "https://twitter.com/%s/status/" % user
for i in range(0, topLimit):
link = tweets[i].get_attribute("href")
# If tweet does not belong to the user, skip
# That's because mentioned tweet by the user are stored
if(not link.startswith(urlPrefix)):
continue
tweetLinks.add(link)
# If MAX_COUNTER consecutive itetarions storing the same number of tweets,
# load new twitter search until last Tweet
if(counter == MAX_COUNTER):
counter = 0
# Last element is status.twitter.com, pop it
# If len(tweets) is 0, no tweets remaining, just finish without
# saving tweets
if(len(tweets) > 0):
lastTweetUrl = tweets[len(tweets)-1].get_attribute("href")
# Load new tweet search to continue scrapping tweets
dateLastTweet = getTweetDate(lastTweetUrl)
# Finish because dateLast tweets is since's date or no tweets remaining
if ((len(tweets) == 0) or (dateLastTweet.date() == since.date())):
endStream = True
# One more iteration needed to store since's date tweets
lastIteration = True
# Last iteration finished, stop the loop
if(lastIteration):
break
# +1 day to include all tweets of that date
oneDay = datetime.timedelta(days=1)
until = getTweetDate(lastTweetUrl) + oneDay
newUrl = getUrl(user, since, dateLastTweet)
browserLoaded = False
attempts = 0
MAX_ATTEMPTS = 10
TIME_SLEEP = 120
TIMEOUT = 30
# After several experiments, sometimes you can get
# a timeout with selenium, catch it
while not browserLoaded:
attempts += 1
try:
browser.set_page_load_timeout(TIMEOUT)
browser.get(newUrl)
htmlElem = browser.find_element_by_tag_name('html')
browserLoaded = True
except TimeoutException as ex:
# Wait 2 minutes and try again.
time.sleep(TIME_SLEEP)
# If 5 times without success, stop trying it
if(attempts == MAX_ATTEMPTS):
break;
# If true, the timeout as not solved. Stop main loop
if not browserLoaded:
print("Timeout exception while loading page, no new tweets stored after that")
break;
except Exception as e:
print("Exception catched, saving tweets found...")
print(e)
break;
# Close browser
browser.close()
queue.put(tweetLinks)
#timeStripe: int, represent days
#timeoutDay : int, seconds limit to wait to get tweets
def getTweets(user, since, until, timeStripe, timeoutDay):
# +1 day to include tweets of until date
until = until + datetime.timedelta(days=1)
begin = int(since.timestamp() - 1)
# +1 to include until date
finish = int(until.timestamp() + 1)
inc = timeStripe * 24 * 3600
tweetLinks = set()
for i in range(begin, finish+1, inc):
since = datetime.datetime.fromtimestamp(i)
until = datetime.datetime.fromtimestamp(i+inc)
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=InnerGetTweets, args=(user, since, until ,queue,))
p.start()
timeout = int(timeoutDay * timeStripe)
p.join(timeout)
# If thread is active, kill it and break loop, if not, save tweets
if p.is_alive():
p.terminate()
p.join()
break
else:
auxSet = queue.get()
for item in auxSet:
tweetLinks.add(item)
return tweetLinks
# Receive a txt with tweet links and generates a csv file with tweets and dates, sorted
def TxtToCSVSorted(txtfile, csvfile):
# Store tweet links
with open(txtfile, 'r') as file:
tweets = file.readlines()
# Get dates and save in an OrderedDict
dictTweet = {}
date_errors = 0
for link in tweets:
# Delete \n at the end of the line
link = link[:-1]
print("Getting " + link)
tweetDate = getTweetDate(link)
print("Date:")
print(tweetDate)
if(tweetDate == datetime.datetime(1970,1,1,1,0,0)):
tweetDate += datetime.timedelta(days=date_errors)
date_errors += 1
timestamp = tweetDate.timestamp()
dictTweet[timestamp] = link
#sortedDictTweet = collections.OrderedDict(sorted(dictTweet.items()), key=lambda t: t[1])
outputFile = open(csvfile, 'w', newline='')
outputWriter = csv.writer(outputFile, delimiter = '\t')
sortedDictTweet = SortedDict(dictTweet)
for key,value in sortedDictTweet.items():
print("Writing " + str(key) +"--" + str(value))
date = datetime.datetime.fromtimestamp(key)
tweetDate = date.strftime('%d/%m/%Y')
outputWriter.writerow([value, tweetDate])
"""
#+1 day to include today's tweets
until = datetime.datetime.now() + datetime.timedelta(days=1)
#oneYear = datetime.timedelta(days=3)
since = datetime.datetime(2018,10,18)
links = getTweets("iMaxi98", since, until, 300, 60)
#getTweetDate("https://twitter.com/MiguelLentisco/status/817132635793264640")
print("Len of links: " + str(len(links)))
with open(sys.argv[1] + "_nosorted.txt", 'w') as file:
for link in links:
file.write(link + "\n")
"""
#TxtToCSVSorted("salidaPaisajes.txt_nosorted.txt", "paisajes_sorted.csv")
"""
# Save links sorted by date into a OrderedDictionary
dictTweet = {}
for link in links:
print("Saving " + link)
tweetDate = getTweetDate(link)
timestamp = tweetDate.timestamp()
dictTweet[timestamp] = link
sortedDictTweet = collections.OrderedDict(sorted(dictTweet.items()), key=lambda t: t[0])
outputFile = open(sys.argv[1] + "_ordered.csv", 'w', newline='')
outputWriter = csv.writer(outputFile, delimiter = '\t')
for key, value in sortedDictTweet.items():
print("Writing " + link)
date = datetime.fromtimestamp(key)
tweetDate = date.strftime('%d/%m/%Y')
outputWriter.writerow([value, tweetDate])
"""
#print("Done. All user's tweets copied to the given file.")