From 6dd07b88a4b8fce0f98d9274decb95541cebc915 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 6 Mar 2024 10:22:49 +0100 Subject: [PATCH] [feat] lib.rs: use packages.html template --- searx/engines/lib_rs.py | 56 +++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 14 +---------- 2 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 searx/engines/lib_rs.py diff --git a/searx/engines/lib_rs.py b/searx/engines/lib_rs.py new file mode 100644 index 000000000..c9435cc2a --- /dev/null +++ b/searx/engines/lib_rs.py @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +"""lib.rs (packages)""" + +from urllib.parse import quote_plus +from lxml import html +from searx.utils import eval_xpath, eval_xpath_list, extract_text + +about = { + 'website': 'https://lib.rs', + 'wikidata_id': 'Q113486010', + 'use_official_api': False, + 'require_api_key': False, + 'results': "HTML", +} + +categories = ["it", "packages"] + +base_url = 'https://lib.rs' + +results_xpath = '/html/body/main/div/ol/li/a' +url_xpath = './@href' +title_xpath = './div[@class="h"]/h4' +content_xpath = './div[@class="h"]/p' +version_xpath = './div[@class="meta"]/span[contains(@class, "version")]' +download_count_xpath = './div[@class="meta"]/span[@class="downloads"]' +tags_xpath = './div[@class="meta"]/span[contains(@class, "k")]/text()' + + +def request(query, params): + params['url'] = f"{base_url}/search?q={quote_plus(query)}" + + return params + + +def response(resp): + results = [] + + doc = html.fromstring(resp.text) + + for result in eval_xpath_list(doc, results_xpath): + package_name = extract_text(eval_xpath(result, title_xpath)) + results.append( + { + 'template': 'packages.html', + 'title': package_name, + 'url': base_url + extract_text(eval_xpath(result, url_xpath)), # type: ignore + 'content': extract_text(eval_xpath(result, content_xpath)), + 'package_name': package_name, + 'version': extract_text(eval_xpath(result, version_xpath)), + 'popularity': extract_text(eval_xpath(result, download_count_xpath)), + 'tags': eval_xpath_list(result, tags_xpath), + } + ) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 865cb6b77..3f817edb7 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -2254,20 +2254,8 @@ engines: - name: lib.rs shortcut: lrs - engine: xpath - search_url: https://lib.rs/search?q={query} - results_xpath: /html/body/main/div/ol/li/a - url_xpath: ./@href - title_xpath: ./div[@class="h"]/h4 - content_xpath: ./div[@class="h"]/p - categories: [it, packages] + engine: lib_rs disabled: true - about: - website: https://lib.rs - wikidata_id: Q113486010 - use_official_api: false - require_api_key: false - results: HTML - name: sourcehut shortcut: srht