| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | """Yahoo Search (Web)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Languages are supported by mapping the language to a domain.  If domain is not | 
					
						
							|  |  |  | found in :py:obj:`lang2domain` URL ``<lang>.search.yahoo.com`` is used. | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | from urllib.parse import ( | 
					
						
							|  |  |  |     unquote, | 
					
						
							|  |  |  |     urlencode, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from searx.utils import ( | 
					
						
							|  |  |  |     eval_xpath_getindex, | 
					
						
							|  |  |  |     eval_xpath_list, | 
					
						
							|  |  |  |     extract_text, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  | from searx.enginelib.traits import EngineTraits | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | traits: EngineTraits | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://search.yahoo.com/', | 
					
						
							|  |  |  |     "wikidata_id": None, | 
					
						
							|  |  |  |     "official_api_documentation": 'https://developer.yahoo.com/api/', | 
					
						
							|  |  |  |     "use_official_api": False, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'HTML', | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2021-12-22 16:58:52 +01:00
										 |  |  | categories = ['general', 'web'] | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2016-07-17 18:42:30 +02:00
										 |  |  | time_range_support = True | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  | # send_accept_language_header = True | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  | time_range_dict = { | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     'day': ('1d', 'd'), | 
					
						
							|  |  |  |     'week': ('1w', 'w'), | 
					
						
							|  |  |  |     'month': ('1m', 'm'), | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-01 05:30:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | lang2domain = { | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     'zh_chs': 'hk.search.yahoo.com', | 
					
						
							|  |  |  |     'zh_cht': 'tw.search.yahoo.com', | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  |     'any': 'search.yahoo.com', | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     'en': 'search.yahoo.com', | 
					
						
							|  |  |  |     'bg': 'search.yahoo.com', | 
					
						
							|  |  |  |     'cs': 'search.yahoo.com', | 
					
						
							|  |  |  |     'da': 'search.yahoo.com', | 
					
						
							|  |  |  |     'el': 'search.yahoo.com', | 
					
						
							|  |  |  |     'et': 'search.yahoo.com', | 
					
						
							|  |  |  |     'he': 'search.yahoo.com', | 
					
						
							|  |  |  |     'hr': 'search.yahoo.com', | 
					
						
							|  |  |  |     'ja': 'search.yahoo.com', | 
					
						
							|  |  |  |     'ko': 'search.yahoo.com', | 
					
						
							|  |  |  |     'sk': 'search.yahoo.com', | 
					
						
							|  |  |  |     'sl': 'search.yahoo.com', | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | """Map language to domain""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  | locale_aliases = { | 
					
						
							|  |  |  |     'zh': 'zh_Hans', | 
					
						
							|  |  |  |     'zh-HK': 'zh_Hans', | 
					
						
							|  |  |  |     'zh-CN': 'zh_Hans',  # dead since 2015 / routed to hk.search.yahoo.com | 
					
						
							|  |  |  |     'zh-TW': 'zh_Hant', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | def request(query, params): | 
					
						
							|  |  |  |     """build request""" | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     lang = locale_aliases.get(params['language'], None) | 
					
						
							|  |  |  |     if not lang: | 
					
						
							|  |  |  |         lang = params['language'].split('-')[0] | 
					
						
							|  |  |  |     lang = traits.get_language(lang, traits.all_locale) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     offset = (params['pageno'] - 1) * 7 + 1 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     age, btf = time_range_dict.get(params['time_range'], ('', '')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     args = urlencode( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             'p': query, | 
					
						
							|  |  |  |             'ei': 'UTF-8', | 
					
						
							|  |  |  |             'fl': 1, | 
					
						
							|  |  |  |             'vl': 'lang_' + lang, | 
					
						
							|  |  |  |             'btf': btf, | 
					
						
							|  |  |  |             'fr2': 'time', | 
					
						
							|  |  |  |             'age': age, | 
					
						
							|  |  |  |             'b': offset, | 
					
						
							|  |  |  |             'xargs': 0, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     domain = lang2domain.get(lang, '%s.search.yahoo.com' % lang) | 
					
						
							|  |  |  |     params['url'] = 'https://%s/search?%s' % (domain, args) | 
					
						
							|  |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 19:09:03 +01:00
										 |  |  | def parse_url(url_string): | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     """remove yahoo-specific tracking-url""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 10:06:14 +01:00
										 |  |  |     endings = ['/RS', '/RK'] | 
					
						
							|  |  |  |     endpositions = [] | 
					
						
							| 
									
										
										
										
											2015-02-04 20:41:40 +01:00
										 |  |  |     start = url_string.find('http', url_string.find('/RU=') + 1) | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 10:06:14 +01:00
										 |  |  |     for ending in endings: | 
					
						
							|  |  |  |         endpos = url_string.rfind(ending) | 
					
						
							|  |  |  |         if endpos > -1: | 
					
						
							|  |  |  |             endpositions.append(endpos) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:37:56 +01:00
										 |  |  |     if start == 0 or len(endpositions) == 0: | 
					
						
							|  |  |  |         return url_string | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  |     end = min(endpositions) | 
					
						
							|  |  |  |     return unquote(url_string[start:end]) | 
					
						
							| 
									
										
										
										
											2014-03-10 16:46:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     """parse response""" | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  |     # parse results | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     for result in eval_xpath_list(dom, '//div[contains(@class,"algo-sr")]'): | 
					
						
							|  |  |  |         url = eval_xpath_getindex(result, './/h3/a/@href', 0, default=None) | 
					
						
							|  |  |  |         if url is None: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         url = parse_url(url) | 
					
						
							| 
									
										
										
										
											2021-10-16 13:05:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |         title = eval_xpath_getindex(result, './/h3/a', 0, default=None) | 
					
						
							|  |  |  |         if title is None: | 
					
						
							| 
									
										
										
										
											2014-03-21 16:36:13 +01:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |         offset = len(extract_text(title.xpath('span'))) | 
					
						
							|  |  |  |         title = extract_text(title)[offset:] | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         content = eval_xpath_getindex(result, './/div[contains(@class, "compText")]', 0, default='') | 
					
						
							| 
									
										
										
										
											2022-04-15 14:14:09 +02:00
										 |  |  |         content = extract_text(content, allow_none=True) | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  |         # append result | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         results.append({'url': url, 'title': title, 'content': content}) | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 09:38:04 +02:00
										 |  |  |     for suggestion in eval_xpath_list(dom, '//div[contains(@class, "AlsoTry")]//table//a'): | 
					
						
							| 
									
										
										
										
											2014-09-01 16:17:29 +02:00
										 |  |  |         # append suggestion | 
					
						
							| 
									
										
										
										
											2014-01-30 02:33:24 +01:00
										 |  |  |         results.append({'suggestion': extract_text(suggestion)}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results | 
					
						
							| 
									
										
										
										
											2016-11-06 03:51:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  | def fetch_traits(engine_traits: EngineTraits): | 
					
						
							|  |  |  |     """Fetch languages from yahoo""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # pylint: disable=import-outside-toplevel | 
					
						
							|  |  |  |     import babel | 
					
						
							|  |  |  |     from searx import network | 
					
						
							|  |  |  |     from searx.locales import language_tag | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     engine_traits.all_locale = 'any' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = network.get('https://search.yahoo.com/preferences/languages') | 
					
						
							|  |  |  |     if not resp.ok: | 
					
						
							|  |  |  |         print("ERROR: response from peertube is not OK.") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     offset = len('lang_') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  |     eng2sxng = {'zh_chs': 'zh_Hans', 'zh_cht': 'zh_Hant'} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 21:56:08 +02:00
										 |  |  |     for val in eval_xpath_list(dom, '//div[contains(@class, "lang-item")]/input/@value'): | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  |         eng_tag = val[offset:] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             sxng_tag = language_tag(babel.Locale.parse(eng2sxng.get(eng_tag, eng_tag))) | 
					
						
							|  |  |  |         except babel.UnknownLocaleError: | 
					
						
							|  |  |  |             print('ERROR: unknown language --> %s' % eng_tag) | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2016-11-06 03:51:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 00:30:26 +02:00
										 |  |  |         conflict = engine_traits.languages.get(sxng_tag) | 
					
						
							|  |  |  |         if conflict: | 
					
						
							|  |  |  |             if conflict != eng_tag: | 
					
						
							|  |  |  |                 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag)) | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         engine_traits.languages[sxng_tag] = eng_tag |