summaryrefslogtreecommitdiff
path: root/searx/engines/npm.py
blob: ac21a62b8624c04132cfc6cca08e348f835583fe (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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""npms.io

"""

from urllib.parse import urlencode
from dateutil import parser


about = {
    "website": "https://npms.io/",
    "wikidata_id": "Q7067518",
    "official_api_documentation": "https://api-docs.npms.io/",
    "use_official_api": False,
    "require_api_key": False,
    "results": "JSON",
}

categories = ['it', 'packages']


# engine dependent config
paging = True
page_size = 25
search_api = "https://api.npms.io/v2/search?"


def request(query: str, params):

    args = urlencode(
        {
            'from': (params["pageno"] - 1) * page_size,
            'q': query,
            'size': page_size,
        }
    )
    params['url'] = search_api + args
    return params


def response(resp):
    results = []
    content = resp.json()
    for entry in content["results"]:
        package = entry["package"]
        publishedDate = package.get("date")
        if publishedDate:
            publishedDate = parser.parse(publishedDate)
        tags = list(entry.get("flags", {}).keys()) + package.get("keywords", [])
        results.append(
            {
                "template": "packages.html",
                "url": package["links"]["npm"],
                "title": package["name"],
                'package_name': package["name"],
                "content": package.get("description", ""),
                "version": package.get("version"),
                "maintainer": package.get("author", {}).get("name"),
                'publishedDate': publishedDate,
                "tags": tags,
                "homepage": package["links"].get("homepage"),
                "source_code_url": package["links"].get("repository"),
            }
        )

    return results