archlinux.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 import network
  14. from searx.utils import extract_text, eval_xpath_list, eval_xpath_getindex
  15. from searx.enginelib.traits import EngineTraits
  16. from searx.locales import language_tag
  17. if TYPE_CHECKING:
  18. import logging
  19. logger: logging.Logger
  20. traits: EngineTraits
  21. about = {
  22. "website": 'https://wiki.archlinux.org/',
  23. "wikidata_id": 'Q101445877',
  24. "official_api_documentation": None,
  25. "use_official_api": False,
  26. "require_api_key": False,
  27. "results": 'HTML',
  28. }
  29. # engine dependent config
  30. categories = ['it', 'software wikis']
  31. paging = True
  32. main_wiki = 'wiki.archlinux.org'
  33. def request(query, params):
  34. sxng_lang = params['searxng_locale'].split('-')[0]
  35. netloc = traits.custom['wiki_netloc'].get(sxng_lang, main_wiki)
  36. title = traits.custom['title'].get(sxng_lang, 'Special:Search')
  37. base_url = 'https://' + netloc + '/index.php?'
  38. offset = (params['pageno'] - 1) * 20
  39. if netloc == main_wiki:
  40. eng_lang: str = traits.get_language(sxng_lang, 'English')
  41. query += ' (' + eng_lang + ')'
  42. elif netloc == 'wiki.archlinuxcn.org':
  43. base_url = 'https://' + netloc + '/wzh/index.php?'
  44. args = {
  45. 'search': query,
  46. 'title': title,
  47. 'limit': 20,
  48. 'offset': offset,
  49. 'profile': 'default',
  50. }
  51. params['url'] = base_url + urlencode(args)
  52. return params
  53. def response(resp):
  54. results = []
  55. dom = lxml.html.fromstring(resp.text)
  56. # get the base URL for the language in which request was made
  57. sxng_lang = resp.search_params['searxng_locale'].split('-')[0]
  58. netloc = traits.custom['wiki_netloc'].get(sxng_lang, main_wiki)
  59. base_url = 'https://' + netloc + '/index.php?'
  60. for result in eval_xpath_list(dom, '//ul[@class="mw-search-results"]/li'):
  61. link = eval_xpath_getindex(result, './/div[@class="mw-search-result-heading"]/a', 0)
  62. content = extract_text(result.xpath('.//div[@class="searchresult"]'))
  63. results.append(
  64. {
  65. 'url': urljoin(base_url, link.get('href')),
  66. 'title': extract_text(link),
  67. 'content': content,
  68. }
  69. )
  70. return results
  71. def fetch_traits(engine_traits: EngineTraits):
  72. """Fetch languages from Archlinix-Wiki. The location of the Wiki address of a
  73. language is mapped in a :py:obj:`custom field
  74. <searx.enginelib.traits.EngineTraits.custom>` (``wiki_netloc``). Depending
  75. on the location, the ``title`` argument in the request is translated.
  76. .. code:: python
  77. "custom": {
  78. "wiki_netloc": {
  79. "de": "wiki.archlinux.de",
  80. # ...
  81. "zh": "wiki.archlinuxcn.org"
  82. }
  83. "title": {
  84. "de": "Spezial:Suche",
  85. # ...
  86. "zh": "Special:\u641c\u7d22"
  87. },
  88. },
  89. """
  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 = network.get('https://wiki.archlinux.org/')
  99. if not resp.ok:
  100. print("ERROR: response from wiki.archlinix.org is not OK.")
  101. dom = lxml.html.fromstring(resp.text)
  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
  114. eng_tag = extract_text(eval_xpath_list(a, ".//span"))
  115. engine_traits.languages[sxng_tag] = eng_tag
  116. engine_traits.languages['en'] = 'English'