forked from Jefferson-Henrique/GetOldTweets-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exporter.py
72 lines (51 loc) · 2.24 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
# -*- coding: utf-8 -*-
import sys,getopt,got,datetime,codecs
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 """\nTo use this jar, you can pass the folowing attributes:
username: Username of a specific twitter account (without @)
since: The lower bound date (yyyy-mm-aa)
until: The upper bound date (yyyy-mm-aa)
querysearch: A query text to be matched
maxtweets: The maximum number of tweets to retrieve
\nExamples:
# Example 1 - Get tweets by username [barackobama]
python Export.py --username 'barackobama' --maxtweets 1\n
# Example 2 - Get tweets by query search [europe refugees]
python Export.py --querysearch 'europe refugees' --maxtweets 1\n
# Example 3 - Get tweets by username and bound dates [barackobama, '2015-09-10', '2015-09-12']
python Export.py --username 'barackobama' --since 2015-09-10 --until 2015-09-12 --maxtweets 1\n"""
return
try:
opts, args = getopt.getopt(argv, "", ("username=", "since=", "until=", "querysearch=", "maxtweets="))
tweetCriteria = got.manager.TweetCriteria()
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 == '--maxtweets':
tweetCriteria.maxTweets = int(arg)
outputFile = codecs.open("output_got.csv", "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 "output_got.csv".'
if __name__ == '__main__':
main(sys.argv[1:])