@@ -1,54 +1,80 @@
11#!/usr/bin/env python
22
3+## Flickr (Images)
4+#
5+# @website https://www.flickr.com
6+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
7+#
8+# @using-api yes
9+# @results JSON
10+# @stable yes
11+# @parse url, title, thumbnail, img_src
12+#More info on api-key : https://www.flickr.com/services/apps/create/
13+
314from urllib import urlencode
4−#from json import loads
5−from urlparse import urljoin
6−from lxml import html
7−from time import time
15+from json import loads
816
917categories = ['images']
1018
11−url = 'https://secure.flickr.com/'
12−search_url = url+'search/?{query}&page={page}'
13−results_xpath = '//div[@class="view display-item-tile"]/figure/div'
19+nb_per_page = 15
20+paging = True
21+api_key= None
22+
23+
24+url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={api_key}&{text}&sort=relevance&extras=description%2C+owner_name%2C+url_o%2C+url_z&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
25+photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
1426
1527paging = True
1628
29+def build_flickr_url(user_id, photo_id):
30+ return photo_url.format(userid=user_id,photoid=photo_id)
31+
1732
1833def request(query, params):
19− params['url'] = search_url.format(query=urlencode({'text': query}),
20− page=params['pageno'])
21− time_string = str(int(time())-3)
22− params['cookies']['BX'] = '3oqjr6d9nmpgl&b=3&s=dh'
23− params['cookies']['xb'] = '421409'
24− params['cookies']['localization'] = 'en-us'
25− params['cookies']['flrbp'] = time_string +\
26− '-3a8cdb85a427a33efda421fbda347b2eaf765a54'
27− params['cookies']['flrbs'] = time_string +\
28− '-ed142ae8765ee62c9ec92a9513665e0ee1ba6776'
29− params['cookies']['flrb'] = '9'
34+ params['url'] = url.format(text=urlencode({'text': query}),
35+ api_key=api_key,
36+ nb_per_page=nb_per_page,
37+ page=params['pageno'])
3038 return params
3139
3240
3341def response(resp):
3442 results = []
35− dom = html.fromstring(resp.text)
36− for result in dom.xpath(results_xpath):
37− img = result.xpath('.//img')
43+
44+ search_results = loads(resp.text)
3845
39− if not img:
40− continue
46+ # return empty array if there are no results
47+ if not 'photos' in search_results:
48+ return []
49+
50+ if not 'photo' in search_results['photos']:
51+ return []
4152
42− img = img[0]
43− img_src = 'https:'+img.attrib.get('src')
53+ photos = search_results['photos']['photo']
4454
45− if not img_src:
55+ # parse results
56+ for photo in photos:
57+ if 'url_o' in photo:
58+ img_src = photo['url_o']
59+ elif 'url_z' in photo:
60+ img_src = photo['url_z']
61+ else:
4662 continue
4763
48− href = urljoin(url, result.xpath('.//a')[0].attrib.get('href'))
49− title = img.attrib.get('alt', '')
50− results.append({'url': href,
64+ url = build_flickr_url(photo['owner'], photo['id'])
65+
66+ title = photo['title']
67+
68+ content = '<span class="photo-author">'+ photo['ownername'] +'</span><br />'
69+
70+ content = content + '<span class="description">' + photo['description']['_content'] + '</span>'
71+
72+ # append result
73+ results.append({'url': url,
5174 'title': title,
5275 'img_src': img_src,
76+ 'content': content,
5377 'template': 'images.html'})
78+
79+ # return results
5480 return results