archlinux.py 4.5 KB

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