qwant.py 4.28 KB
Newer Older
Cqoicebordel's avatar
Cqoicebordel committed
1
"""
Cqoicebordel's avatar
Cqoicebordel committed
2
 Qwant (Web, Images, News, Social)
Cqoicebordel's avatar
Cqoicebordel committed
3
4
5
6
7
8
9
10
11
12

 @website     https://qwant.com/
 @provide-api not officially (https://api.qwant.com/api/search/)

 @using-api   yes
 @results     JSON
 @stable      yes
 @parse       url, title, content
"""

Cqoicebordel's avatar
Cqoicebordel committed
13
from datetime import datetime
14
15
from json import loads
from searx.utils import html_to_text
Adam Tauber's avatar
Adam Tauber committed
16
from searx.url_utils import urlencode
17
from searx.utils import match_language
Cqoicebordel's avatar
Cqoicebordel committed
18
19

# engine dependent config
Cqoicebordel's avatar
Cqoicebordel committed
20
categories = None
Cqoicebordel's avatar
Cqoicebordel committed
21
22
paging = True
language_support = True
marc's avatar
marc committed
23
supported_languages_url = 'https://qwant.com/region'
Cqoicebordel's avatar
Cqoicebordel committed
24

25
26
27
28
category_to_keyword = {'general': 'web',
                       'images': 'images',
                       'news': 'news',
                       'social media': 'social'}
Cqoicebordel's avatar
Cqoicebordel committed
29

Cqoicebordel's avatar
Cqoicebordel committed
30
# search-url
Adam Tauber's avatar
Adam Tauber committed
31
url = 'https://api.qwant.com/api/search/{keyword}?count=10&offset={offset}&f=&{query}&t={keyword}&uiv=4'
Cqoicebordel's avatar
Cqoicebordel committed
32
33
34
35
36
37


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

38
39
40
41
42
43
44
45
46
    if categories[0] and categories[0] in category_to_keyword:

        params['url'] = url.format(keyword=category_to_keyword[categories[0]],
                                   query=urlencode({'q': query}),
                                   offset=offset)
    else:
        params['url'] = url.format(keyword='web',
                                   query=urlencode({'q': query}),
                                   offset=offset)
Cqoicebordel's avatar
Cqoicebordel committed
47

48
    # add language tag
49
    language = match_language(params['language'], supported_languages, language_aliases)
50
    params['url'] += '&locale=' + language.replace('-', '_').lower()
Cqoicebordel's avatar
Cqoicebordel committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

    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 'data' not in search_results:
        return []

    data = search_results.get('data', {})

    res = data.get('result', {})

    # parse results
    for result in res.get('items', {}):

72
        title = html_to_text(result['title'])
Cqoicebordel's avatar
Cqoicebordel committed
73
        res_url = result['url']
74
        content = html_to_text(result['desc'])
Cqoicebordel's avatar
Cqoicebordel committed
75

76
        if category_to_keyword.get(categories[0], '') == 'web':
Cqoicebordel's avatar
Cqoicebordel committed
77
78
79
80
            results.append({'title': title,
                            'content': content,
                            'url': res_url})

81
        elif category_to_keyword.get(categories[0], '') == 'images':
Cqoicebordel's avatar
Cqoicebordel committed
82
83
84
85
86
87
88
89
90
            thumbnail_src = result['thumbnail']
            img_src = result['media']
            results.append({'template': 'images.html',
                            'url': res_url,
                            'title': title,
                            'content': '',
                            'thumbnail_src': thumbnail_src,
                            'img_src': img_src})

91
        elif category_to_keyword.get(categories[0], '') == 'social':
Cqoicebordel's avatar
Cqoicebordel committed
92
            published_date = datetime.fromtimestamp(result['date'], None)
93
94
95
96
97
98
            img_src = result.get('img', None)
            results.append({'url': res_url,
                            'title': title,
                            'publishedDate': published_date,
                            'content': content,
                            'img_src': img_src})
Cqoicebordel's avatar
Cqoicebordel committed
99

100
101
102
103
104
105
106
        elif category_to_keyword.get(categories[0], '') == 'news':
            published_date = datetime.fromtimestamp(result['date'], None)
            media = result.get('media', [])
            if len(media) > 0:
                img_src = media[0].get('pict', {}).get('url', None)
            else:
                img_src = None
Cqoicebordel's avatar
Cqoicebordel committed
107
108
109
            results.append({'url': res_url,
                            'title': title,
                            'publishedDate': published_date,
110
111
                            'content': content,
                            'img_src': img_src})
Cqoicebordel's avatar
Cqoicebordel committed
112
113

    return results
marc's avatar
marc committed
114
115
116
117
118
119
120
121
122
123
124
125
126


# get supported languages from their site
def _fetch_supported_languages(resp):
    # list of regions is embedded in page as a js object
    response_text = resp.text
    response_text = response_text[response_text.find('regionalisation'):]
    response_text = response_text[response_text.find('{'):response_text.find(');')]

    regions_json = loads(response_text)

    supported_languages = []
    for lang in regions_json['languages'].values():
127
128
        if lang['code'] == 'nb':
            lang['code'] = 'no'
marc's avatar
marc committed
129
130
131
132
        for country in lang['countries']:
            supported_languages.append(lang['code'] + '-' + country)

    return supported_languages