summaryrefslogtreecommitdiff
path: root/searx/engines/docker_hub.py
blob: 021c05827abac719747c0045f7e8ed2d73dc23d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Docker Hub (IT)

"""
# pylint: disable=use-dict-literal

from urllib.parse import urlencode
from dateutil import parser

about = {
    "website": 'https://hub.docker.com',
    "wikidata_id": 'Q100769064',
    "official_api_documentation": 'https://docs.docker.com/registry/spec/api/',
    "use_official_api": True,
    "require_api_key": False,
    "results": 'JSON',
}

categories = ['it', 'packages']  # optional
paging = True

base_url = "https://hub.docker.com"
page_size = 10


def request(query, params):
    args = {
        "query": query,
        "from": page_size * (params['pageno'] - 1),
        "size": page_size,
    }
    params['url'] = f"{base_url}/api/search/v3/catalog/search?{urlencode(args)}"

    return params


def response(resp):
    '''post-response callback
    resp: requests response object
    '''
    results = []
    json_resp = resp.json()

    for item in json_resp.get("results", []):
        image_source = item.get("source")
        is_official = image_source in ["store", "official"]

        popularity_infos = [f"{item.get('star_count', 0)} stars"]

        architectures = []
        for rate_plan in item.get("rate_plans", []):
            pull_count = rate_plan.get("repositories", [{}])[0].get("pull_count")
            if pull_count:
                popularity_infos.insert(0, f"{pull_count} pulls")
            architectures.extend(arch['name'] for arch in rate_plan.get("architectures", []) if arch['name'])

        result = {
            'template': 'packages.html',
            'url': base_url + ("/_/" if is_official else "/r/") + item.get("slug", ""),
            'title': item.get("name"),
            'content': item.get("short_description"),
            'thumbnail': item["logo_url"].get("large") or item["logo_url"].get("small"),
            'package_name': item.get("name"),
            'maintainer': item["publisher"].get("name"),
            'publishedDate': parser.parse(item.get("updated_at") or item.get("created_at")),
            'popularity': ', '.join(popularity_infos),
            'tags': architectures,
        }
        results.append(result)

    return results