| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | # SPDX-License-Identifier: AGPL-3.0-or-later""" Arch Linux Wiki API: Mediawiki provides API, but Arch Wiki blocks access to it"""from urllib.parse import urlencode, urljoinfrom lxml import htmlfrom searx.utils import extract_text, eval_xpath_list, eval_xpath_getindex# aboutabout = {    "website": 'https://wiki.archlinux.org/',    "wikidata_id": 'Q101445877',    "official_api_documentation": None,    "use_official_api": False,    "require_api_key": False,    "results": 'HTML',}# engine dependent configcategories = ['it']paging = Truebase_url = 'https://wiki.archlinux.org'# xpath queriesxpath_results = '//ul[@class="mw-search-results"]/li'xpath_link = './/div[@class="mw-search-result-heading"]/a'# cut 'en' from 'en-US', 'de' from 'de-CH', and so ondef 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 languageslang_urls = {    'all': {        'base': 'https://wiki.archlinux.org',        'search': '/index.php?title=Special:Search&offset={offset}&{query}'    },    'de': {        'base': 'https://wiki.archlinux.de',        'search': '/index.php?title=Spezial:Suche&offset={offset}&{query}'    },    'fr': {        'base': 'https://wiki.archlinux.fr',        'search': '/index.php?title=Spécial:Recherche&offset={offset}&{query}'    },    'ja': {        'base': 'https://wiki.archlinuxjp.org',        'search': '/index.php?title=特別:検索&offset={offset}&{query}'    },    'ro': {        'base': 'http://wiki.archlinux.ro',        'search': '/index.php?title=Special:Căutare&offset={offset}&{query}'    },    'tr': {        'base': 'http://archtr.org/wiki',        'search': '/index.php?title=Özel:Ara&offset={offset}&{query}'    }}# get base & search URLs for selected languagedef get_lang_urls(language):    if language in lang_urls:        return lang_urls[language]    return lang_urls['all']# 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': 'Українська',    'zh': '简体中文'}supported_languages = dict(lang_urls, **main_langs)# do search-requestdef 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:        query += ' (' + main_langs[language] + ')'    # 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']    params['url'] = search_url.format(query=query, offset=offset)    return params# get response from search-requestdef 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 eval_xpath_list(dom, xpath_results):        link = eval_xpath_getindex(result, xpath_link, 0)        href = urljoin(base_url, link.attrib.get('href'))        title = extract_text(link)        results.append({'url': href,                        'title': title})    return results
 |