-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
182 lines (172 loc) · 7.7 KB
/
database.py
File metadata and controls
182 lines (172 loc) · 7.7 KB
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
__author__="daveshepard"
__date__ ="$Jun 10, 2011 4:11:10 PM$"
import MySQLdb
import threading
import time
import simplejson
import logging
host = "localhost"
username = "root"
password = "GIS4ucla"
database = "hcnow"
TWITTER_TABLE = "tweets"
#connection = None
def get_connection():
connection = MySQLdb.connect(
host=host,user=username,passwd=password,db=database,
use_unicode=True,charset="utf8"
)
connection.set_character_set("utf8")
return connection
def get_cursor(conn=None):
if conn is not None:
return conn.cursor(MySQLdb.cursors.DictCursor)
return get_connection().cursor(MySQLdb.cursors.DictCursor)
class TweetStore(threading.Thread):
TWEET_TABLE = 'tweets'
def __init__(self, commit_threshold=10, interval=60, daemon=True):
self.commit_threshold = commit_threshold
self.tweets = []
self.interval = interval
self.logger = logging.getLogger('database')
def generator(key):
return lambda item: item[key]
self.field_correlations = {
'from_user_id': '',
'profile_image_url': None,
}
threading.Thread.__init__(self)
def log(self, message):
print "[Database] " + message
#self.logger.info({'query_url_id': 'Database', 'message': message})
#self.logger.info(message,extra={ 'query_url_id':'Database'})
# self.logger.info("[Database] " + message)
def add_tweets(self, items):
self.tweets.append(items)
def run(self):
self.log("Starting TweetStore")
time.sleep(10)
self.running = True
while self.running:
self.commit()
time.sleep(self.interval)
def stop(self):
self.running = False
self.log("Stopping tweetstore thread from top stop.")
def commit(self):
conn = get_connection()
for tweet_group in self.tweets:
url_id = tweet_group["url_id"]
self.log("Committing %s tweets for query_url_id %s" % (len(tweet_group["results"]), url_id))
for tweet in tweet_group["results"]:
fields = """query_url_id, from_user_id, profile_image_url, tweeted_at,
from_user, twitter_id, text, source, json"""
value_string = u"""%s, %s, %s, %s, %s, %s,%s, %s, %s"""
if "user" not in tweet:
tweet["user"] = {
"id_str" : "No ID",
"profile_image_url" : "nothing",
"screen_name": "userless tweet",
}
values = (
url_id, tweet["user"]['id_str'],
tweet["user"]['profile_image_url'],
time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(tweet['created_at'],
'%a %b %d %H:%M:%S +0000 %Y'
)),
tweet["user"]['screen_name'], tweet['id_str'],
tweet['text'],
tweet['source'], simplejson.dumps(tweet),
)
if "location" in tweet:
fields += ", location"
value_string += ", %s"
values += (tweet['location'], )
if "iso_language_code" in tweet["metadata"]:
fields += ", iso_language_code "
value_string += ", %s"
values += (tweet["metadata"]['iso_language_code'], )
if "geo" in tweet and tweet["geo"]:
fields += ", reported_lat, reported_lon, reported_geometry, reported_geometry_pt"
value_string += u", %s, %s, %s, GeomFromText(%s)"
values += (
tweet['geo']['coordinates'][1], tweet['geo']['coordinates'][0],
"GeomFromText('POINT(%s %s)')" % (tweet['geo']['coordinates'][1],
tweet['geo']['coordinates'][0]),
#"GeomFromText('POINT(%s %s)')" % (tweet['geo']['coordinates'][1],
"POINT(%s %s)" % (tweet['geo']['coordinates'][1],
tweet['geo']['coordinates'][0]),
)
if "in_reply_to_user_id_str" in tweet and tweet["in_reply_to_user_id_str"]:
fields += ", to_user_id"
value_string += ", %s"
values += (tweet['in_reply_to_user_id_str'], )
if "retweet_id" in tweet and tweet["retweet_id"]:
fields += ", retweet_id"
value_string += ", %s "
values += (tweet["retweet_id"], )
query = "INSERT INTO " + self.TWEET_TABLE + "(" + fields + ", created_at, updated_at) VALUES (" + value_string + ", NOW(), NOW())"
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query, values)
conn.commit()
self.log("Commit finished")
self.tweets = []
def commit_safe(self):
""" Saves tweets to database.
Attempts thread safety using get_tweet_group
"""
self.log("Doing safe commit")
conn = get_connection()
while True:
tweet_group = self.get_tweet_group()
if not tweet_group:
break
url_id = tweet_group["url_id"]
for tweet in tweet_group["results"]:
fields = """query_url_id, from_user_id, location, profile_image_url, tweeted_at,
from_user, twitter_id, text, iso_language_code, source"""
value_string = u"""%s, %s, %s, %s, %s, %s,%s, %s, %s, %s"""
values = (
url_id, tweet['from_user_id'], tweet['location'],
tweet['profile_image_url'],
time.strftime("%Y-%m-%d %H:%M:%S", time.strptime(tweet['created_at'],
"%a, %d %b %Y %H:%M:%S +0000")),
tweet['from_user'], tweet['id'],
tweet['text'],
tweet['iso_language_code'], tweet['source']
)
if "geo" in tweet and tweet["geo"]:
fields += ", reported_lat, reported_lon, reported_geometry_pt"
value_string += u", %s, %s, %s"
values += (
tweet['geo']['coordinates'][1], tweet['geo']['coordinates'][0],
"GeomFromText('POINT(%s,%s)')" % (tweet['geo']['coordinates'][1],
tweet['geo']['coordinates'][0]),
)
if "to_user_id" in tweet and tweet["to_user_id"]:
fields += ", to_user_id"
value_string += ", %s"
values += (tweet['to_user_id'], )
if "retweet_id" in tweet and tweet["retweet_id"]:
fields += ", retweet_id"
value_string += ", %s "
values += (tweet["retweet_id"], )
query = "INSERT INTO " + self.TWEET_TABLE + "(" + fields + ", created_at, updated_at) VALUES (" + value_string + ", NOW(), NOW())"
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute(query, values)
conn.commit()
def get_tweet_group(self):
""" Returns tweet group. Operates destructively on list.
"""
if len(self.tweets) > 0:
tweet_group = self.tweets[0]
self.tweets = self.tweets[1:]
return tweet_group
else:
return None
def stop(self):
if len(self.tweets) > 0:
self.commit()
self.running = False
# threading.Thread.stop()
self.log("Stopping tweetstore thread from lower stop.")