-
Notifications
You must be signed in to change notification settings - Fork 807
/
Exporter.py
79 lines (56 loc) · 2 KB
/
Exporter.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
# -*- coding: utf-8 -*-
import sys,getopt,datetime,codecs
if sys.version_info[0] < 3:
import got
else:
import got3 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':
f = open('exporter_help_text.txt', 'r')
print f.read()
f.close()
return
try:
opts, args = getopt.getopt(argv, "", ("username=", "near=", "within=", "since=", "until=", "querysearch=", "toptweets", "maxtweets=", "output="))
tweetCriteria = got.manager.TweetCriteria()
outputFileName = "output_got.csv"
for opt,arg in opts:
if opt == '--username':
tweetCriteria.username = arg
elif opt == '--since':
tweetCriteria.since = arg
elif opt == '--until':
tweetCriteria.until = arg
elif opt == '--querysearch':
tweetCriteria.querySearch = arg
elif opt == '--toptweets':
tweetCriteria.topTweets = True
elif opt == '--maxtweets':
tweetCriteria.maxTweets = int(arg)
elif opt == '--near':
tweetCriteria.near = '"' + arg + '"'
elif opt == '--within':
tweetCriteria.within = '"' + arg + '"'
elif opt == '--within':
tweetCriteria.within = '"' + arg + '"'
elif opt == '--output':
outputFileName = arg
outputFile = codecs.open(outputFileName, "w+", "utf-8")
outputFile.write('username;date;retweets;favorites;text;geo;mentions;hashtags;id;permalink')
print('Searching...\n')
def receiveBuffer(tweets):
for t in tweets:
outputFile.write(('\n%s;%s;%d;%d;"%s";%s;%s;%s;"%s";%s' % (t.username, t.date.strftime("%Y-%m-%d %H:%M"), t.retweets, t.favorites, t.text, t.geo, t.mentions, t.hashtags, t.id, t.permalink)))
outputFile.flush()
print('More %d saved on file...\n' % len(tweets))
got.manager.TweetManager.getTweets(tweetCriteria, receiveBuffer)
except arg:
print('Arguments parser error, try -h' + arg)
finally:
outputFile.close()
print('Done. Output file generated "%s".' % outputFileName)
if __name__ == '__main__':
main(sys.argv[1:])