summaryrefslogtreecommitdiff
path: root/searx/engines/stackexchange.py
blob: e4092c26feee2e18febba2f9c54e5494cbeb8e4c (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
57
58
59
60
61
62
63
64
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""Stack Exchange API v2.3

* https://api.stackexchange.com/

"""

from json import loads
from urllib.parse import urlencode

about = {
    "website": 'https://stackexchange.com',
    "wikidata_id": 'Q3495447',
    "official_api_documentation": 'https://api.stackexchange.com/docs',
    "use_official_api": True,
    "require_api_key": False,
    "results": 'JSON',
}

paging = True
pagesize = 10

api_site = 'stackoverflow'
api_sort= 'activity'
api_order = 'desc'

# https://api.stackexchange.com/docs/advanced-search
search_api = 'https://api.stackexchange.com/2.3/search/advanced?'

def request(query, params):

    args = urlencode({
        'q' : query,
        'page' : params['pageno'],
        'pagesize' : pagesize,
        'site' : api_site,
        'sort' : api_sort,
        'order': 'desc',
        })
    params['url'] = search_api + args

    return params

def response(resp):

    results = []
    json_data = loads(resp.text)

    for result in json_data['items']:

        content = "[%s]" % ", ".join(result['tags'])
        content += " %s" % result['owner']['display_name']
        if result['is_answered']:
            content += ' // is answered'
        content += " // score: %s" % result['score']

        results.append({
            'url': "https://%s.com/q/%s" % (api_site, result['question_id']),
            'title': result['title'],
            'content': content,
        })

    return results