.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
stackexchange.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Stack Exchange API v2.3
3
4* https://api.stackexchange.com/
5
6"""
7
8import html
9from json import loads
10from urllib.parse import urlencode
11
12about = {
13 "website": 'https://stackexchange.com',
14 "wikidata_id": 'Q3495447',
15 "official_api_documentation": 'https://api.stackexchange.com/docs',
16 "use_official_api": True,
17 "require_api_key": False,
18 "results": 'JSON',
19}
20
21paging = True
22pagesize = 10
23
24api_site = 'stackoverflow'
25api_sort = 'activity'
26api_order = 'desc'
27
28# https://api.stackexchange.com/docs/advanced-search
29search_api = 'https://api.stackexchange.com/2.3/search/advanced?'
30
31
32def request(query, params):
33
34 args = urlencode(
35 {
36 'q': query,
37 'page': params['pageno'],
38 'pagesize': pagesize,
39 'site': api_site,
40 'sort': api_sort,
41 'order': 'desc',
42 }
43 )
44 params['url'] = search_api + args
45
46 return params
47
48
49def response(resp):
50
51 results = []
52 json_data = loads(resp.text)
53
54 for result in json_data['items']:
55
56 content = "[%s]" % ", ".join(result['tags'])
57 content += " %s" % result['owner']['display_name']
58 if result['is_answered']:
59 content += ' // is answered'
60 content += " // score: %s" % result['score']
61
62 results.append(
63 {
64 'url': "https://%s.com/q/%s" % (api_site, result['question_id']),
65 'title': html.unescape(result['title']),
66 'content': html.unescape(content),
67 }
68 )
69
70 return results