.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
pkg_go_dev.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""pkg.go.dev (packages)"""
3
4import re
5from urllib.parse import urlencode
6from dateutil import parser
7
8import babel
9import flask_babel
10from lxml import html
11from searx.utils import eval_xpath, eval_xpath_list, extract_text
12
13about = {
14 'website': 'https://pkg.go.dev/',
15 'use_official_api': False,
16 'official_api_documentation': None,
17 'require_api_key': False,
18 'results': 'HTML',
19}
20
21categories = ["packages", "it"]
22
23base_url = "https://pkg.go.dev"
24max_result_count = 50
25
26results_xpath = '/html/body/main/div[contains(@class,"SearchResults")]/div[not(@class)]/div[@class="SearchSnippet"]'
27url_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/@href'
28title_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/text()'
29package_name_xpath = './div[@class="SearchSnippet-headerContainer"]/h2/a/span/text()'
30version_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span/strong[1]/text()'
31updated_xpath = (
32 './div[contains(@class, "SearchSnippet-infoLabel")]/span/span[@data-test-id="snippet-published"]/strong/text()'
33)
34content_xpath = './p[@class="SearchSnippet-synopsis"]'
35popularity_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/a/strong/text()'
36license_name_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span[@data-test-id="snippet-license"]/a/text()'
37license_url_xpath = './div[contains(@class, "SearchSnippet-infoLabel")]/span[@data-test-id="snippet-license"]/a/@href'
38
39
40def request(query, params):
41 args = {
42 'q': query,
43 'm': 'package',
44 'limit': max_result_count,
45 }
46 params['url'] = f"{base_url}/search?{urlencode(args)}"
47
48 return params
49
50
51def response(resp):
52 results = []
53
54 doc = html.fromstring(resp.text)
55
56 for result in eval_xpath_list(doc, results_xpath):
57 publishedDate = extract_text(eval_xpath(result, updated_xpath))
58 try:
59 publishedDate = parser.parse(publishedDate)
60 except parser.ParserError:
61 publishedDate = None
62
63 # 110n 15,000.00 (EN) --> 15.000,00 (DE)
64 popularity = extract_text(eval_xpath(result, popularity_xpath)).strip()
65 popularity = babel.numbers.parse_decimal(popularity, locale='en_US')
66 # popularity is of type str ..
67 popularity = flask_babel.format_decimal(popularity)
68
69 results.append(
70 {
71 'template': 'packages.html',
72 'url': base_url + extract_text(eval_xpath(result, url_xpath)),
73 'title': extract_text(eval_xpath(result, title_xpath)),
74 'content': extract_text(eval_xpath(result, content_xpath)),
75 'package_name': re.sub(r"\‍(|\‍)", "", extract_text(eval_xpath(result, package_name_xpath))),
76 'version': extract_text(eval_xpath(result, version_xpath)),
77 'popularity': popularity,
78 'license_name': extract_text(eval_xpath(result, license_name_xpath)),
79 'license_url': base_url + extract_text(eval_xpath(result, license_url_xpath)),
80 'publishedDate': publishedDate,
81 }
82 )
83
84 return results
request(query, params)
Definition pkg_go_dev.py:40