.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
archlinux.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3Arch Linux Wiki
4~~~~~~~~~~~~~~~
5
6This implementation does not use a official API: Mediawiki provides API, but
7Arch Wiki blocks access to it.
8
9"""
10
11from typing import TYPE_CHECKING
12from urllib.parse import urlencode, urljoin, urlparse
13import lxml
14import babel
15
16from searx.utils import extract_text, eval_xpath_list, eval_xpath_getindex
17from searx.enginelib.traits import EngineTraits
18from searx.locales import language_tag
19
20if TYPE_CHECKING:
21 import logging
22
23 logger: logging.Logger
24
25traits: EngineTraits
26
27
28about = {
29 "website": 'https://wiki.archlinux.org/',
30 "wikidata_id": 'Q101445877',
31 "official_api_documentation": None,
32 "use_official_api": False,
33 "require_api_key": False,
34 "results": 'HTML',
35}
36
37# engine dependent config
38categories = ['it', 'software wikis']
39paging = True
40main_wiki = 'wiki.archlinux.org'
41
42
43def request(query, params):
44
45 sxng_lang = params['searxng_locale'].split('-')[0]
46 netloc: str = traits.custom['wiki_netloc'].get(sxng_lang, main_wiki) # type: ignore
47 title: str = traits.custom['title'].get(sxng_lang, 'Special:Search') # type: ignore
48 base_url = 'https://' + netloc + '/index.php?'
49 offset = (params['pageno'] - 1) * 20
50
51 if netloc == main_wiki:
52 eng_lang: str = traits.get_language(sxng_lang, 'English') # type: ignore
53 query += ' (' + eng_lang + ')'
54 # wiki.archlinux.org is protected by anubis
55 # - https://github.com/searxng/searxng/issues/4646#issuecomment-2817848019
56 params['headers']['User-Agent'] = "SearXNG"
57 elif netloc == 'wiki.archlinuxcn.org':
58 base_url = 'https://' + netloc + '/wzh/index.php?'
59
60 args = {
61 'search': query,
62 'title': title,
63 'limit': 20,
64 'offset': offset,
65 'profile': 'default',
66 }
67
68 params['url'] = base_url + urlencode(args)
69 return params
70
71
72def response(resp):
73
74 results = []
75 dom = lxml.html.fromstring(resp.text) # type: ignore
76
77 # get the base URL for the language in which request was made
78 sxng_lang = resp.search_params['searxng_locale'].split('-')[0]
79 netloc: str = traits.custom['wiki_netloc'].get(sxng_lang, main_wiki) # type: ignore
80 base_url = 'https://' + netloc + '/index.php?'
81
82 for result in eval_xpath_list(dom, '//ul[@class="mw-search-results"]/li'):
83 link = eval_xpath_getindex(result, './/div[@class="mw-search-result-heading"]/a', 0)
84 content = extract_text(result.xpath('.//div[@class="searchresult"]'))
85 results.append(
86 {
87 'url': urljoin(base_url, link.get('href')), # type: ignore
88 'title': extract_text(link),
89 'content': content,
90 }
91 )
92
93 return results
94
95
96def fetch_traits(engine_traits: EngineTraits):
97 """Fetch languages from Archlinux-Wiki. The location of the Wiki address of a
98 language is mapped in a :py:obj:`custom field
99 <searx.enginelib.traits.EngineTraits.custom>` (``wiki_netloc``). Depending
100 on the location, the ``title`` argument in the request is translated.
101
102 .. code:: python
103
104 "custom": {
105 "wiki_netloc": {
106 "de": "wiki.archlinux.de",
107 # ...
108 "zh": "wiki.archlinuxcn.org"
109 }
110 "title": {
111 "de": "Spezial:Suche",
112 # ...
113 "zh": "Special:\u641c\u7d22"
114 },
115 },
116
117 """
118 # pylint: disable=import-outside-toplevel
119 from searx.network import get # see https://github.com/searxng/searxng/issues/762
120
121 engine_traits.custom['wiki_netloc'] = {}
122 engine_traits.custom['title'] = {}
123
124 title_map = {
125 'de': 'Spezial:Suche',
126 'fa': 'ویژه:جستجو',
127 'ja': '特別:検索',
128 'zh': 'Special:搜索',
129 }
130
131 resp = get('https://wiki.archlinux.org/')
132 if not resp.ok: # type: ignore
133 print("ERROR: response from wiki.archlinux.org is not OK.")
134
135 dom = lxml.html.fromstring(resp.text) # type: ignore
136 for a in eval_xpath_list(dom, "//a[@class='interlanguage-link-target']"):
137
138 sxng_tag = language_tag(babel.Locale.parse(a.get('lang'), sep='-'))
139 # zh_Hans --> zh
140 sxng_tag = sxng_tag.split('_')[0]
141
142 netloc = urlparse(a.get('href')).netloc
143 if netloc != 'wiki.archlinux.org':
144 title = title_map.get(sxng_tag)
145 if not title:
146 print("ERROR: title tag from %s (%s) is unknown" % (netloc, sxng_tag))
147 continue
148 engine_traits.custom['wiki_netloc'][sxng_tag] = netloc
149 engine_traits.custom['title'][sxng_tag] = title # type: ignore
150
151 eng_tag = extract_text(eval_xpath_list(a, ".//span"))
152 engine_traits.languages[sxng_tag] = eng_tag # type: ignore
153
154 engine_traits.languages['en'] = 'English'
request(query, params)
Definition archlinux.py:43
fetch_traits(EngineTraits engine_traits)
Definition archlinux.py:96