summaryrefslogtreecommitdiff
path: root/searx
diff options
context:
space:
mode:
authorBnyro <bnyro@tutanota.com>2023-08-03 18:33:10 +0200
committerMarkus Heiser <markus.heiser@darmarIT.de>2023-09-01 18:39:24 +0200
commit9c4e9d381462c16e93ef74fd5133512f720a77e5 (patch)
tree93535e4f8dcc4c20fc99b5bd09e59b46eae35011 /searx
parentd92c6dbf6ce5df21869a84fada7bf8dc33ad289a (diff)
downloadsearxng-9c4e9d381462c16e93ef74fd5133512f720a77e5.tar.gz
searxng-9c4e9d381462c16e93ef74fd5133512f720a77e5.zip
[feat] implementation of Wikimedia commons for images
Diffstat (limited to 'searx')
-rw-r--r--searx/engines/wikicommons.py71
-rw-r--r--searx/settings.yml6
2 files changed, 77 insertions, 0 deletions
diff --git a/searx/engines/wikicommons.py b/searx/engines/wikicommons.py
new file mode 100644
index 000000000..47b2a5bea
--- /dev/null
+++ b/searx/engines/wikicommons.py
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: AGPL-3.0-or-later
+# lint: pylint
+"""Wikimedia Commons (images)
+
+"""
+
+from urllib.parse import urlencode
+
+# about
+about = {
+ "website": 'https://commons.wikimedia.org/',
+ "wikidata_id": 'Q565',
+ "official_api_documentation": 'https://commons.wikimedia.org/w/api.php',
+ "use_official_api": True,
+ "require_api_key": False,
+ "results": 'JSON',
+}
+
+base_url = "https://commons.wikimedia.org"
+search_prefix = (
+ '?action=query'
+ '&format=json'
+ '&generator=search'
+ '&gsrnamespace=6'
+ '&gsrprop=snippet'
+ '&prop=info|imageinfo'
+ '&iiprop=url|size|mime'
+ '&iiurlheight=180' # needed for the thumb url
+)
+paging = True
+number_of_results = 10
+
+
+def request(query, params):
+ language = 'en'
+ if params['language'] != 'all':
+ language = params['language'].split('-')[0]
+
+ args = {
+ 'uselang': language,
+ 'gsrlimit': number_of_results,
+ 'gsroffset': number_of_results * (params["pageno"] - 1),
+ 'gsrsearch': "filetype:bitmap|drawing " + query,
+ }
+
+ params["url"] = f"{base_url}/w/api.php{search_prefix}&{urlencode(args)}"
+ return params
+
+
+def response(resp):
+ results = []
+ json = resp.json()
+
+ if not json.get("query", {}).get("pages"):
+ return results
+
+ for item in json["query"]["pages"].values():
+ imageinfo = item["imageinfo"][0]
+ title = item["title"].replace("File:", "").rsplit('.', 1)[0]
+ result = {
+ 'url': imageinfo["descriptionurl"],
+ 'title': title,
+ 'content': item["snippet"],
+ 'img_src': imageinfo["url"],
+ 'img_format': f'{imageinfo["width"]} x {imageinfo["height"]}',
+ 'thumbnail_src': imageinfo["thumburl"],
+ 'template': 'images.html',
+ }
+ results.append(result)
+
+ return results
diff --git a/searx/settings.yml b/searx/settings.yml
index 9f53c4d0f..85434370d 100644
--- a/searx/settings.yml
+++ b/searx/settings.yml
@@ -1640,6 +1640,12 @@ engines:
website: https://www.wikivoyage.org/
wikidata_id: Q373
+ - name: wikicommons.images
+ engine: wikicommons
+ shortcut: wc
+ categories: images
+ number_of_results: 10
+
- name: wolframalpha
shortcut: wa
# You can use the engine using the official stable API, but you need an API