@@ -1,54 +1,83 @@
1#!/usr/bin/env python
1#!/usr/bin/env python
5+# @website https://www.flickr.com
6+# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
11+# @parse url, title, thumbnail, img_src
12+#More info on api-key : https://www.flickr.com/services/apps/create/
3from urllib import urlencode
14from urllib import urlencode
4−#from json import loads
15+from json import loads
5from urlparse import urljoin
16from urlparse import urljoin
6from lxml import html
17from lxml import html
7from time import time
18from time import time
9categories = ['images']
20categories = ['images']
11−url = 'https://secure.flickr.com/'
22+nb_per_page = 15
12−search_url = url+'search/?{query}&page={page}'
23+paging = True
13−results_xpath = '//div[@class="view display-item-tile"]/figure/div'
24+api_key= None
27+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}'
28+photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
15paging = True
30paging = True
32+def build_flickr_url(user_id, photo_id):
33+ return photo_url.format(userid=user_id,photoid=photo_id)
18def request(query, params):
36def request(query, params):
19− params['url'] = search_url.format(query=urlencode({'text': query}),
37+ params['url'] = url.format(text=urlencode({'text': query}),
39+ nb_per_page=nb_per_page,
20− page=params['pageno'])
40+ 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'
30 return params
41 return params
33def response(resp):
44def response(resp):
34 results = []
45 results = []
35− dom = html.fromstring(resp.text)
46+
36− for result in dom.xpath(results_xpath):
47+ search_results = loads(resp.text)
37− img = result.xpath('.//img')
39− if not img:
49+ # return empty array if there are no results
40− continue
50+ if not 'photos' in search_results:
53+ if not 'photo' in search_results['photos']:
42− img = img[0]
56+ photos = search_results['photos']['photo']
43− img_src = 'https:'+img.attrib.get('src')
61+ img_src = photo['url_o']
62+ elif 'url_z' in photo:
45− if not img_src:
63+ img_src = photo['url_z']
48− href = urljoin(url, result.xpath('.//a')[0].attrib.get('href'))
67+ url = build_flickr_url(photo['owner'], photo['id'])
49− title = img.attrib.get('alt', '')
68+
69+ title = photo['title']
71+ content = '<span class="photo-author">'+ photo['ownername'] +'</span><br />'
73+ content = content + ' <span class="description">' + photo['description']['_content'] + '</span>'
50− results.append({'url': href,
76+ results.append({'url': url,
51 'title': title,
77 'title': title,
52 'img_src': img_src,
78 'img_src': img_src,
53 'template': 'images.html'})
80 'template': 'images.html'})
54 return results
83 return results