From a49232ee29aa01e5ef2a825bec5f09021a69e0cc Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Tue, 14 May 2024 20:06:20 +0100 Subject: [PATCH] [feat] engine: implementation of cargo search (crates.io) --- searx/engines/crates.py | 70 +++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 6 ++++ 2 files changed, 76 insertions(+) create mode 100644 searx/engines/crates.py diff --git a/searx/engines/crates.py b/searx/engines/crates.py new file mode 100644 index 000000000..a87ef42c8 --- /dev/null +++ b/searx/engines/crates.py @@ -0,0 +1,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 diff --git a/searx/settings.yml b/searx/settings.yml index b5f1a2b36..65c4966d4 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -924,6 +924,12 @@ engines: shortcut: hex disabled: true + - name: crates.io + engine: crates + shortcut: crates + disabled: true + timeout: 6.0 + - name: hoogle engine: xpath search_url: https://hoogle.haskell.org/?hoogle={query}