.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
repology.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Repology_ monitors a huge number of package repositories and other sources
3comparing packages versions across them and gathering other information.
4
5Repology_ shows you in which repositories a given project is packaged, which
6version is the latest and which needs updating, who maintains the package, and
7other related information.
8
9.. _Repology: https://repology.org/docs/about
10
11Configuration
12=============
13
14The engine is inactive by default, meaning it is not available in the service.
15If you want to offer the engine, the ``inactive`` flag must be set to ``false``.
16
17.. code:: yaml
18
19 - name: repology
20 inactive: false
21
22"""
23
24import typing as t
25
26from urllib.parse import urlencode
27from searx.result_types import EngineResults
28
29if t.TYPE_CHECKING:
30 from searx.extended_types import SXNG_Response
31
32about = {
33 'website': 'https://repology.org',
34 'wikidata_id': 'Q107409859',
35 'use_official_api': True,
36 'official_api_documentation': 'https://repology.org/api/v1',
37 'require_api_key': False,
38 'results': 'JSON',
39}
40categories: list[str] = ['packages', 'it']
41base_url: str = 'https://repology.org'
42
43
44def request(query: str, params: dict[str, t.Any]) -> None:
45 args = {
46 'search': query,
47 }
48 params['url'] = f"{base_url}/api/v1/projects/?{urlencode(args)}"
49
50
51def _get_most_common(items: list[str | None]) -> str | None:
52 counts: dict[str | None, int] = {}
53 for item in items:
54 if item:
55 counts[item] = counts.get(item, 0) + 1
56
57 if len(counts) == 0:
58 return None
59 return max(counts, key=counts.get)
60
61
62def _flatten(xss):
63 return [x for xs in xss for x in xs]
64
65
66def response(resp: 'SXNG_Response') -> EngineResults:
67 res = EngineResults()
68
69 resp_json = resp.json()
70 for pkgname, repositories in resp_json.items():
71
72 # either there's a package with status "newest" or we assume that the
73 # most commonly used version is the latest released (non-alpha) version
74 latest_version = None
75 for repo in repositories:
76 if repo.get("status") == "newest":
77 latest_version = repo["version"]
78 break
79 else:
80 latest_version = _get_most_common([repo.get("version") for repo in repositories])
81
82 res.add(
83 res.types.LegacyResult(
84 template='packages.html',
85 url=f"{base_url}/project/{pkgname}/versions",
86 title=pkgname,
87 content=_get_most_common([pkg.get("summary") for pkg in repositories]),
88 package_name=_get_most_common([pkg.get("visiblename") for pkg in repositories]),
89 version=latest_version,
90 license_name=_get_most_common(_flatten([pkg.get("licenses", []) for pkg in repositories])),
91 tags=list({pkg.get("repo") for pkg in repositories}), # ensure that tags are unique
92 )
93 )
94
95 return res
None request(str query, dict[str, t.Any] params)
Definition repology.py:44
str|None _get_most_common(list[str|None] items)
Definition repology.py:51
EngineResults response('SXNG_Response' resp)
Definition repology.py:66