searxng/searx/engines/flickr.py

38 lines
1.1 KiB
Python
Raw Normal View History

2013-10-18 00:15:26 +00:00
#!/usr/bin/env python
2013-10-23 21:55:37 +00:00
from urllib import urlencode
2013-10-18 00:15:26 +00:00
from lxml import html
from urlparse import urljoin
2013-10-18 07:35:29 +00:00
categories = ['images']
2013-10-18 00:15:26 +00:00
2013-10-23 21:55:37 +00:00
url = 'https://secure.flickr.com/'
2014-01-30 00:19:51 +00:00
search_url = url+'search/?{query}&page={page}'
2014-01-20 01:31:20 +00:00
results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]' # noqa
2014-01-30 00:19:51 +00:00
paging = True
2013-10-18 00:15:26 +00:00
def request(query, params):
2014-01-30 00:19:51 +00:00
params['url'] = search_url.format(query=urlencode({'q': query}),
page=params['pageno'])
2013-10-18 00:15:26 +00:00
return params
2014-01-20 01:31:20 +00:00
2013-10-18 00:15:26 +00:00
def response(resp):
global base_url
results = []
dom = html.fromstring(resp.text)
2014-01-20 01:31:20 +00:00
for result in dom.xpath(results_xpath):
2013-10-23 21:55:37 +00:00
href = urljoin(url, result.attrib.get('href'))
2013-10-18 07:35:29 +00:00
img = result.xpath('.//img')[0]
title = img.attrib.get('alt', '')
img_src = img.attrib.get('data-defer-src')
if not img_src:
continue
2014-01-20 01:31:20 +00:00
results.append({'url': href,
'title': title,
'img_src': img_src,
'template': 'images.html'})
2013-10-18 00:15:26 +00:00
return results