forked from matteoredaelli/dump_tweets.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_tweets.py
executable file
·291 lines (246 loc) · 9.75 KB
/
dump_tweets.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
#!/usr/bin/env python
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## AUTHOR: matteo DOT redaelli AT libero DOT it
## AUTHOR'S WEBSITE: http://www.redaelli.org/matteo/
## This file is part of the project dump_tweets.py, hosted at https://github.com/matteoredaelli/dump_tweets.py
__VERSION__="1.0-SNAPSHOT"
__AUTHORS__ = "Matteo Redaelli, matteo DOT redaelli AT libero DOT it"
__LICENSE__ = "GPLV3+"
#import argparse # valid for python >= 2.7
from datetime import datetime
import calendar
import getopt
import HTMLParser
import json
import os.path
import rfc822
import string
import sys
import time
import urllib
import urllib2
import MySQLdb
import re
def create_table(db_cursor, tablename):
sql_statement = """CREATE TABLE %s (
id varchar(30) NOT NULL,
PRIMARY KEY(id),
from_user varchar(30) NOT NULL,
timestamp int(11) NOT NULL,
text varchar(250) NOT NULL,
iso_language_code char(2) default NULL,
geo_lat double(10,6) default NULL,
geo_long double(10,6) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8""" % (tablename)
db_cursor.execute(sql_statement.encode('utf8'))
def drop_table(db_cursor, tablename):
sql_statement = """DROP TABLE %s""" % (tablename)
db_cursor.execute(sql_statement.encode('utf8'))
def my_exit(conn=False, db_cursor = False):
if db_cursor != False:
db_cursor.close()
if conn != False:
conn.close()
sys.exit(1)
def usage():
print
print "Software : %s" % __file__
print "Version : %s" % __VERSION__
print "License : %s" % __LICENSE__
print "Author(s) : %s" % __AUTHORS__
print
print "USAGE: %s -q|--query string [-f|--file filename] [-i|--since_id number] [-v|--verbose] [-s|--slip number] [-r|--result_type string] [-d|--db dbhost:dbport:dbuser:dbpass:dbtable] [-C|--create-table] [-D|--drop-table]" % __file__
print
print " -q string: string is the search string"
print " -d string: the tweets are saved to a MySQL db. strig is like 'dbhost:dbport:dbuser:dbpass:dbtable'"
print " -f string: max_id is saved to a file, it will be used for incremental dumping after a restart"
print " -r string: result_type"
print " -s number: the script runs forever, it retrives tweets each number seconds"
print
def clean_string(s):
s = HTMLParser.HTMLParser().unescape(s)
s = urllib.unquote(s)
s = s.strip()
return s
def dump_tweets(q, since_id=0, verbose=True, rpp=100, result_type = 'mixed', db_cursor=False, db_table=False):
base_url = "http://search.twitter.com/search.json"
query = "?" + urllib.urlencode({'q' : q,
'since_id' : since_id,
'rpp' : rpp,
'result_type' : result_type,
'page' : 1,
'include_entities': 1
})
max_id = counter = 0
for c in range(1, 15):
url = base_url + query
if verbose:
print >> sys.stderr, url
raw_response = urllib2.urlopen(url)
json_response = json.load(raw_response)
max_id = json_response["max_id"]
raw_response.close()
all_tweets = json_response["results"]
counter = counter + len(all_tweets)
for tweet in all_tweets:
print tweet
id = tweet["id"]
timestamp = calendar.timegm(rfc822.parsedate(tweet["created_at"]))
from_user = clean_string(tweet["from_user"])
##from_user_id = clean_string(tweet["from_user_id"])
text = clean_string(tweet["text"])
##iso_language_code = ""
iso_language_code = tweet["iso_language_code"]
## "geo":{"coordinates":[48.748530,2.448800],"type":"Point"}
if tweet["geo"]:
geo_lat = tweet["geo"]["coordinates"][0]
geo_long = tweet["geo"]["coordinates"][1]
else:
geo_lat = 0.0
geo_long = 0.0
row = str(id) + " : " + str(timestamp) + " : " + from_user + " : " + text + " : " + iso_language_code
print row.encode('utf8')
if db_cursor != False:
sql_statement = u"""insert into %s (id, from_user, timestamp, text, iso_language_code, geo_lat, geo_long) values (%d, '%s', %d, '%s', '%s', %f, %f)""" % (db_table, id, from_user, timestamp, text.replace("'","\\'"), iso_language_code, geo_lat, geo_long)
##print >> sys.stderr, sql_statement
try:
db_cursor.execute(sql_statement.encode('utf8'))
db_cursor.connection.commit()
except MySQLdb.Error, e:
print >> sys.stderr, "Error %d: %s" % (e.args[0], e.args[1])
print >> sys.stderr, "Skipping inserting this tweet to the DB"
##print json_response["next_page"]
if "next_page" in json_response.keys():
query = json_response["next_page"]
else:
break
result = dict()
result["max_id"] = max_id
result["counter"] = counter
return result
def rep_dump_tweets(q, since_id=0, sleep=3600, verbose=True, rpp=100, result_type = 'mixed', filename=False, db_cursor=False, db_table=False):
while True:
if verbose:
print >> sys.stderr, "since_id = " + str(since_id)
result = dump_tweets(q=q, since_id=since_id, verbose=verbose, db_cursor=db_cursor, db_table=db_table)
max_id = result["max_id"]
counter = result["counter"]
if verbose:
print >> sys.stderr, "retreived tweets = " + str(counter)
print >> sys.stderr, "max_id = " + str(max_id)
since_id = max_id
if filename != False:
id_to_file(since_id, filename)
if sleep == 0:
break
time.sleep(sleep)
def id_from_file(filename):
f = open(filename, 'r')
row = f.readline().strip()
f.close()
if row == "":
id = 0
else:
id = int(row)
return id
def id_to_file(id, filename):
f = open(filename, 'w')
f.write(str(id))
f.close()
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "CDd:f:hi:q:r:s:v",
["create-table", "drop-table", "db=", "file=", "help", "query=", "result_type=", "since_id=", "sleep=", "verbose"])
except getopt.GetoptError, err:
usage()
sys.exit(2)
q = ""
since_id = 0
result_type = "mixed"
sleep = 0
filename = tablename = action = False
verbose = False
db = conn = db_cursor = db_table = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-d", "--database"):
db = string.split(a,":")
elif o in ("-f", "--file"):
filename = a
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-i", "--since_id"):
since_id = int(a)
elif o in ("-q", "--query"):
q = a
elif o in ("-r", "--result_type"):
result_type = a
elif o in ("-s", "--sleep"):
sleep = int(a)
elif o in ("-C", "--create_table"):
action = "create"
elif o in ("-D", "--drop_table"):
action = "drop"
else:
assert False, "unhandled option"
db_cursor = False
db_table = False
if not (db == False):
if len(db) != 6:
print("-d option is wrong.")
usage()
db_host = db[0]
db_port = db[1]
db_name = db[2]
db_user = db[3]
db_pass = db[4]
db_table = db[5]
try:
conn = MySQLdb.connect (host = db_host,
user = db_user,
passwd = db_pass,
db = db_name,
charset = "utf8",
use_unicode=True)
db_cursor = conn.cursor()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
usage()
my_exit()
if action == "create":
create_table(db_cursor, db_table)
my_exit(conn=conn, db_cursor = db_cursor)
if action == "drop":
drop_table(db_cursor, db_table)
my_exit(conn=conn, db_cursor = db_cursor)
if q == "":
print("Required -q option is missing. No search text no party!")
usage()
my_exit(conn=conn, db_cursor = db_cursor)
if filename != False:
if os.path.isfile(filename):
if verbose:
print >> sys.stderr, "Reading since_id from file " + filename
since_id = id_from_file(filename)
rep_dump_tweets(q=q,
since_id=since_id,
sleep=sleep,
verbose=verbose,
result_type=result_type,
filename=filename,
db_cursor = db_cursor,
db_table=db_table)
my_exit(conn=conn, db_cursor = db_cursor)
if __name__ == "__main__":
main()