This repository was archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathutils.py
More file actions
253 lines (190 loc) · 7.76 KB
/
utils.py
File metadata and controls
253 lines (190 loc) · 7.76 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
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
import os, sys
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, "%s/libs" % BASE_DIR)
import time
import json
from datetime import datetime, timedelta
import httplib2
import logging.config
from config import Config
from apiclient.discovery import build
from apiclient.errors import HttpError
from gnippy import rules, searchclient
f = file("./config")
config = Config(f)
class Utils:
BQ_CLIENT = None
@staticmethod
def get_bq():
if Utils.BQ_CLIENT:
return Utils.BQ_CLIENT
BQ_CREDENTIALS = None
# If runing on Google stack, authenticate natively
if Utils.isGae():
from oauth2client import appengine
BQ_CREDENTIALS = appengine.AppAssertionCredentials(scope='https://www.googleapis.com/auth/bigquery')
else:
from oauth2client.client import SignedJwtAssertionCredentials
KEY = Utils.read_file(config.KEY_FILE)
BQ_CREDENTIALS = SignedJwtAssertionCredentials(config.SERVICE_ACCOUNT, KEY, 'https://www.googleapis.com/auth/bigquery')
BQ_HTTP = BQ_CREDENTIALS.authorize(httplib2.Http())
Utils.BQ_CLIENT = build('bigquery', 'v2', http=BQ_HTTP)
return Utils.BQ_CLIENT
@staticmethod
def isGae():
# http://stackoverflow.com/questions/1916579/in-python-how-can-i-test-if-im-in-google-app-engine-sdk
software = os.environ.get('SERVER_SOFTWARE', None)
return software and ("Google App Engine" in software or "Development" in software)
@staticmethod
def get_gnip():
g = searchclient.SearchClient(config.GNIP_SEARCH_USERNAME, config.GNIP_SEARCH_PASSWORD, config.GNIP_SEARCH_URL)
return g
@staticmethod
def insert_table(dataset_id, table_id, schema=None):
schema_file = None
if config.MODE == QueryBuilder.GNIP:
schema_file = "./schema/schema_gnip.json"
else:
schema_file = "./schema/schema_twitter.json"
schema_str = Utils.read_file(schema_file)
schema = json.loads(schema_str)
body = {
"tableReference" : {
"projectId" : config.PROJECT_ID,
"tableId" : table_id,
"datasetId" : dataset_id
},
"schema" : {
"fields" : schema
}
}
response = None
try:
response = Utils.get_bq().tables().insert(projectId=config.PROJECT_ID, datasetId=dataset_id, body=body).execute()
except HttpError, e:
# HttpError 409 when requesting URI returned
# "Already Exists: Table twitter-for-bigquery:gnip.tweets_nbafinals"
if e.resp.status == 409:
response = True
else:
raise e
return response
@staticmethod
def insert_records(dataset_id, table_id, tweets):
# ensure insertId to avoid duplicate records
body = {
"kind": "bigquery#tableDataInsertAllRequest",
"rows": [{ "insertId" : t["id"], "json" : Utils.scrub(t) } for t in tweets ]
}
response = Utils.get_bq().tabledata().insertAll(projectId=config.PROJECT_ID, datasetId=dataset_id, tableId=table_id, body=body).execute()
# print "insert_records: %s %s %s" % (config.PROJECT_ID, dataset_id, table_id)
# print response
return response
@staticmethod
def import_from_file(dataset_id, table_id, filename, single_tweet=False):
records = []
if single_tweet:
records = [json.loads(Utils.read_file(SAMPLE_TWEET_FILE))]
success = Utils.insert_records(dataset_id, table_id, records)
return success
else:
with open(filename, "r") as f:
records = [Utils.scrub(json.loads(tweet)) for tweet in f if json.loads(tweet).get("delete", None) == None]
success = Utils.insert_records(dataset_id, table_id, [record])
@staticmethod
def get_config(config_file):
props = {}
for name in (name for name in dir(config) if not name.startswith('_')):
props[name] = getattr(config, name, '')
return props
@staticmethod
def enable_logging():
root = None
if Utils.isGae():
logging.getLogger().setLevel(logging.DEBUG)
else:
path = "./logging.conf"
logging.config.fileConfig(path)
root = logging.getLogger("root")
return root
@staticmethod
# BUGBUG: aim to NOT scrub results
def scrub(d):
# d.iteritems isn't used as you can't del or the iterator breaks.
for key, value in d.items():
if value is None:
del d[key]
# maintain geo with lat/long
elif key == 'geo':
geo = d.get('geo', None)
if geo:
type = geo.get('type', None)
coordinates = geo.get('coordinates', None)
del geo['coordinates']
if type == 'Point':
geo['lat'] = coordinates[0]
geo['long'] = coordinates[1]
elif type == 'Polygon':
geo['lat'] = sum(pair[1] for pair in coordinates[0]) / 4
geo['long'] = sum(pair[0] for pair in coordinates[0]) / 4
# print "\t\t\t FOUND", geo
d['geo'] = geo
# remove other coordinates that are 4-point bounding boxes
elif key == 'coordinates':
del d[key]
elif key == 'bounding_box': # in 'place' object
del d[key]
elif key == 'attributes': # in 'place' object
del d[key]
elif key == 'retweeted_status':
del d[key]
elif key == 'created_at':
d[key] = Utils.convert_timestamp(value)
elif isinstance(value, dict):
Utils.scrub(value)
return d # For convenience
@staticmethod
def read_file(fn):
data = ""
with open(fn, "r") as f:
for line in f:
data = data + line
return data
@staticmethod
def convert_timestamp(str):
ts = time.strptime(str,'%a %b %d %H:%M:%S +0000 %Y')
ts = time.strftime('%Y-%m-%d %H:%M:%S', ts)
return ts
@staticmethod
def millis_to_date(ts):
return datetime.fromtimestamp(ts/1000)
@staticmethod
def millis_to_str(ts, format='%Y-%m-%d %H:%M'):
return Utils.millis_to_date(ts).strftime(format)
@staticmethod
def parse_bqid(id):
if id:
import re
return re.split('\:|\.', id)
return None
@staticmethod
def make_tag(dataset, table):
return "%s.%s" % (dataset, table)
# main() generates a schema from a tweet. It requires the following
# library to work, and is not included in this TwitterDev package
# https://github.com/tylertreat/BigQuery-Python
# def main():
#
# from bigquery import schema_from_record
#
# tweet_str = Utils.read_file("data/sample_tweet_powertrack.json")
# record = json.loads(record_str)
# schema = schema_from_record(record)
# schema = json.dumps(schema)
# print schema
#
# with open('data/schema.json', 'wt') as out:
# res = json.dump(schema, out, sort_keys=False, indent=4, separators=(',', ': '))
#
# if __name__ == "__main__":
# main()