@@ -1,10 +1,10 @@
11#!/usr/bin/env python
22
3−## Flickr (Images)
4−#
3+# Flickr (Images)
4+#
55# @website https://www.flickr.com
6−# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
7−#
6+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
7+#
88# @using-api no
99# @results HTML
1010# @stable no
@@ -12,8 +12,6 @@
1212
1313from urllib import urlencode
1414from json import loads
15−from urlparse import urljoin
16−from lxml import html
1715import re
1816
1917categories = ['images']
@@ -22,11 +20,13 @@ url = 'https://secure.flickr.com/'
2220search_url = url+'search/?{query}&page={page}'
2321photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
2422regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
23+image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'n', 'm', 't', 'q', 's')
2524
2625paging = True
2726
27+
2828def build_flickr_url(user_id, photo_id):
29− return photo_url.format(userid=user_id,photoid=photo_id)
29+ return photo_url.format(userid=user_id, photoid=photo_id)
3030
3131
3232def request(query, params):
@@ -37,58 +37,45 @@ def request(query, params):
3737
3838def response(resp):
3939 results = []
40−
40+
4141 matches = regex.search(resp.text)
42−
43− if matches == None:
42+
43+ if matches is None:
4444 return results
4545
4646 match = matches.group(1)
4747 search_results = loads(match)
48−
49− if not '_data' in search_results:
48+
49+ if '_data' not in search_results:
5050 return []
51−
51+
5252 photos = search_results['_data']
53−
53+
5454 for photo in photos:
55−
55+
5656 # In paged configuration, the first pages' photos are represented by a None object
57− if photo == None:
57+ if photo is None:
5858 continue
59−
59+
60+ img_src = None
6061 # From the biggest to the lowest format
61− if 'o' in photo['sizes']:
62− img_src = photo['sizes']['o']['displayUrl']
63− elif 'k' in photo['sizes']:
64− img_src = photo['sizes']['k']['displayUrl']
65− elif 'h' in photo['sizes']:
66− img_src = photo['sizes']['h']['displayUrl']
67− elif 'b' in photo['sizes']:
68− img_src = photo['sizes']['b']['displayUrl']
69− elif 'c' in photo['sizes']:
70− img_src = photo['sizes']['c']['displayUrl']
71− elif 'z' in photo['sizes']:
72− img_src = photo['sizes']['z']['displayUrl']
73− elif 'n' in photo['sizes']:
74− img_src = photo['sizes']['n']['displayUrl']
75− elif 'm' in photo['sizes']:
76− img_src = photo['sizes']['m']['displayUrl']
77− elif 't' in photo['sizes']:
78− img_src = photo['sizes']['to']['displayUrl']
79− elif 'q' in photo['sizes']:
80− img_src = photo['sizes']['q']['displayUrl']
81− elif 's' in photo['sizes']:
82− img_src = photo['sizes']['s']['displayUrl']
83− else:
62+ for image_size in image_sizes:
63+ if image_size in photo['sizes']:
64+ img_src = photo['sizes'][image_size]['displayUrl']
65+ break
66+
67+ if not img_src:
68+ continue
69+
70+ if 'id' not in photo['owner']:
8471 continue
85−
72+
8673 url = build_flickr_url(photo['owner']['id'], photo['id'])
8774
8875 title = photo['title']
89−
90− content = '<span class="photo-author">'+ photo['owner']['username'] +'</span><br />'
91−
76+
77+ content = '<span class="photo-author">' + photo['owner']['username'] + '</span><br />'
78+
9279 if 'description' in photo:
9380 content = content + '<span class="description">' + photo['description'] + '</span>'
9481
@@ -98,5 +85,5 @@ def response(resp):
9885 'img_src': img_src,
9986 'content': content,
10087 'template': 'images.html'})
101−
88+
10289 return results