1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-10 23:20:12 +01:00

[feat] wikimedia commons: support for videos, audio and other files

This commit is contained in:
Bnyro 2024-04-25 18:21:47 +02:00 committed by Markus Heiser
parent 46d7a8289b
commit 78077126f2
2 changed files with 60 additions and 6 deletions

View File

@ -3,6 +3,8 @@
""" """
import datetime
from urllib.parse import urlencode from urllib.parse import urlencode
# about # about
@ -14,6 +16,8 @@ about = {
"require_api_key": False, "require_api_key": False,
"results": 'JSON', "results": 'JSON',
} }
categories = ['images']
search_type = 'images'
base_url = "https://commons.wikimedia.org" base_url = "https://commons.wikimedia.org"
search_prefix = ( search_prefix = (
@ -29,17 +33,29 @@ search_prefix = (
paging = True paging = True
number_of_results = 10 number_of_results = 10
search_types = {
'images': 'bitmap|drawing',
'videos': 'video',
'audio': 'audio',
'files': 'multimedia|office|archive|3d',
}
def request(query, params): def request(query, params):
language = 'en' language = 'en'
if params['language'] != 'all': if params['language'] != 'all':
language = params['language'].split('-')[0] language = params['language'].split('-')[0]
if search_type not in search_types:
raise ValueError(f"Unsupported search type: {search_type}")
filetype = search_types[search_type]
args = { args = {
'uselang': language, 'uselang': language,
'gsrlimit': number_of_results, 'gsrlimit': number_of_results,
'gsroffset': number_of_results * (params["pageno"] - 1), 'gsroffset': number_of_results * (params["pageno"] - 1),
'gsrsearch': "filetype:bitmap|drawing " + query, 'gsrsearch': f"filetype:{filetype} {query}",
} }
params["url"] = f"{base_url}/w/api.php{search_prefix}&{urlencode(args, safe=':|')}" params["url"] = f"{base_url}/w/api.php{search_prefix}&{urlencode(args, safe=':|')}"
@ -52,7 +68,6 @@ def response(resp):
if not json.get("query", {}).get("pages"): if not json.get("query", {}).get("pages"):
return results return results
for item in json["query"]["pages"].values(): for item in json["query"]["pages"].values():
imageinfo = item["imageinfo"][0] imageinfo = item["imageinfo"][0]
title = item["title"].replace("File:", "").rsplit('.', 1)[0] title = item["title"].replace("File:", "").rsplit('.', 1)[0]
@ -60,11 +75,28 @@ def response(resp):
'url': imageinfo["descriptionurl"], 'url': imageinfo["descriptionurl"],
'title': title, 'title': title,
'content': item["snippet"], 'content': item["snippet"],
'img_src': imageinfo["url"],
'resolution': f'{imageinfo["width"]} x {imageinfo["height"]}',
'thumbnail_src': imageinfo["thumburl"],
'template': 'images.html',
} }
if search_type == "images":
result['template'] = 'images.html'
result['img_src'] = imageinfo["url"]
result['thumbnail_src'] = imageinfo["thumburl"]
result['resolution'] = f'{imageinfo["width"]} x {imageinfo["height"]}'
else:
result['thumbnail'] = imageinfo["thumburl"]
if search_type == "videos":
result['template'] = 'videos.html'
if imageinfo.get('duration'):
result['length'] = datetime.timedelta(seconds=int(imageinfo['duration']))
result['iframe_src'] = imageinfo['url']
elif search_type == "files":
result['template'] = 'files.html'
result['metadata'] = imageinfo['mime']
result['size'] = imageinfo['size']
elif search_type == "audio":
result['iframe_src'] = imageinfo['url']
results.append(result) results.append(result)
return results return results

View File

@ -1908,6 +1908,28 @@ engines:
engine: wikicommons engine: wikicommons
shortcut: wc shortcut: wc
categories: images categories: images
search_type: images
number_of_results: 10
- name: wikicommons.videos
engine: wikicommons
shortcut: wcv
categories: videos
search_type: videos
number_of_results: 10
- name: wikicommons.audio
engine: wikicommons
shortcut: wca
categories: music
search_type: audio
number_of_results: 10
- name: wikicommons.files
engine: wikicommons
shortcut: wcf
categories: files
search_type: files
number_of_results: 10 number_of_results: 10
- name: wolframalpha - name: wolframalpha