Ponysearch/searx/engines/flickr.py

49 lines
2.1 KiB
Python
Raw Normal View History

2013-10-18 02:15:26 +02:00
#!/usr/bin/env python
2013-10-23 23:55:37 +02:00
from urllib import urlencode
2014-05-16 18:07:34 +02:00
from json import loads
#from urlparse import urljoin
2013-10-18 02:15:26 +02:00
2013-10-18 09:35:29 +02:00
categories = ['images']
2013-10-18 02:15:26 +02:00
2014-05-16 18:07:34 +02:00
# url = 'https://secure.flickr.com/'
# search_url = url+'search/?{query}&page={page}'
# results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]' # noqa
2014-01-20 02:31:20 +01:00
2014-01-30 01:19:51 +01:00
paging = True
2014-05-16 18:07:34 +02:00
# text=[query]
# TODO clean "extras"
search_url = 'https://api.flickr.com/services/rest?extras=can_addmeta%2Ccan_comment%2Ccan_download%2Ccan_share%2Ccontact%2Ccount_comments%2Ccount_faves%2Ccount_notes%2Cdate_taken%2Cdate_upload%2Cdescription%2Cicon_urls_deep%2Cisfavorite%2Cispro%2Clicense%2Cmedia%2Cneeds_interstitial%2Cowner_name%2Cowner_datecreate%2Cpath_alias%2Crealname%2Csafety_level%2Csecret_k%2Csecret_h%2Curl_c%2Curl_h%2Curl_k%2Curl_l%2Curl_m%2Curl_n%2Curl_o%2Curl_q%2Curl_s%2Curl_sq%2Curl_t%2Curl_z%2Cvisibility&per_page=50&page={page}&{query}&sort=relevance&method=flickr.photos.search&api_key=ad11b34c341305471e3c410a02e671d0&format=json' # noqa
2013-10-18 02:15:26 +02:00
def request(query, params):
2014-05-16 18:07:34 +02:00
params['url'] = search_url.format(query=urlencode({'text': query}),
2014-01-30 01:19:51 +01:00
page=params['pageno'])
2014-05-16 18:07:34 +02:00
#params['url'] = search_url.format(query=urlencode({'q': query}),
# page=params['pageno'])
2013-10-18 02:15:26 +02:00
return params
2014-01-20 02:31:20 +01:00
2013-10-18 02:15:26 +02:00
def response(resp):
results = []
2014-05-16 18:07:34 +02:00
images = loads(resp.text[14:-1])["photos"]["photo"]
for i in images:
results.append({'url': i['url_s'],
'title': i['title'],
'img_src': i['url_s'],
2014-01-20 02:31:20 +01:00
'template': 'images.html'})
2014-05-16 18:07:34 +02:00
#dom = html.fromstring(resp.text)
#for result in dom.xpath(results_xpath):
# href = urljoin(url, result.attrib.get('href'))
# img = result.xpath('.//img')[0]
# title = img.attrib.get('alt', '')
# img_src = img.attrib.get('data-defer-src')
# if not img_src:
# continue
# results.append({'url': href,
# 'title': title,
# 'img_src': img_src,
# 'template': 'images.html'})
2013-10-18 02:15:26 +02:00
return results