| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  |  Arch Linux Wiki | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 19:38:48 +01:00
										 |  |  |  @website      https://wiki.archlinux.org | 
					
						
							|  |  |  |  @provide-api  no (Mediawiki provides API, but Arch Wiki blocks access to it | 
					
						
							|  |  |  |  @using-api    no | 
					
						
							|  |  |  |  @results      HTML | 
					
						
							|  |  |  |  @stable       no (HTML can change) | 
					
						
							|  |  |  |  @parse        url, title | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode, urljoin | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2020-10-02 18:13:56 +02:00
										 |  |  | from searx.utils import extract_text | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							|  |  |  | categories = ['it'] | 
					
						
							|  |  |  | language_support = True | 
					
						
							|  |  |  | paging = True | 
					
						
							|  |  |  | base_url = 'https://wiki.archlinux.org' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # xpath queries | 
					
						
							|  |  |  | xpath_results = '//ul[@class="mw-search-results"]/li' | 
					
						
							|  |  |  | xpath_link = './/div[@class="mw-search-result-heading"]/a' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-20 22:47:20 +02:00
										 |  |  | # cut 'en' from 'en-US', 'de' from 'de-CH', and so on | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | def locale_to_lang_code(locale): | 
					
						
							| 
									
										
										
										
											2016-08-06 06:34:56 +02:00
										 |  |  |     if locale.find('-') >= 0: | 
					
						
							|  |  |  |         locale = locale.split('-')[0] | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  |     return locale | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 19:49:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | # 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 | 
					
						
							|  |  |  | lang_urls = { | 
					
						
							| 
									
										
										
										
											2019-01-06 15:27:46 +01:00
										 |  |  |     'all': { | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  |         '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 language | 
					
						
							|  |  |  | def get_lang_urls(language): | 
					
						
							|  |  |  |     if language in lang_urls: | 
					
						
							|  |  |  |         return lang_urls[language] | 
					
						
							| 
									
										
										
										
											2019-01-06 15:27:46 +01:00
										 |  |  |     return lang_urls['all'] | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 19:49:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | # 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': '简体中文' | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-06 06:34:56 +02:00
										 |  |  | supported_languages = dict(lang_urls, **main_langs) | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # do search-request | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  |     # translate the locale (e.g. 'en-US') to language code ('en') | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  |     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 16:25:03 +02:00
										 |  |  |         query += ' (' + main_langs[language] + ')' | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01: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'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = search_url.format(query=query, offset=offset) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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')) | 
					
						
							| 
									
										
										
										
											2016-12-09 11:44:24 +01:00
										 |  |  |         title = extract_text(link) | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         results.append({'url': href, | 
					
						
							| 
									
										
										
										
											2016-03-24 19:38:48 +01:00
										 |  |  |                         'title': title}) | 
					
						
							| 
									
										
										
										
											2016-03-23 20:57:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results |