on master help sign in
[fix] flickr engine code cleanup ++ handle missing owner
b975418Adam Tauber
searx/engines/flickr-noapi.py+32−45
@@ -1,10 +1,10 @@
1#!/usr/bin/env python
1#!/usr/bin/env python
2
2
3−## Flickr (Images)
3+# Flickr (Images)
4−#
4+#
5# @website https://www.flickr.com
5# @website https://www.flickr.com
6−# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
6+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
7−#
7+#
8# @using-api no
8# @using-api no
9# @results HTML
9# @results HTML
10# @stable no
10# @stable no
@@ -12,8 +12,6 @@
12
12
13from urllib import urlencode
13from urllib import urlencode
14from json import loads
14from json import loads
15−from urlparse import urljoin
16−from lxml import html
17import re
15import re
18
16
19categories = ['images']
17categories = ['images']
@@ -22,11 +20,13 @@ url = 'https://secure.flickr.com/'
22search_url = url+'search/?{query}&page={page}'
20search_url = url+'search/?{query}&page={page}'
23photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
21photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
24regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
22regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
23+image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'n', 'm', 't', 'q', 's')
25
24
26paging = True
25paging = True
27
26
27+
28def build_flickr_url(user_id, photo_id):
28def 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)
30
30
31
31
32def request(query, params):
32def request(query, params):
@@ -37,58 +37,45 @@ def request(query, params):
37
37
38def response(resp):
38def response(resp):
39 results = []
39 results = []
40−
40+
41 matches = regex.search(resp.text)
41 matches = regex.search(resp.text)
42−
42+
43− if matches == None:
43+ if matches is None:
44 return results
44 return results
45
45
46 match = matches.group(1)
46 match = matches.group(1)
47 search_results = loads(match)
47 search_results = loads(match)
48−
48+
49− if not '_data' in search_results:
49+ if '_data' not in search_results:
50 return []
50 return []
51−
51+
52 photos = search_results['_data']
52 photos = search_results['_data']
53−
53+
54 for photo in photos:
54 for photo in photos:
55−
55+
56 # In paged configuration, the first pages' photos are represented by a None object
56 # In paged configuration, the first pages' photos are represented by a None object
57− if photo == None:
57+ if photo is None:
58 continue
58 continue
59−
59+
60+ img_src = None
60 # From the biggest to the lowest format
61 # From the biggest to the lowest format
61− if 'o' in photo['sizes']:
62+ for image_size in image_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']:
63+ if image_size in photo['sizes']:
68− img_src = photo['sizes']['b']['displayUrl']
64+ img_src = photo['sizes'][image_size]['displayUrl']
69− elif 'c' in photo['sizes']:
65+ break
70− img_src = photo['sizes']['c']['displayUrl']
66+
71− elif 'z' in photo['sizes']:
67+ if not img_src:
72− img_src = photo['sizes']['z']['displayUrl']
68+ continue
73− elif 'n' in photo['sizes']:
69+
74− img_src = photo['sizes']['n']['displayUrl']
70+ if 'id' not in photo['owner']:
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:
84 continue
71 continue
85−
72+
86 url = build_flickr_url(photo['owner']['id'], photo['id'])
73 url = build_flickr_url(photo['owner']['id'], photo['id'])
87
74
88 title = photo['title']
75 title = photo['title']
89−
76+
90− content = '<span class="photo-author">'+ photo['owner']['username'] +'</span><br />'
77+ content = '<span class="photo-author">' + photo['owner']['username'] + '</span><br />'
91−
78+
92 if 'description' in photo:
79 if 'description' in photo:
93 content = content + '<span class="description">' + photo['description'] + '</span>'
80 content = content + '<span class="description">' + photo['description'] + '</span>'
94
81
@@ -98,5 +85,5 @@ def response(resp):
98 'img_src': img_src,
85 'img_src': img_src,
99 'content': content,
86 'content': content,
100 'template': 'images.html'})
87 'template': 'images.html'})
101−
88+
102 return results
89 return results