summaryrefslogtreecommitdiff
path: root/searx/engines/crates.py
blob: a87ef42c852533ed085cae5cc064598167fd5d5d (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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Cargo search on crates.io"""

from collections import OrderedDict
from urllib.parse import urlencode

from dateutil import parser

about = {
    "website": "https://crates.io/",
    "wikidata_id": None,
    "official_api_documentation": "https://crates.io/data-access",
    "use_official_api": True,
    "require_api_key": False,
    "results": "JSON",
}

categories = ["it", "packages", "cargo"]


# engine dependent config
paging = True
page_size = 10
search_url = "https://crates.io/api/v1/crates"

linked_terms = OrderedDict(
    [
        ("homepage", "Project homepage"),
        ("documentation", "Documentation"),
        ("repository", "Source code"),
    ]
)


def request(query: str, params):

    args = urlencode({"page": params["pageno"], "q": query, "per_page": page_size})
    params["url"] = f"{search_url}?{args}"
    return params


def response(resp):
    results = []

    for package in resp.json()["crates"]:

        published_date = package.get("updated_at")
        published_date = parser.parse(published_date)

        links = {}
        for k, v in linked_terms.items():
            l = package.get(k)
            if l:
                links[v] = l

        results.append(
            {
                "template": "packages.html",
                "url": f'https://crates.io/crates/{package["name"]}',
                "title": package["name"],
                "package_name": package["name"],
                "tags": package["keywords"],
                "content": package["description"],
                "version": package["newest_version"] or package["max_version"] or package["max_stable_version"],
                "publishedDate": published_date,
                "links": links,
            }
        )

    return results