5 from django
.conf
import settings
9 logger
= logging
.getLogger(__name__
)
12 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"
15 def get_photo_sizes(photo_id
):
16 """Returns available sizes for the photo with the given ID
18 Returns a list of dicts containing the following keys
27 api_key
= settings
.FLICKR_API_KEY
28 url
= GET_SIZES_TEMPLATE
.format(api_key
=api_key
, photo_id
=photo_id
)
31 resp
= requests
.get(url
)
32 except requests
.exceptions
.RequestException
as e
:
33 logger
.warning("Retrieving Flickr photo sizes failed: %s", str(e
))
37 resp_obj
= resp
.json()
38 except json
.JSONDecodeError
as jde
:
42 return resp_obj
["sizes"]["size"]
47 def get_photo_id(url
):
48 """Returns the Photo ID for a Photo URL
50 >>> get_photo_id('https://farm9.staticflickr.com/8747/12346789012_bf1e234567_b.jpg')
53 >>> get_photo_id('https://www.flickr.com/photos/someuser/12345678901/')
59 "http://.*flickr.com/[^/]+/([^_]+)_.*",
60 "https://.*staticflickr.com/[^/]+/([^_]+)_.*",
61 "https?://.*flickr.com/photos/[^/]+/([^/]+).*",
64 for regex
in photo_id_re
:
65 match
= re
.match(regex
, url
)
70 def is_flickr_image(url
):
71 """Returns True if the URL represents a Flickr images
73 >>> is_flickr_image('https://farm9.staticflickr.com/8747/12346789012_bf1e234567_b.jpg')
76 >>> is_flickr_image('http://www.example.com/podcast.mp3')
79 >>> is_flickr_image(None)
85 return bool(re
.search(r
"flickr\.com.*\.(jpg|jpeg|png|gif)", url
))
88 def get_display_photo(url
, label
="Medium"):
89 photo_id
= get_photo_id(url
)
90 sizes
= get_photo_sizes(photo_id
)
92 if s
["label"] == label
: