forked from nprapps/tshirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo.py
executable file
·62 lines (49 loc) · 1.96 KB
/
photo.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
#!/usr/bin/env python
import csv
import json
import requests
FIELDNAMES = ['date', 'name', 'caption', 'url', 'preview', 'remote_img_url', 'moderated']
class Photo(object):
def __init__(self, **kwargs):
for field in FIELDNAMES:
if kwargs[field]:
setattr(self, field, kwargs[field])
setattr(
self,
'local_img_id',
self.remote_img_url\
.split(u's3.amazonaws.com/')[1]\
.replace('_8.jpg', '.jpg'))
setattr(
self,
'local_img_url',
'/tshirt/img/s2s-instagram-%s' % self.remote_img_url\
.split(u's3.amazonaws.com/')[1]\
.replace('_8.jpg', '.jpg'))
def get_photo_csv():
r = requests.get('https://docs.google.com/spreadsheet/pub?key=0Aga89cI2jWk5dFdqZmp0YXQ5RDBNWlNaTV90cXFYQWc&output=csv')
with open('data/photos.csv', 'wb') as writefile:
writefile.write(r.content)
def parse_photo_csv():
with open('data/photos.csv', 'rb') as readfile:
photos = list(csv.DictReader(readfile, fieldnames=FIELDNAMES))
print "Processing %s entries from Google Docs." % len(photos)
payload = []
for photo in photos:
if photo['moderated'] != '':
r = requests.get(photo['remote_img_url'])
if r.status_code == 200:
p = Photo(**photo)
payload.append(p.__dict__)
with open('www/img/s2s-instagram-%s' % p.local_img_id, 'wb') as writefile:
writefile.write(r.content)
else:
print "! Error: Photo %s does not have an image file." % (photo['url'])
else:
print "* Unmoderated: Photo %s has been unmoderated." % (photo['url'])
print "Writing %s photos to JSON." % len(payload)
with open('www/data/photos.json', 'wb') as writefile:
writefile.write(json.dumps(payload))
if __name__ == "__main__":
get_photo_csv()
parse_photo_csv()