-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·66 lines (54 loc) · 1.7 KB
/
main.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
#!/usr/bin/env python
import traceback
import json
import sys
from flickrapi import FlickrAPI, shorturl
import scraperwiki
from collections import OrderedDict
API_KEY = "8812d02940ff6669b30904b807ecc49b"
flickr = FlickrAPI(API_KEY)
UNIQUE_KEYS = ['id']
class InvalidArgumentError(Exception):
pass
def main():
try:
if len(sys.argv) != 2:
raise InvalidArgumentError("Please supply a single argument. An example would be 'kittens'")
else:
search_flickr(sys.argv[1])
except Exception, e:
scraperwiki.status('error', type(e).__name__)
print json.dumps({
'error': {
'type': type(e).__name__,
'message': str(e),
'trace': traceback.format_exc()
}
})
else:
scraperwiki.status('ok')
print json.dumps({
'success': {
'type': 'ok',
'message': "Saved Flickr photo information"
}
})
def search_flickr(searchvalue):
favs = flickr.walk(tags=searchvalue, extras="geo")
rows = []
for i, photo in enumerate(favs):
if photo.get('latitude') != '0':
row = OrderedDict()
row['id'] = photo.get('id')
row['title'] = photo.get('title')
row['latitude'] = float(photo.get('latitude'))
row['longitude'] = float(photo.get('longitude'))
row['url'] = shorturl.url(photo.get('id'))
rows.append(row)
if i < 10000:
break
submit_to_scraperwiki(rows, searchvalue)
def submit_to_scraperwiki(rows, tablename):
scraperwiki.sqlite.save(UNIQUE_KEYS, rows, tablename )
if __name__ == '__main__':
main()