add flickr support
[mygpo.git] / mygpo / data / flickr.py
blob373d254adb05a1c2590093482513859c45d9ff8c
2 # This file is part of gpodder.net.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
19 import re
20 import json
21 import urllib
22 from mygpo import settings
24 def get_photo_sizes(photo_id):
25 api_key = settings.FLICKR_API_KEY
26 request = 'http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=%s&photo_id=%s&format=json' % (api_key, photo_id)
28 resp = urllib.urlopen(request).read()
30 extract_re = '^jsonFlickrApi\((.*)\)$'
31 resp_content = re.match(extract_re, resp).group(1)
33 resp_obj = json.loads(resp_content)
35 return resp_obj['sizes']['size']
37 def get_photo_id(url):
38 photo_id_re = 'http://.*flickr.com/[^/]+/([^_]+)_.*'
39 return re.match(photo_id_re, url).group(1)
42 def is_flickr_image(url):
43 return re.search('flickr\.com.*\.(jpg|jpeg|png|gif)', url)
45 def get_display_photo(url, label='Medium'):
46 photo_id = get_photo_id(url)
47 sizes = get_photo_sizes(photo_id)
48 for s in sizes:
49 if s['label'] == label:
50 return s['source']
52 return url