summaryrefslogtreecommitdiff
path: root/searx/engines/blekko_images.py
blob: 2bae9c35ef8e98d93df766418239651bc21feb51 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Blekko (Images)
#
# @website     https://blekko.com
# @provide-api yes (inofficial)
#
# @using-api   yes
# @results     JSON
# @stable      yes
# @parse       url, title, img_src

from json import loads
from urllib import urlencode

# engine dependent config
categories = ['images']
paging = True

# search-url
base_url = 'https://blekko.com'
search_url = '/api/images?{query}&c={c}'


# do search-request
def request(query, params):
    c = (params['pageno'] - 1) * 48

    params['url'] = base_url +\
        search_url.format(query=urlencode({'q': query}),
                          c=c)

    if params['pageno'] != 1:
        params['url'] += '&page={pageno}'.format(pageno=(params['pageno']-1))

    return params


# get response from search-request
def response(resp):
    results = []

    search_results = loads(resp.text)

    # return empty array if there are no results
    if not search_results:
        return []

    for result in search_results:
        # append result
        results.append({'url': result['page_url'],
                        'title': result['title'],
                        'content': '',
                        'img_src': result['url'],
                        'template': 'images.html'})

    # return results
    return results