.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 elif netloc == 'wiki.archlinuxcn.org':
55 base_url = 'https://' + netloc + '/wzh/index.php?'
56
57 args = {
58 'search': query,
59 'title': title,
60 'limit': 20,
61 'offset': offset,
62 'profile': 'default',
63 }
64
65 params['url'] = base_url + urlencode(args)
66 return params
67
68
69def response(resp):
70
71 results = []
72 dom = lxml.html.fromstring(resp.text) # type: ignore
73
74 # get the base URL for the language in which request was made
75 sxng_lang = resp.search_params['searxng_locale'].split('-')[0]
76 netloc: str = traits.custom['wiki_netloc'].get(sxng_lang, main_wiki) # type: ignore
77 base_url = 'https://' + netloc + '/index.php?'
78
79 for result in eval_xpath_list(dom, '//ul[@class="mw-search-results"]/li'):
80 link = eval_xpath_getindex(result, './/div[@class="mw-search-result-heading"]/a', 0)
81 content = extract_text(result.xpath('.//div[@class="searchresult"]'))
82 results.append(
83 {
84 'url': urljoin(base_url, link.get('href')), # type: ignore
85 'title': extract_text(link),
86 'content': content,
87 }
88 )
89
90 return results
91
92
93def fetch_traits(engine_traits: EngineTraits):
94 """Fetch languages from Archlinux-Wiki. The location of the Wiki address of a
95 language is mapped in a :py:obj:`custom field
96 <searx.enginelib.traits.EngineTraits.custom>` (``wiki_netloc``). Depending
97 on the location, the ``title`` argument in the request is translated.
98
99 .. code:: python
100
101 "custom": {
102 "wiki_netloc": {
103 "de": "wiki.archlinux.de",
104 # ...
105 "zh": "wiki.archlinuxcn.org"
106 }
107 "title": {
108 "de": "Spezial:Suche",
109 # ...
110 "zh": "Special:\u641c\u7d22"
111 },
112 },
113
114 """
115 # pylint: disable=import-outside-toplevel
116 from searx.network import get # see https://github.com/searxng/searxng/issues/762
117
118 engine_traits.custom['wiki_netloc'] = {}
119 engine_traits.custom['title'] = {}
120
121 title_map = {
122 'de': 'Spezial:Suche',
123 'fa': 'ویژه:جستجو',
124 'ja': '特別:検索',
125 'zh': 'Special:搜索',
126 }
127
128 resp = get('https://wiki.archlinux.org/')
129 if not resp.ok: # type: ignore
130 print("ERROR: response from wiki.archlinux.org is not OK.")
131
132 dom = lxml.html.fromstring(resp.text) # type: ignore
133 for a in eval_xpath_list(dom, "//a[@class='interlanguage-link-target']"):
134
135 sxng_tag = language_tag(babel.Locale.parse(a.get('lang'), sep='-'))
136 # zh_Hans --> zh
137 sxng_tag = sxng_tag.split('_')[0]
138
139 netloc = urlparse(a.get('href')).netloc
140 if netloc != 'wiki.archlinux.org':
141 title = title_map.get(sxng_tag)
142 if not title:
143 print("ERROR: title tag from %s (%s) is unknown" % (netloc, sxng_tag))
144 continue
145 engine_traits.custom['wiki_netloc'][sxng_tag] = netloc
146 engine_traits.custom['title'][sxng_tag] = title # type: ignore
147
148 eng_tag = extract_text(eval_xpath_list(a, ".//span"))
149 engine_traits.languages[sxng_tag] = eng_tag # type: ignore
150
151 engine_traits.languages['en'] = 'English'
request(query, params)
Definition archlinux.py:43
fetch_traits(EngineTraits engine_traits)
Definition archlinux.py:93