5 from django
.conf
import settings
8 logger
= logging
.getLogger(__name__
)
11 GET_SIZES_TEMPLATE
= 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key={api_key}&photo_id={photo_id}&format=json&nojsoncallback=1'
14 def get_photo_sizes(photo_id
):
15 """ Returns available sizes for the photo with the given ID
17 Returns a list of dicts containing the following keys
26 api_key
= settings
.FLICKR_API_KEY
27 url
= GET_SIZES_TEMPLATE
.format(api_key
=api_key
, photo_id
=photo_id
)
30 resp
= requests
.get(url
)
31 except requests
.exceptions
.RequestException
as e
:
32 logger
.warn('Retrieving Flickr photo sizes failed: %s', str(e
))
35 resp_obj
= resp
.json()
38 return resp_obj
['sizes']['size']
43 def get_photo_id(url
):
44 """ Returns the Photo ID for a Photo URL
46 >>> get_photo_id('https://farm9.staticflickr.com/8747/12346789012_bf1e234567_b.jpg')
49 >>> get_photo_id('https://www.flickr.com/photos/someuser/12345678901/')
55 'http://.*flickr.com/[^/]+/([^_]+)_.*',
56 'https://.*staticflickr.com/[^/]+/([^_]+)_.*',
57 'https?://.*flickr.com/photos/[^/]+/([^/]+).*',
60 for regex
in photo_id_re
:
61 match
= re
.match(regex
, url
)
66 def is_flickr_image(url
):
67 """ Returns True if the URL represents a Flickr images
69 >>> is_flickr_image('https://farm9.staticflickr.com/8747/12346789012_bf1e234567_b.jpg')
72 >>> is_flickr_image('http://www.example.com/podcast.mp3')
75 >>> is_flickr_image(None)
81 return bool(re
.search('flickr\.com.*\.(jpg|jpeg|png|gif)', url
))
83 def get_display_photo(url
, label
='Medium'):
84 photo_id
= get_photo_id(url
)
85 sizes
= get_photo_sizes(photo_id
)
87 if s
['label'] == label
: