archlinux.py 4.5 KB

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