summaryrefslogtreecommitdiff
path: root/searx/engines/flickr.py
blob: d9554b99ac78a36e248082e88857b882d2ae9978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python

from urllib import urlencode
from lxml import html
from urlparse import urljoin

categories = ['images']

url = 'https://secure.flickr.com/'
search_url = url+'search/?{query}'
results_xpath = '//div[@id="thumbnails"]//a[@class="rapidnofollow photo-click" and @data-track="photo-click"]'  # noqa


def request(query, params):
    params['url'] = search_url.format(query=urlencode({'q': query}))
    return params


def response(resp):
    global base_url
    results = []
    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'})
    return results