searxng/searx/engines/flickr.py

29 lines
890 B
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/'
search_url = url+'search/?q={query}'
2013-10-18 00:15:26 +00:00
def request(query, params):
2013-10-23 21:55:37 +00:00
params['url'] = search_url.format(query=urlencode({'q': query}))
2013-10-18 00:15:26 +00:00
return params
def response(resp):
global base_url
results = []
dom = html.fromstring(resp.text)
2013-10-18 07:35:29 +00:00
for result in dom.xpath('//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]'):
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
2013-10-23 21:55:37 +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