-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet_data.py
57 lines (47 loc) · 1.68 KB
/
tweet_data.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
import os, sys, re, getopt
import traceback
if sys.version_info[0] < 3:
raise Exception("Python 2.x is not supported. Please upgrade to 3.x")
import GetOldTweets3 as got
try:
tweetCriteria = got.manager.TweetCriteria()
outputFileName = "./tweet1.csv"
debug = False
tweetCriteria.querySearch = "#KeralaFloods"
tweetCriteria.since="2018-08-16"
tweetCriteria.until="2018-08-20"
tweetCriteria.maxTweets = 35000
tweetCriteria.lang = "en"
outputFile = open(outputFileName, "w+", encoding="utf8")
outputFile.write('date,username,retweets,text,mentions,hashtags\n')
cnt = 0
def recieveBuffer(tweets):
global cnt
for t in tweets:
if t.text != '':
data = [t.date.strftime("%Y-%m-%d %H:%M:%S"),
t.username,
t.retweets,
'"'+t.text.replace('"','""')+'"',
t.mentions,
t.hashtags]
data[:] = [i if isinstance(i, str) else str(i) for i in data]
outputFile.write(','.join(data) + '\n')
outputFile.flush()
cnt += len(tweets)
if sys.stdout.isatty():
print("\rSaved %i"%cnt, end='', flush=True)
else:
print(cnt, end=' ', flush=True)
print("Downloading tweets...")
got.manager.TweetManager.getTweets(tweetCriteria, recieveBuffer, debug=debug)
except KeyboardInterrupt:
print("\r\nInterrupted.\r\n")
except Exception as err:
print(traceback.format_exc())
print(str(err))
finally:
if "outputFile" in locals():
outputFile.close()
print()
print('Done. Output file generated "%s".' % outputFileName)