-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
294 lines (202 loc) · 10.1 KB
/
utils.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
291
292
293
294
import os
import cooperhewitt.api.client
import csv
import StringIO
import boto
from boto.s3.key import Key
import time
import datetime
import sendgrid
import dropbox
import re
from mongoengine import *
import urllib2 as urllib
MONGO_URL = os.environ.get("MONGOHQ_URL")
if MONGO_URL:
credentials = re.sub(r"(.*?)//(.*?)(@hatch)", r"\2",MONGO_URL)
username = credentials.split(":")[0]
password = credentials.split(":")[1]
connect(
MONGO_URL.split("/")[-1],
host=MONGO_URL,
port=1043,
username=username,
password=password
)
else:
database = os.environ['DB_NAME']
host = os.environ['DB_HOST']
connect(
database,
host=host,
port=27017
)
class Log(Document):
method = StringField(max_length=300)
completed_at = DateTimeField(default=datetime.datetime.now, required=True)
submitted_at = DateTimeField(default=datetime.datetime.now, required=True)
data = DynamicField()
class Role(Document):
name = StringField(max_length=80, unique=True)
description = StringField(max_length=255)
class User(Document):
email = StringField(max_length=255)
password = StringField(max_length=255)
active = BooleanField(default=True)
confirmed_at = DateTimeField()
roles = ListField(ReferenceField(Role), default=[])
access_token = os.environ['CH_API_KEY']
hostname = os.environ['CH_API_HOST']
s3_key = os.environ['S3_KEY']
s3_secret = os.environ['S3_SECRET']
s3_bucket = os.environ['S3_BUCKET']
sendgrid_password = os.environ['SENDGRID_PASSWORD']
sendgrid_username = os.environ['SENDGRID_USERNAME']
dropbox_access_token = os.environ['DROPBOX_ACCESS_TOKEN']
def search_objects(data):
query = data['meta']
api = cooperhewitt.api.client.OAuth2(access_token, hostname=hostname)
method = 'cooperhewitt.search.objects'
args = { 'query': query }
rsp = api.call(method, **args)
pages = int(rsp['pages'])
output = StringIO.StringIO()
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(['id', 'accession_number', 'creditline', 'date', 'decade', 'department_id', 'description', 'dimensions', 'inscribed', 'justification', 'markings', 'media_id', 'medium', 'period_id', 'provenance', 'signed', 'title', 'tms:id', 'type', 'type_id', 'url', 'woe:country', 'year_acquired', 'year_end', 'year_start', 'image url'])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
for x in range(0, pages):
args = { 'query': query, 'page': x }
rsp = api.call(method, **args)
objects = rsp['objects']
for obj in objects:
img_url = ''
for image in obj['images']:
if image['b']['is_primary'] == '1':
img_url = image['b']['url']
obj = utf8ify_dict(obj)
writer.writerow([obj['id'], obj['accession_number'], obj['creditline'], obj['date'], obj['decade'], obj['department_id'], obj['description'], obj['dimensions'], obj['inscribed'], obj['justification'], obj['markings'], obj['media_id'], obj['medium'], obj['period_id'], obj['provenance'], obj['signed'], obj['title'], obj['tms:id'], obj['type'], obj['type_id'], obj['url'], obj['woe:country'], obj['year_acquired'], obj['year_end'], obj['year_start'], img_url])
if img_url != '':
upload_dropbox(img_url, st)
upload_csv_dropbox(output, st)
dropbox_link = get_dropbox_share(st)
to_email = data['email'].encode('utf8')
send_email(to_email, dropbox_link)
data = {}
data['dropbox_link'] = dropbox_link
data['dropbox_folder'] = st
data['email'] = to_email
job = Log(method='search_objects', data=data).save()
success = "You just uploaded " + st + " to dropbox and emailed " + to_email + " about it."
return success
def random_objects(data):
numobjects = data['meta']
api = cooperhewitt.api.client.OAuth2(access_token, hostname=hostname)
method = 'cooperhewitt.objects.getRandom'
output = StringIO.StringIO()
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(['id', 'accession_number', 'creditline', 'date', 'decade', 'department_id', 'description', 'dimensions', 'inscribed', 'justification', 'markings', 'media_id', 'medium', 'period_id', 'provenance', 'signed', 'title', 'tms:id', 'type', 'type_id', 'url', 'woe:country', 'year_acquired', 'year_end', 'year_start', 'image url'])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
for x in range(0, int(numobjects)):
rsp = api.call(method)
obj = rsp['object']
img_url = ''
# for image in obj['images']:
# if image['b']['is_primary'] == '1':
# img_url = image['b']['url']
obj = utf8ify_dict(obj)
writer.writerow([obj['id'], obj['accession_number'], obj['creditline'], obj['date'], obj['decade'], obj['department_id'], obj['description'], obj['dimensions'], obj['inscribed'], obj['justification'], obj['markings'], obj['media_id'], obj['medium'], obj['period_id'], obj['provenance'], obj['signed'], obj['title'], obj['tms:id'], obj['type'], obj['type_id'], obj['url'], obj['woe:country'], obj['year_acquired'], obj['year_end'], obj['year_start'], img_url])
upload_csv_dropbox(output, st)
dropbox_link = get_dropbox_share(st)
to_email = data['email'].encode('utf8')
send_email(to_email, dropbox_link)
data = {}
data['dropbox_link'] = dropbox_link
data['dropbox_folder'] = st
data['email'] = to_email
job = Log(method='random_objects', data=data).save()
success = "You just uploaded " + st + " to dropbox and emailed " + to_email + " about it."
def list_objects(data):
obj_list = data['meta']
obj_list = obj_list.splitlines()
api = cooperhewitt.api.client.OAuth2(access_token, hostname=hostname)
method = 'cooperhewitt.objects.getInfo'
output = StringIO.StringIO()
writer = csv.writer(output, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
writer.writerow(['id', 'accession_number', 'creditline', 'date', 'decade', 'department_id', 'description', 'dimensions', 'inscribed', 'justification', 'markings', 'media_id', 'medium', 'period_id', 'provenance', 'signed', 'title', 'tms:id', 'type', 'type_id', 'url', 'woe:country', 'year_acquired', 'year_end', 'year_start', 'image url'])
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S')
for line in obj_list:
args = { 'accession_number': line }
rsp = api.call(method, **args)
if rsp['stat'] == 'ok':
obj = rsp['object']
img_url = ''
for image in obj['images']:
if image['b']['is_primary'] == '1':
img_url = image['b']['url']
obj = utf8ify_dict(obj)
writer.writerow([obj['id'], obj['accession_number'], obj['creditline'], obj['date'], obj['decade'], obj['department_id'], obj['description'], obj['dimensions'], obj['inscribed'], obj['justification'], obj['markings'], obj['media_id'], obj['medium'], obj['period_id'], obj['provenance'], obj['signed'], obj['title'], obj['tms:id'], obj['type'], obj['type_id'], obj['url'], obj['woe:country'], obj['year_acquired'], obj['year_end'], obj['year_start'], img_url])
if img_url != '':
upload_dropbox(img_url, st)
else:
writer.writerow(["Couldn't find a record for " + line])
upload_csv_dropbox(output, st)
dropbox_link = get_dropbox_share(st)
to_email = data['email'].encode('utf8')
send_email(to_email, dropbox_link)
data = {}
data['dropbox_link'] = dropbox_link
data['dropbox_folder'] = st
data['email'] = to_email
job = Log(method='list_objects', data=data).save()
success = "You just uploaded " + st + " to dropbox and emailed " + to_email + " about it."
return success
def upload_s3(filename, data):
# upload the csv data to S3
conn = boto.connect_s3(s3_key, s3_secret)
bucket = conn.create_bucket(s3_bucket)
k = Key(bucket)
k.key = filename # we need some kind of naming conv..
k.set_contents_from_string(data.getvalue())
k.make_public()
def upload_dropbox(url, jobname):
from cStringIO import StringIO
client = dropbox.client.DropboxClient(dropbox_access_token)
img_file = urllib.urlopen(url)
im = StringIO(img_file.read())
file_name = url.split('/')[-1]
file_name = jobname + '/' + file_name
response = client.put_file(file_name, im, overwrite=True)
def upload_csv_dropbox(data, jobname):
from cStringIO import StringIO
client = dropbox.client.DropboxClient(dropbox_access_token)
filename = jobname + "/" + jobname + "_" + "data.csv"
response = client.put_file(filename, data)
def get_dropbox_share(jobname):
from cStringIO import StringIO
client = dropbox.client.DropboxClient(dropbox_access_token)
rsp = client.share(jobname, short_url=True)
return rsp['url']
def send_email(to, filename):
# send an email? er... sumptin
sg = sendgrid.SendGridClient(sendgrid_username, sendgrid_password)
message = sendgrid.Mail()
body_html = 'Thanks for using csv-me. Here is a link to your stuff:<br><br> ' + filename
body_text = 'Thanks for using csv-me. Here is a link to your stuff:\n\n ' + filename
message.add_to(to)
message.set_subject('Yo, your file is ready!')
message.set_html(body_html)
message.set_text(body_text)
message.set_from('Micah Walter <[email protected]>') ## your webmaster
status, msg = sg.send(message)
def utf8ify_dict(stuff):
for k, v in stuff.items():
if v:
try:
v = v.encode('utf8')
except Exception, e:
v = ''
stuff[k] = v
return stuff