.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
wikicommons.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Wikimedia Commons (images)
3
4"""
5
6import datetime
7
8from urllib.parse import urlencode
9
10# about
11about = {
12 "website": 'https://commons.wikimedia.org/',
13 "wikidata_id": 'Q565',
14 "official_api_documentation": 'https://commons.wikimedia.org/w/api.php',
15 "use_official_api": True,
16 "require_api_key": False,
17 "results": 'JSON',
18}
19categories = ['images']
20search_type = 'images'
21
22base_url = "https://commons.wikimedia.org"
23search_prefix = (
24 '?action=query'
25 '&format=json'
26 '&generator=search'
27 '&gsrnamespace=6'
28 '&gsrprop=snippet'
29 '&prop=info|imageinfo'
30 '&iiprop=url|size|mime'
31 '&iiurlheight=180' # needed for the thumb url
32)
33paging = True
34number_of_results = 10
35
36search_types = {
37 'images': 'bitmap|drawing',
38 'videos': 'video',
39 'audio': 'audio',
40 'files': 'multimedia|office|archive|3d',
41}
42
43
44def request(query, params):
45 language = 'en'
46 if params['language'] != 'all':
47 language = params['language'].split('-')[0]
48
49 if search_type not in search_types:
50 raise ValueError(f"Unsupported search type: {search_type}")
51
52 filetype = search_types[search_type]
53
54 args = {
55 'uselang': language,
56 'gsrlimit': number_of_results,
57 'gsroffset': number_of_results * (params["pageno"] - 1),
58 'gsrsearch': f"filetype:{filetype} {query}",
59 }
60
61 params["url"] = f"{base_url}/w/api.php{search_prefix}&{urlencode(args, safe=':|')}"
62 return params
63
64
65def response(resp):
66 results = []
67 json = resp.json()
68
69 if not json.get("query", {}).get("pages"):
70 return results
71 for item in json["query"]["pages"].values():
72 imageinfo = item["imageinfo"][0]
73 title = item["title"].replace("File:", "").rsplit('.', 1)[0]
74 result = {
75 'url': imageinfo["descriptionurl"],
76 'title': title,
77 'content': item["snippet"],
78 }
79
80 if search_type == "images":
81 result['template'] = 'images.html'
82 result['img_src'] = imageinfo["url"]
83 result['thumbnail_src'] = imageinfo["thumburl"]
84 result['resolution'] = f'{imageinfo["width"]} x {imageinfo["height"]}'
85 else:
86 result['thumbnail'] = imageinfo["thumburl"]
87
88 if search_type == "videos":
89 result['template'] = 'videos.html'
90 if imageinfo.get('duration'):
91 result['length'] = datetime.timedelta(seconds=int(imageinfo['duration']))
92 result['iframe_src'] = imageinfo['url']
93 elif search_type == "files":
94 result['template'] = 'files.html'
95 result['metadata'] = imageinfo['mime']
96 result['size'] = imageinfo['size']
97 elif search_type == "audio":
98 result['iframe_src'] = imageinfo['url']
99
100 results.append(result)
101
102 return results