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