yahoo.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Yahoo Search (Web)
  3. Languages are supported by mapping the language to a domain. If domain is not
  4. found in :py:obj:`lang2domain` URL ``<lang>.search.yahoo.com`` is used.
  5. """
  6. from urllib.parse import (
  7. unquote,
  8. urlencode,
  9. )
  10. from lxml import html
  11. from searx.utils import (
  12. eval_xpath_getindex,
  13. eval_xpath_list,
  14. extract_text,
  15. html_to_text,
  16. )
  17. from searx.enginelib.traits import EngineTraits
  18. traits: EngineTraits
  19. # about
  20. about = {
  21. "website": 'https://search.yahoo.com/',
  22. "wikidata_id": None,
  23. "official_api_documentation": 'https://developer.yahoo.com/api/',
  24. "use_official_api": False,
  25. "require_api_key": False,
  26. "results": 'HTML',
  27. }
  28. # engine dependent config
  29. categories = ['general', 'web']
  30. paging = True
  31. time_range_support = True
  32. # send_accept_language_header = True
  33. time_range_dict = {
  34. 'day': ('1d', 'd'),
  35. 'week': ('1w', 'w'),
  36. 'month': ('1m', 'm'),
  37. }
  38. region2domain = {
  39. "CO": "co.search.yahoo.com", # Colombia
  40. "TH": "th.search.yahoo.com", # Thailand
  41. "VE": "ve.search.yahoo.com", # Venezuela
  42. "CL": "cl.search.yahoo.com", # Chile
  43. "HK": "hk.search.yahoo.com", # Hong Kong
  44. "PE": "pe.search.yahoo.com", # Peru
  45. "CA": "ca.search.yahoo.com", # Canada
  46. "DE": "de.search.yahoo.com", # Germany
  47. "FR": "fr.search.yahoo.com", # France
  48. "TW": "tw.search.yahoo.com", # Taiwan
  49. "GB": "uk.search.yahoo.com", # United Kingdom
  50. "UK": "uk.search.yahoo.com",
  51. "BR": "br.search.yahoo.com", # Brazil
  52. "IN": "in.search.yahoo.com", # India
  53. "ES": "espanol.search.yahoo.com", # Espanol
  54. "PH": "ph.search.yahoo.com", # Philippines
  55. "AR": "ar.search.yahoo.com", # Argentina
  56. "MX": "mx.search.yahoo.com", # Mexico
  57. "SG": "sg.search.yahoo.com", # Singapore
  58. }
  59. """Map regions to domain"""
  60. lang2domain = {
  61. 'zh_chs': 'hk.search.yahoo.com',
  62. 'zh_cht': 'tw.search.yahoo.com',
  63. 'any': 'search.yahoo.com',
  64. 'en': 'search.yahoo.com',
  65. 'bg': 'search.yahoo.com',
  66. 'cs': 'search.yahoo.com',
  67. 'da': 'search.yahoo.com',
  68. 'el': 'search.yahoo.com',
  69. 'et': 'search.yahoo.com',
  70. 'he': 'search.yahoo.com',
  71. 'hr': 'search.yahoo.com',
  72. 'ja': 'search.yahoo.com',
  73. 'ko': 'search.yahoo.com',
  74. 'sk': 'search.yahoo.com',
  75. 'sl': 'search.yahoo.com',
  76. }
  77. """Map language to domain"""
  78. yahoo_languages = {
  79. "all": "any",
  80. "ar": "ar", # Arabic
  81. "bg": "bg", # Bulgarian
  82. "cs": "cs", # Czech
  83. "da": "da", # Danish
  84. "de": "de", # German
  85. "el": "el", # Greek
  86. "en": "en", # English
  87. "es": "es", # Spanish
  88. "et": "et", # Estonian
  89. "fi": "fi", # Finnish
  90. "fr": "fr", # French
  91. "he": "he", # Hebrew
  92. "hr": "hr", # Croatian
  93. "hu": "hu", # Hungarian
  94. "it": "it", # Italian
  95. "ja": "ja", # Japanese
  96. "ko": "ko", # Korean
  97. "lt": "lt", # Lithuanian
  98. "lv": "lv", # Latvian
  99. "nl": "nl", # Dutch
  100. "no": "no", # Norwegian
  101. "pl": "pl", # Polish
  102. "pt": "pt", # Portuguese
  103. "ro": "ro", # Romanian
  104. "ru": "ru", # Russian
  105. "sk": "sk", # Slovak
  106. "sl": "sl", # Slovenian
  107. "sv": "sv", # Swedish
  108. "th": "th", # Thai
  109. "tr": "tr", # Turkish
  110. "zh": "zh_chs", # Chinese (Simplified)
  111. "zh_Hans": "zh_chs",
  112. 'zh-CN': "zh_chs",
  113. "zh_Hant": "zh_cht", # Chinese (Traditional)
  114. "zh-HK": "zh_cht",
  115. 'zh-TW': "zh_cht",
  116. }
  117. def request(query, params):
  118. """build request"""
  119. lang, region = (params["language"].split("-") + [None])[:2]
  120. lang = yahoo_languages.get(lang, "any")
  121. offset = (params['pageno'] - 1) * 7 + 1
  122. age, btf = time_range_dict.get(params['time_range'], ('', ''))
  123. args = urlencode(
  124. {
  125. 'p': query,
  126. 'ei': 'UTF-8',
  127. 'fl': 1,
  128. 'vl': 'lang_' + lang,
  129. 'btf': btf,
  130. 'fr2': 'time',
  131. 'age': age,
  132. 'b': offset,
  133. 'xargs': 0,
  134. }
  135. )
  136. domain = region2domain.get(region)
  137. if not domain:
  138. domain = lang2domain.get(lang, '%s.search.yahoo.com' % lang)
  139. params['url'] = 'https://%s/search?%s' % (domain, args)
  140. params['domain'] = domain
  141. def parse_url(url_string):
  142. """remove yahoo-specific tracking-url"""
  143. endings = ['/RS', '/RK']
  144. endpositions = []
  145. start = url_string.find('http', url_string.find('/RU=') + 1)
  146. for ending in endings:
  147. endpos = url_string.rfind(ending)
  148. if endpos > -1:
  149. endpositions.append(endpos)
  150. if start == 0 or len(endpositions) == 0:
  151. return url_string
  152. end = min(endpositions)
  153. return unquote(url_string[start:end])
  154. def response(resp):
  155. """parse response"""
  156. results = []
  157. dom = html.fromstring(resp.text)
  158. url_xpath = './/div[contains(@class,"compTitle")]/h3/a/@href'
  159. title_xpath = './/h3//a/@aria-label'
  160. domain = resp.search_params['domain']
  161. if domain == "search.yahoo.com":
  162. url_xpath = './/div[contains(@class,"compTitle")]/a/@href'
  163. title_xpath = './/div[contains(@class,"compTitle")]/a/h3/span'
  164. # parse results
  165. for result in eval_xpath_list(dom, '//div[contains(@class,"algo-sr")]'):
  166. url = eval_xpath_getindex(result, url_xpath, 0, default=None)
  167. if url is None:
  168. continue
  169. url = parse_url(url)
  170. title = eval_xpath_getindex(result, title_xpath, 0, default='')
  171. title: str = extract_text(title)
  172. content = eval_xpath_getindex(result, './/div[contains(@class, "compText")]', 0, default='')
  173. content: str = extract_text(content, allow_none=True)
  174. # append result
  175. results.append(
  176. {
  177. 'url': url,
  178. # title sometimes contains HTML tags / see
  179. # https://github.com/searxng/searxng/issues/3790
  180. 'title': " ".join(html_to_text(title).strip().split()),
  181. 'content': " ".join(html_to_text(content).strip().split()),
  182. }
  183. )
  184. for suggestion in eval_xpath_list(dom, '//div[contains(@class, "AlsoTry")]//table//a'):
  185. # append suggestion
  186. results.append({'suggestion': extract_text(suggestion)})
  187. return results