This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflickr2wp_worker.py
61 lines (49 loc) · 1.91 KB
/
flickr2wp_worker.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
import os
import re
import sys
import flickrapi
from jinja2 import Template
API_KEY = '7fa8d2c3af054ab89adea2bd0220ae66'
SIZES = {'Square 75': 'url_s',
'Square 150': 'url_q',
'Thumbnail': 'url_t',
'Small 240': 'url_s',
'Small 320': 'url_n',
'Medium 500': 'url_m',
'Medium 640': 'url_z',
'Medium 800': 'url_c',
'Large 1024': 'url_l',
'Large 1600': 'url_h',
'Original': 'url_o'}
def main():
inp = sys.stdin.read()
sys.stderr.write(inp)
set_id = find_set_id(inp)
photos = get_set_photos(set_id)
sys.stdout.write(render_photos(photos))
def find_set_id(text):
m = re.search('http://www.flickr.com/photos/(?P<user>[\w@]+)/sets/(?P<set_id>\d+)/', text, re.M)
return m.groupdict().get('set_id')
def get_set_photos(set_id):
"""
Returns {'photo_title': 'title', 'href': 'http://...'} sequence.
"""
flickr = flickrapi.FlickrAPI(API_KEY)
photoset_photos = (flickr.photosets_getPhotos(
api_key=API_KEY, photoset_id=set_id,
extras=','.join((SIZES['Large 1024'], SIZES['Medium 640'], SIZES['Original']))
).getiterator('photo')) # Python 2.6 compatibility
# for photo in sorted(photoset_photos, key=lambda p: p.attrig.get('title')):
for photo in photoset_photos:
orig_url = photo.attrib.get(SIZES['Original'])
photo_url = photo.attrib.get(SIZES['Medium 640'], orig_url)
fullsize_url = photo.attrib.get(SIZES['Large 1024'], orig_url)
yield {'title': photo.attrib.get('title').replace('-', ' '),
'alt': photo.attrib.get('title').replace('-', ' '),
'href': photo_url, 'fullsize_url': fullsize_url}
def render_photos(photos):
with open(os.path.join(os.path.dirname(__file__), 'photoset-template.html'), 'r') as f:
template = Template(f.read())
return template.render(photos=photos)
if __name__ == '__main__':
main()