-
Notifications
You must be signed in to change notification settings - Fork 9
/
flickr.py
53 lines (45 loc) · 1.5 KB
/
flickr.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
from flickrapi import FlickrAPI
KEY = 'KEY'
SECRET = 'SECRET'
# List of sizes:
# url_o: Original (4520 × 3229)
# url_k: Large 2048 (2048 × 1463)
# url_h: Large 1600 (1600 × 1143)
# url_l=: Large 1024 (1024 × 732)
# url_c: Medium 800 (800 × 572)
# url_z: Medium 640 (640 × 457)
# url_m: Medium 500 (500 × 357)
# url_n: Small 320 (320 × 229)
# url_s: Small 240 (240 × 171)
# url_t: Thumbnail (100 × 71)
# url_q: Square 150 (150 × 150)
# url_sq: Square 75 (75 × 75)
SIZES = ["url_o", "url_k", "url_h", "url_l", "url_c"] # order of preference
def get_photos(image_tag):
extras = ','.join(SIZES)
flickr = FlickrAPI(KEY, SECRET)
photos = flickr.walk(text=image_tag,
extras=extras, # get the url for the original size image
privacy_filter=1, # search only for public photos
per_page=50,
sort='relevance')
return photos
def get_url(photo):
for i in range(len(SIZES)):
url = photo.get(SIZES[i])
if url: # if url is None try with the next size
return url
def get_urls(image_tag, max):
photos = get_photos(image_tag)
counter=0
urls=[]
for photo in photos:
if counter < max:
url = get_url(photo) # get preffered size url
if url:
urls.append(url)
counter += 1
# if no url for the desired sizes then try with the next photo
else:
break
return urls