summaryrefslogtreecommitdiff
path: root/searx/engines/stackexchange.py
diff options
context:
space:
mode:
authorAlexandre Flament <alex@al-f.net>2021-09-29 10:38:50 +0200
committerGitHub <noreply@github.com>2021-09-29 10:38:50 +0200
commita582cf3d8231f5ed8a881aa87576dfc0600e1c07 (patch)
treef3addfa011eb0ace1fefe7d39024c204fed02951 /searx/engines/stackexchange.py
parent7124fd17041c0a2dfc07bff7f3fe7de370b57a81 (diff)
parentecb3912bd000bddd10841775ecada538386818c5 (diff)
Merge pull request #353 from return42/stackexchange
[mod] engines - add Stack Exchange API v2.3
Diffstat (limited to 'searx/engines/stackexchange.py')
-rw-r--r--searx/engines/stackexchange.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/searx/engines/stackexchange.py b/searx/engines/stackexchange.py
new file mode 100644
index 000000000..34cba687c
--- /dev/null
+++ b/searx/engines/stackexchange.py
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+# lint: pylint
+"""Stack Exchange API v2.3
+
+* https://api.stackexchange.com/
+
+"""
+
+import html
+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': html.unescape(result['title']),
+ 'content': html.unescape(content),
+ })
+
+ return results