archlinux.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Arch Linux Wiki
  4. API: Mediawiki provides API, but Arch Wiki blocks access to it
  5. """
  6. from urllib.parse import urlencode, urljoin
  7. from lxml import html
  8. from searx.utils import extract_text, eval_xpath_list, eval_xpath_getindex
  9. # about
  10. about = {
  11. "website": 'https://wiki.archlinux.org/',
  12. "wikidata_id": 'Q101445877',
  13. "official_api_documentation": None,
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'HTML',
  17. }
  18. # engine dependent config
  19. categories = ['it']
  20. language_support = True
  21. paging = True
  22. base_url = 'https://wiki.archlinux.org'
  23. # xpath queries
  24. xpath_results = '//ul[@class="mw-search-results"]/li'
  25. xpath_link = './/div[@class="mw-search-result-heading"]/a'
  26. # cut 'en' from 'en-US', 'de' from 'de-CH', and so on
  27. def locale_to_lang_code(locale):
  28. if locale.find('-') >= 0:
  29. locale = locale.split('-')[0]
  30. return locale
  31. # wikis for some languages were moved off from the main site, we need to make
  32. # requests to correct URLs to be able to get results in those languages
  33. lang_urls = {
  34. 'all': {
  35. 'base': 'https://wiki.archlinux.org',
  36. 'search': '/index.php?title=Special:Search&offset={offset}&{query}'
  37. },
  38. 'de': {
  39. 'base': 'https://wiki.archlinux.de',
  40. 'search': '/index.php?title=Spezial:Suche&offset={offset}&{query}'
  41. },
  42. 'fr': {
  43. 'base': 'https://wiki.archlinux.fr',
  44. 'search': '/index.php?title=Spécial:Recherche&offset={offset}&{query}'
  45. },
  46. 'ja': {
  47. 'base': 'https://wiki.archlinuxjp.org',
  48. 'search': '/index.php?title=特別:検索&offset={offset}&{query}'
  49. },
  50. 'ro': {
  51. 'base': 'http://wiki.archlinux.ro',
  52. 'search': '/index.php?title=Special:Căutare&offset={offset}&{query}'
  53. },
  54. 'tr': {
  55. 'base': 'http://archtr.org/wiki',
  56. 'search': '/index.php?title=Özel:Ara&offset={offset}&{query}'
  57. }
  58. }
  59. # get base & search URLs for selected language
  60. def get_lang_urls(language):
  61. if language in lang_urls:
  62. return lang_urls[language]
  63. return lang_urls['all']
  64. # Language names to build search requests for
  65. # those languages which are hosted on the main site.
  66. main_langs = {
  67. 'ar': 'العربية',
  68. 'bg': 'Български',
  69. 'cs': 'Česky',
  70. 'da': 'Dansk',
  71. 'el': 'Ελληνικά',
  72. 'es': 'Español',
  73. 'he': 'עברית',
  74. 'hr': 'Hrvatski',
  75. 'hu': 'Magyar',
  76. 'it': 'Italiano',
  77. 'ko': '한국어',
  78. 'lt': 'Lietuviškai',
  79. 'nl': 'Nederlands',
  80. 'pl': 'Polski',
  81. 'pt': 'Português',
  82. 'ru': 'Русский',
  83. 'sl': 'Slovenský',
  84. 'th': 'ไทย',
  85. 'uk': 'Українська',
  86. 'zh': '简体中文'
  87. }
  88. supported_languages = dict(lang_urls, **main_langs)
  89. # do search-request
  90. def request(query, params):
  91. # translate the locale (e.g. 'en-US') to language code ('en')
  92. language = locale_to_lang_code(params['language'])
  93. # if our language is hosted on the main site, we need to add its name
  94. # to the query in order to narrow the results to that language
  95. if language in main_langs:
  96. query += ' (' + main_langs[language] + ')'
  97. # prepare the request parameters
  98. query = urlencode({'search': query})
  99. offset = (params['pageno'] - 1) * 20
  100. # get request URLs for our language of choice
  101. urls = get_lang_urls(language)
  102. search_url = urls['base'] + urls['search']
  103. params['url'] = search_url.format(query=query, offset=offset)
  104. return params
  105. # get response from search-request
  106. def response(resp):
  107. # get the base URL for the language in which request was made
  108. language = locale_to_lang_code(resp.search_params['language'])
  109. base_url = get_lang_urls(language)['base']
  110. results = []
  111. dom = html.fromstring(resp.text)
  112. # parse results
  113. for result in eval_xpath_list(dom, xpath_results):
  114. link = eval_xpath_getindex(result, xpath_link, 0)
  115. href = urljoin(base_url, link.attrib.get('href'))
  116. title = extract_text(link)
  117. results.append({'url': href,
  118. 'title': title})
  119. return results