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