searxng/searx/engines/pkg_go_dev.py
Markus Heiser 86b4d2f2d0 [mod] activate pyright checks (in CI)
We have been using a static type checker (pyright) for a long time, but its
check was not yet a prerequisite for passing the quality gate.  It was checked
in the CI, but the error messages were only logged.

As is always the case in life, with checks that you have to do but which have no
consequences; you neglect them :-)

We didn't activate the checks back then because we (even today) have too much
monkey patching in our code (not only in the engines, httpx and others objects
are also affected).

We want to replace monkey patching with clear interfaces for a long time, the
basis for this is increased typing and we can only achieve this if we make type
checking an integral part of the quality gate.

  This PR activates the type check; in order to pass the check, a few typings
  were corrected in the code, but most type inconsistencies were deactivated via
  inline comments.

This was particularly necessary in places where the code uses properties that
stick to the objects (monkey patching).  The sticking of properties only happens
in a few places, but the access to these properties extends over the entire
code, which is why there are many `# type: ignore` markers in the code ... which
we will hopefully be able to remove again successively in the future.

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
2024-04-27 18:31:52 +02:00

89 lines
3.2 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""pkg.go.dev (packages)"""
import re
from urllib.parse import urlencode
from dateutil import parser
import babel.numbers
import flask_babel
from lxml import html
from searx.utils import eval_xpath, eval_xpath_list, extract_text
about = {
'website': 'https://pkg.go.dev/',
'use_official_api': False,
'official_api_documentation': None,
'require_api_key': False,
'results': 'HTML',
}
categories = ["packages", "it"]
base_url = "https://pkg.go.dev"
max_result_count = 50
results_xpath = '/html/body/main/div[contains(@class,"SearchResults")]/div[not(@class)]/div[@class="SearchSnippet"]'
url_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/@href'
title_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/text()'
package_name_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/span/text()'
version_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span/strong[1]/text()'
updated_xpath = (
'./div[contains(@class, "SearchSnippet-infoLabel")]/span/span[@data-test-id="snippet-published"]/strong/text()'
)
content_xpath = './p[@class="SearchSnippet-synopsis"]'
popularity_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/a/strong/text()'
license_name_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span[@data-test-id="snippet-license"]/a/text()'
license_url_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span[@data-test-id="snippet-license"]/a/@href'
def request(query, params):
args = {
'q': query,
'm': 'package',
'limit': max_result_count,
}
params['url'] = f"{base_url}/search?{urlencode(args)}"
return params
def response(resp):
results = []
doc = html.fromstring(resp.text)
for result in eval_xpath_list(doc, results_xpath):
publishedDate = extract_text(eval_xpath(result, updated_xpath))
try:
publishedDate = parser.parse(publishedDate)
except parser.ParserError:
publishedDate = None
# 110n 15,000.00 (EN) --> 15.000,00 (DE)
popularity = extract_text(eval_xpath(result, popularity_xpath)).strip()
popularity = babel.numbers.parse_decimal(popularity, locale='en_US')
# popularity is of type str ..
popularity = flask_babel.format_decimal(popularity)
results.append(
{
'template': 'packages.html',
'url': base_url + extract_text(eval_xpath(result, url_xpath)), # type: ignore
'title': extract_text(eval_xpath(result, title_xpath)),
'content': extract_text(eval_xpath(result, content_xpath)),
'package_name': re.sub(
r"\(|\)",
"",
extract_text(eval_xpath(result, package_name_xpath)),
), # type: ignore
'version': extract_text(eval_xpath(result, version_xpath)),
'popularity': popularity,
'license_name': extract_text(eval_xpath(result, license_name_xpath)),
'license_url': base_url + extract_text(eval_xpath(result, license_url_xpath)), # type: ignore
'publishedDate': publishedDate,
}
)
return results