2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-08-01 08:48:38 +00:00
|
|
|
"""
|
|
|
|
Gentoo Wiki
|
|
|
|
"""
|
|
|
|
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode, urljoin
|
2018-08-01 08:48:38 +00:00
|
|
|
from lxml import html
|
2020-10-02 16:13:56 +00:00
|
|
|
from searx.utils import extract_text
|
2018-08-01 08:48:38 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://wiki.gentoo.org/',
|
|
|
|
"wikidata_id": 'Q1050637',
|
|
|
|
"official_api_documentation": 'https://wiki.gentoo.org/api.php',
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
|
2018-08-01 08:48:38 +00:00
|
|
|
# engine dependent config
|
2021-12-22 15:58:52 +00:00
|
|
|
categories = ['it', 'software wikis']
|
2018-08-01 08:48:38 +00:00
|
|
|
paging = True
|
|
|
|
base_url = 'https://wiki.gentoo.org'
|
|
|
|
|
|
|
|
# xpath queries
|
|
|
|
xpath_results = '//ul[@class="mw-search-results"]/li'
|
|
|
|
xpath_link = './/div[@class="mw-search-result-heading"]/a'
|
2022-12-30 17:28:02 +00:00
|
|
|
xpath_content = './/div[@class="searchresult"]'
|
2018-08-01 08:48:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# cut 'en' from 'en-US', 'de' from 'de-CH', and so on
|
|
|
|
def locale_to_lang_code(locale):
|
|
|
|
if locale.find('-') >= 0:
|
|
|
|
locale = locale.split('-')[0]
|
|
|
|
return locale
|
|
|
|
|
|
|
|
|
|
|
|
# wikis for some languages were moved off from the main site, we need to make
|
|
|
|
# requests to correct URLs to be able to get results in those languages
|
2018-08-19 01:31:09 +00:00
|
|
|
lang_urls = {
|
2021-12-27 08:26:22 +00:00
|
|
|
'en': {'base': 'https://wiki.gentoo.org', 'search': '/index.php?title=Special:Search&offset={offset}&{query}'},
|
2018-08-19 01:22:04 +00:00
|
|
|
'others': {
|
|
|
|
'base': 'https://wiki.gentoo.org',
|
|
|
|
'search': '/index.php?title=Special:Search&offset={offset}&{query}\
|
2021-12-27 08:26:22 +00:00
|
|
|
&profile=translation&languagefilter={language}',
|
|
|
|
},
|
2018-08-01 08:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# get base & search URLs for selected language
|
|
|
|
def get_lang_urls(language):
|
2018-08-19 01:22:04 +00:00
|
|
|
if language != 'en':
|
|
|
|
return lang_urls['others']
|
2018-08-01 08:48:38 +00:00
|
|
|
return lang_urls['en']
|
|
|
|
|
|
|
|
|
|
|
|
# Language names to build search requests for
|
|
|
|
# those languages which are hosted on the main site.
|
|
|
|
main_langs = {
|
|
|
|
'ar': 'العربية',
|
|
|
|
'bg': 'Български',
|
|
|
|
'cs': 'Česky',
|
|
|
|
'da': 'Dansk',
|
|
|
|
'el': 'Ελληνικά',
|
|
|
|
'es': 'Español',
|
|
|
|
'he': 'עברית',
|
|
|
|
'hr': 'Hrvatski',
|
|
|
|
'hu': 'Magyar',
|
|
|
|
'it': 'Italiano',
|
|
|
|
'ko': '한국어',
|
|
|
|
'lt': 'Lietuviškai',
|
|
|
|
'nl': 'Nederlands',
|
|
|
|
'pl': 'Polski',
|
|
|
|
'pt': 'Português',
|
|
|
|
'ru': 'Русский',
|
|
|
|
'sl': 'Slovenský',
|
|
|
|
'th': 'ไทย',
|
|
|
|
'uk': 'Українська',
|
2021-12-27 08:26:22 +00:00
|
|
|
'zh': '简体中文',
|
2018-08-01 08:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
|
|
|
# translate the locale (e.g. 'en-US') to language code ('en')
|
|
|
|
language = locale_to_lang_code(params['language'])
|
|
|
|
|
|
|
|
# if our language is hosted on the main site, we need to add its name
|
|
|
|
# to the query in order to narrow the results to that language
|
|
|
|
if language in main_langs:
|
2020-08-11 14:25:03 +00:00
|
|
|
query += ' (' + main_langs[language] + ')'
|
2018-08-01 08:48:38 +00:00
|
|
|
|
|
|
|
# prepare the request parameters
|
|
|
|
query = urlencode({'search': query})
|
|
|
|
offset = (params['pageno'] - 1) * 20
|
|
|
|
|
|
|
|
# get request URLs for our language of choice
|
|
|
|
urls = get_lang_urls(language)
|
|
|
|
search_url = urls['base'] + urls['search']
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
params['url'] = search_url.format(query=query, offset=offset, language=language)
|
2018-08-01 08:48:38 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
# get the base URL for the language in which request was made
|
|
|
|
language = locale_to_lang_code(resp.search_params['language'])
|
|
|
|
base_url = get_lang_urls(language)['base']
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
|
|
|
|
# parse results
|
|
|
|
for result in dom.xpath(xpath_results):
|
|
|
|
link = result.xpath(xpath_link)[0]
|
|
|
|
href = urljoin(base_url, link.attrib.get('href'))
|
|
|
|
title = extract_text(link)
|
2022-12-30 17:28:02 +00:00
|
|
|
content = extract_text(result.xpath(xpath_content))
|
2018-08-01 08:48:38 +00:00
|
|
|
|
2022-12-30 17:28:02 +00:00
|
|
|
results.append({'url': href, 'title': title, 'content': content})
|
2018-08-01 08:48:38 +00:00
|
|
|
|
|
|
|
return results
|