-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweets_data.py
154 lines (122 loc) · 5.2 KB
/
tweets_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
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
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
def main(argv):
if len(argv) == 0:
print('You must pass some parameters. Use \"-h\" to help.')
return
if len(argv) == 1 and argv[0] == '-h':
print(__doc__)
return
try:
opts, args = getopt.getopt(argv, "", ("querysearch=",
"username=",
"usernames-from-file=",
"since=",
"until=",
"near=",
"within=",
"toptweets",
"maxtweets=",
"lang=",
"output=",
"debug"))
tweetCriteria = got.manager.TweetCriteria()
outputFileName = "/home/rrahul/Desktop/nepal.csv"
debug = False
usernames = set()
username_files = set()
for opt, arg in opts:
if opt == '--querysearch':
tweetCriteria.querySearch = arg
elif opt == '--username':
usernames_ = [u.lstrip('@') for u in re.split(r'[\s,]+', arg) if u]
usernames_ = [u.lower() for u in usernames_ if u]
usernames |= set(usernames_)
elif opt == '--usernames-from-file':
username_files.add(arg)
elif opt == '--since':
tweetCriteria.since = arg
elif opt == '--until':
tweetCriteria.until = arg
elif opt == '--near':
tweetCriteria.near = '"' + arg + '"'
elif opt == '--within':
tweetCriteria.within = arg
elif opt == '--toptweets':
tweetCriteria.topTweets = True
elif opt == '--maxtweets':
tweetCriteria.maxTweets = int(arg)
elif opt == '--lang':
tweetCriteria.lang = arg
elif opt == '--output':
outputFileName = arg
elif opt == '--debug':
debug = True
if debug:
print(' '.join(sys.argv))
print("GetOldTweets3", got.__version__)
if username_files:
for uf in username_files:
if not os.path.isfile(uf):
raise Exception("File not found: %s"%uf)
with open(uf) as f:
data = f.read()
data = re.sub('(?m)#.*?$', '', data) # remove comments
usernames_ = [u.lstrip('@') for u in re.split(r'[\s,]+', data) if u]
usernames_ = [u.lower() for u in usernames_ if u]
usernames |= set(usernames_)
print("Found %i usernames in %s" % (len(usernames_), uf))
if usernames:
if len(usernames) > 1:
tweetCriteria.username = usernames
if len(usernames)>20 and tweetCriteria.maxTweets > 0:
maxtweets_ = (len(usernames) // 20 + (len(usernames)%20>0)) * tweetCriteria.maxTweets
print("Warning: due to multiple username batches `maxtweets' set to %i" % maxtweets_)
else:
tweetCriteria.username = usernames.pop()
outputFile = open(outputFileName, "w+", encoding="utf8")
outputFile.write('date,username,to,replies,retweets,favorites,text,geo,mentions,hashtags,id,permalink\n')
cnt = 0
def receiveBuffer(tweets):
nonlocal cnt
for t in tweets:
data = [t.date.strftime("%Y-%m-%d %H:%M:%S"),
t.username,
t.to or '',
t.replies,
t.retweets,
t.favorites,
'"'+t.text.replace('"','""')+'"',
t.geo,
t.mentions,
t.hashtags,
t.id,
t.permalink]
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, receiveBuffer, debug=debug)
except getopt.GetoptError as err:
print('Arguments parser error, try -h')
print('\t' + str(err))
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)
if __name__ == '__main__':
main(sys.argv[1:])