startpage.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Startpage's language & region selectors are a mess ..
  3. .. _startpage regions:
  4. Startpage regions
  5. =================
  6. In the list of regions there are tags we need to map to common region tags::
  7. pt-BR_BR --> pt_BR
  8. zh-CN_CN --> zh_Hans_CN
  9. zh-TW_TW --> zh_Hant_TW
  10. zh-TW_HK --> zh_Hant_HK
  11. en-GB_GB --> en_GB
  12. and there is at least one tag with a three letter language tag (ISO 639-2)::
  13. fil_PH --> fil_PH
  14. The locale code ``no_NO`` from Startpage does not exists and is mapped to
  15. ``nb-NO``::
  16. babel.core.UnknownLocaleError: unknown locale 'no_NO'
  17. For reference see languages-subtag at iana; ``no`` is the macrolanguage [1]_ and
  18. W3C recommends subtag over macrolanguage [2]_.
  19. .. [1] `iana: language-subtag-registry
  20. <https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry>`_ ::
  21. type: language
  22. Subtag: nb
  23. Description: Norwegian Bokmål
  24. Added: 2005-10-16
  25. Suppress-Script: Latn
  26. Macrolanguage: no
  27. .. [2]
  28. Use macrolanguages with care. Some language subtags have a Scope field set to
  29. macrolanguage, i.e. this primary language subtag encompasses a number of more
  30. specific primary language subtags in the registry. ... As we recommended for
  31. the collection subtags mentioned above, in most cases you should try to use
  32. the more specific subtags ... `W3: The primary language subtag
  33. <https://www.w3.org/International/questions/qa-choosing-language-tags#langsubtag>`_
  34. .. _startpage languages:
  35. Startpage languages
  36. ===================
  37. :py:obj:`send_accept_language_header`:
  38. The displayed name in Startpage's settings page depend on the location of the
  39. IP when ``Accept-Language`` HTTP header is unset. In :py:obj:`fetch_traits`
  40. we use::
  41. 'Accept-Language': "en-US,en;q=0.5",
  42. ..
  43. to get uniform names independent from the IP).
  44. .. _startpage categories:
  45. Startpage categories
  46. ====================
  47. Startpage's category (for Web-search, News, Videos, ..) is set by
  48. :py:obj:`startpage_categ` in settings.yml::
  49. - name: startpage
  50. engine: startpage
  51. startpage_categ: web
  52. ...
  53. .. hint::
  54. The default category is ``web`` .. and other categories than ``web`` are not
  55. yet implemented.
  56. """
  57. from typing import TYPE_CHECKING
  58. from collections import OrderedDict
  59. import re
  60. from unicodedata import normalize, combining
  61. from time import time
  62. from datetime import datetime, timedelta
  63. import dateutil.parser
  64. import lxml.html
  65. import babel
  66. from searx.utils import extract_text, eval_xpath, gen_useragent
  67. from searx.network import get # see https://github.com/searxng/searxng/issues/762
  68. from searx.exceptions import SearxEngineCaptchaException
  69. from searx.locales import region_tag
  70. from searx.enginelib.traits import EngineTraits
  71. if TYPE_CHECKING:
  72. import logging
  73. logger: logging.Logger
  74. traits: EngineTraits
  75. # about
  76. about = {
  77. "website": 'https://startpage.com',
  78. "wikidata_id": 'Q2333295',
  79. "official_api_documentation": None,
  80. "use_official_api": False,
  81. "require_api_key": False,
  82. "results": 'HTML',
  83. }
  84. startpage_categ = 'web'
  85. """Startpage's category, visit :ref:`startpage categories`.
  86. """
  87. send_accept_language_header = True
  88. """Startpage tries to guess user's language and territory from the HTTP
  89. ``Accept-Language``. Optional the user can select a search-language (can be
  90. different to the UI language) and a region filter.
  91. """
  92. # engine dependent config
  93. categories = ['general', 'web']
  94. paging = True
  95. max_page = 18
  96. """Tested 18 pages maximum (argument ``page``), to be save max is set to 20."""
  97. time_range_support = True
  98. safesearch = True
  99. time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'}
  100. safesearch_dict = {0: '0', 1: '1', 2: '1'}
  101. # search-url
  102. base_url = 'https://www.startpage.com'
  103. search_url = base_url + '/sp/search'
  104. # specific xpath variables
  105. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  106. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  107. search_form_xpath = '//form[@id="search"]'
  108. """XPath of Startpage's origin search form
  109. .. code: html
  110. <form action="/sp/search" method="post">
  111. <input type="text" name="query" value="" ..>
  112. <input type="hidden" name="t" value="device">
  113. <input type="hidden" name="lui" value="english">
  114. <input type="hidden" name="sc" value="Q7Mt5TRqowKB00">
  115. <input type="hidden" name="cat" value="web">
  116. <input type="hidden" class="abp" id="abp-input" name="abp" value="1">
  117. </form>
  118. """
  119. # timestamp of the last fetch of 'sc' code
  120. sc_code_ts = 0
  121. sc_code = ''
  122. sc_code_cache_sec = 30
  123. """Time in seconds the sc-code is cached in memory :py:obj:`get_sc_code`."""
  124. def get_sc_code(searxng_locale, params):
  125. """Get an actual ``sc`` argument from Startpage's search form (HTML page).
  126. Startpage puts a ``sc`` argument on every HTML :py:obj:`search form
  127. <search_form_xpath>`. Without this argument Startpage considers the request
  128. is from a bot. We do not know what is encoded in the value of the ``sc``
  129. argument, but it seems to be a kind of a *time-stamp*.
  130. Startpage's search form generates a new sc-code on each request. This
  131. function scrap a new sc-code from Startpage's home page every
  132. :py:obj:`sc_code_cache_sec` seconds.
  133. """
  134. global sc_code_ts, sc_code # pylint: disable=global-statement
  135. if sc_code and (time() < (sc_code_ts + sc_code_cache_sec)):
  136. logger.debug("get_sc_code: reuse '%s'", sc_code)
  137. return sc_code
  138. headers = {**params['headers']}
  139. headers['Origin'] = base_url
  140. headers['Referer'] = base_url + '/'
  141. # headers['Connection'] = 'keep-alive'
  142. # headers['Accept-Encoding'] = 'gzip, deflate, br'
  143. # headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
  144. # headers['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0'
  145. # add Accept-Language header
  146. if searxng_locale == 'all':
  147. searxng_locale = 'en-US'
  148. locale = babel.Locale.parse(searxng_locale, sep='-')
  149. if send_accept_language_header:
  150. ac_lang = locale.language
  151. if locale.territory:
  152. ac_lang = "%s-%s,%s;q=0.9,*;q=0.5" % (
  153. locale.language,
  154. locale.territory,
  155. locale.language,
  156. )
  157. headers['Accept-Language'] = ac_lang
  158. get_sc_url = base_url + '/?sc=%s' % (sc_code)
  159. logger.debug("query new sc time-stamp ... %s", get_sc_url)
  160. logger.debug("headers: %s", headers)
  161. resp = get(get_sc_url, headers=headers)
  162. # ?? x = network.get('https://www.startpage.com/sp/cdn/images/filter-chevron.svg', headers=headers)
  163. # ?? https://www.startpage.com/sp/cdn/images/filter-chevron.svg
  164. # ?? ping-back URL: https://www.startpage.com/sp/pb?sc=TLsB0oITjZ8F21
  165. if str(resp.url).startswith('https://www.startpage.com/sp/captcha'): # type: ignore
  166. raise SearxEngineCaptchaException(
  167. message="get_sc_code: got redirected to https://www.startpage.com/sp/captcha",
  168. )
  169. dom = lxml.html.fromstring(resp.text) # type: ignore
  170. try:
  171. sc_code = eval_xpath(dom, search_form_xpath + '//input[@name="sc"]/@value')[0]
  172. except IndexError as exc:
  173. logger.debug("suspend startpage API --> https://github.com/searxng/searxng/pull/695")
  174. raise SearxEngineCaptchaException(
  175. message="get_sc_code: [PR-695] query new sc time-stamp failed! (%s)" % resp.url, # type: ignore
  176. ) from exc
  177. sc_code_ts = time()
  178. logger.debug("get_sc_code: new value is: %s", sc_code)
  179. return sc_code
  180. def request(query, params):
  181. """Assemble a Startpage request.
  182. To avoid CAPTCHA we need to send a well formed HTTP POST request with a
  183. cookie. We need to form a request that is identical to the request build by
  184. Startpage's search form:
  185. - in the cookie the **region** is selected
  186. - in the HTTP POST data the **language** is selected
  187. Additionally the arguments form Startpage's search form needs to be set in
  188. HTML POST data / compare ``<input>`` elements: :py:obj:`search_form_xpath`.
  189. """
  190. if startpage_categ == 'web':
  191. return _request_cat_web(query, params)
  192. logger.error("Startpages's category '%' is not yet implemented.", startpage_categ)
  193. return params
  194. def _request_cat_web(query, params):
  195. engine_region = traits.get_region(params['searxng_locale'], 'en-US')
  196. engine_language = traits.get_language(params['searxng_locale'], 'en')
  197. # build arguments
  198. args = {
  199. 'query': query,
  200. 'cat': 'web',
  201. 't': 'device',
  202. 'sc': get_sc_code(params['searxng_locale'], params), # hint: this func needs HTTP headers,
  203. 'with_date': time_range_dict.get(params['time_range'], ''),
  204. }
  205. if engine_language:
  206. args['language'] = engine_language
  207. args['lui'] = engine_language
  208. args['abp'] = '1'
  209. if params['pageno'] > 1:
  210. args['page'] = params['pageno']
  211. # build cookie
  212. lang_homepage = 'en'
  213. cookie = OrderedDict()
  214. cookie['date_time'] = 'world'
  215. cookie['disable_family_filter'] = safesearch_dict[params['safesearch']]
  216. cookie['disable_open_in_new_window'] = '0'
  217. cookie['enable_post_method'] = '1' # hint: POST
  218. cookie['enable_proxy_safety_suggest'] = '1'
  219. cookie['enable_stay_control'] = '1'
  220. cookie['instant_answers'] = '1'
  221. cookie['lang_homepage'] = 's/device/%s/' % lang_homepage
  222. cookie['num_of_results'] = '10'
  223. cookie['suggestions'] = '1'
  224. cookie['wt_unit'] = 'celsius'
  225. if engine_language:
  226. cookie['language'] = engine_language
  227. cookie['language_ui'] = engine_language
  228. if engine_region:
  229. cookie['search_results_region'] = engine_region
  230. params['cookies']['preferences'] = 'N1N'.join(["%sEEE%s" % x for x in cookie.items()])
  231. logger.debug('cookie preferences: %s', params['cookies']['preferences'])
  232. # POST request
  233. logger.debug("data: %s", args)
  234. params['data'] = args
  235. params['method'] = 'POST'
  236. params['url'] = search_url
  237. params['headers']['Origin'] = base_url
  238. params['headers']['Referer'] = base_url + '/'
  239. # is the Accept header needed?
  240. # params['headers']['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
  241. return params
  242. # get response from search-request
  243. def response(resp):
  244. dom = lxml.html.fromstring(resp.text)
  245. if startpage_categ == 'web':
  246. return _response_cat_web(dom)
  247. logger.error("Startpages's category '%' is not yet implemented.", startpage_categ)
  248. return []
  249. def _response_cat_web(dom):
  250. results = []
  251. # parse results
  252. for result in eval_xpath(dom, '//div[@class="w-gl"]/div[contains(@class, "result")]'):
  253. links = eval_xpath(result, './/a[contains(@class, "result-title result-link")]')
  254. if not links:
  255. continue
  256. link = links[0]
  257. url = link.attrib.get('href')
  258. # block google-ad url's
  259. if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
  260. continue
  261. # block startpage search url's
  262. if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
  263. continue
  264. title = extract_text(eval_xpath(link, 'h2'))
  265. content = eval_xpath(result, './/p[contains(@class, "description")]')
  266. content = extract_text(content, allow_none=True) or ''
  267. published_date = None
  268. # check if search result starts with something like: "2 Sep 2014 ... "
  269. if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
  270. date_pos = content.find('...') + 4
  271. date_string = content[0 : date_pos - 5]
  272. # fix content string
  273. content = content[date_pos:]
  274. try:
  275. published_date = dateutil.parser.parse(date_string, dayfirst=True)
  276. except ValueError:
  277. pass
  278. # check if search result starts with something like: "5 days ago ... "
  279. elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
  280. date_pos = content.find('...') + 4
  281. date_string = content[0 : date_pos - 5]
  282. # calculate datetime
  283. published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group())) # type: ignore
  284. # fix content string
  285. content = content[date_pos:]
  286. if published_date:
  287. # append result
  288. results.append({'url': url, 'title': title, 'content': content, 'publishedDate': published_date})
  289. else:
  290. # append result
  291. results.append({'url': url, 'title': title, 'content': content})
  292. # return results
  293. return results
  294. def fetch_traits(engine_traits: EngineTraits):
  295. """Fetch :ref:`languages <startpage languages>` and :ref:`regions <startpage
  296. regions>` from Startpage."""
  297. # pylint: disable=too-many-branches
  298. headers = {
  299. 'User-Agent': gen_useragent(),
  300. 'Accept-Language': "en-US,en;q=0.5", # bing needs to set the English language
  301. }
  302. resp = get('https://www.startpage.com/do/settings', headers=headers)
  303. if not resp.ok: # type: ignore
  304. print("ERROR: response from Startpage is not OK.")
  305. dom = lxml.html.fromstring(resp.text) # type: ignore
  306. # regions
  307. sp_region_names = []
  308. for option in dom.xpath('//form[@name="settings"]//select[@name="search_results_region"]/option'):
  309. sp_region_names.append(option.get('value'))
  310. for eng_tag in sp_region_names:
  311. if eng_tag == 'all':
  312. continue
  313. babel_region_tag = {'no_NO': 'nb_NO'}.get(eng_tag, eng_tag) # norway
  314. if '-' in babel_region_tag:
  315. l, r = babel_region_tag.split('-')
  316. r = r.split('_')[-1]
  317. sxng_tag = region_tag(babel.Locale.parse(l + '_' + r, sep='_'))
  318. else:
  319. try:
  320. sxng_tag = region_tag(babel.Locale.parse(babel_region_tag, sep='_'))
  321. except babel.UnknownLocaleError:
  322. print("ERROR: can't determine babel locale of startpage's locale %s" % eng_tag)
  323. continue
  324. conflict = engine_traits.regions.get(sxng_tag)
  325. if conflict:
  326. if conflict != eng_tag:
  327. print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
  328. continue
  329. engine_traits.regions[sxng_tag] = eng_tag
  330. # languages
  331. catalog_engine2code = {name.lower(): lang_code for lang_code, name in babel.Locale('en').languages.items()}
  332. # get the native name of every language known by babel
  333. for lang_code in filter(
  334. lambda lang_code: lang_code.find('_') == -1, babel.localedata.locale_identifiers() # type: ignore
  335. ):
  336. native_name = babel.Locale(lang_code).get_language_name().lower() # type: ignore
  337. # add native name exactly as it is
  338. catalog_engine2code[native_name] = lang_code
  339. # add "normalized" language name (i.e. français becomes francais and español becomes espanol)
  340. unaccented_name = ''.join(filter(lambda c: not combining(c), normalize('NFKD', native_name)))
  341. if len(unaccented_name) == len(unaccented_name.encode()):
  342. # add only if result is ascii (otherwise "normalization" didn't work)
  343. catalog_engine2code[unaccented_name] = lang_code
  344. # values that can't be determined by babel's languages names
  345. catalog_engine2code.update(
  346. {
  347. # traditional chinese used in ..
  348. 'fantizhengwen': 'zh_Hant',
  349. # Korean alphabet
  350. 'hangul': 'ko',
  351. # Malayalam is one of 22 scheduled languages of India.
  352. 'malayam': 'ml',
  353. 'norsk': 'nb',
  354. 'sinhalese': 'si',
  355. }
  356. )
  357. skip_eng_tags = {
  358. 'english_uk', # SearXNG lang 'en' already maps to 'english'
  359. }
  360. for option in dom.xpath('//form[@name="settings"]//select[@name="language"]/option'):
  361. eng_tag = option.get('value')
  362. if eng_tag in skip_eng_tags:
  363. continue
  364. name = extract_text(option).lower() # type: ignore
  365. sxng_tag = catalog_engine2code.get(eng_tag)
  366. if sxng_tag is None:
  367. sxng_tag = catalog_engine2code[name]
  368. conflict = engine_traits.languages.get(sxng_tag)
  369. if conflict:
  370. if conflict != eng_tag:
  371. print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
  372. continue
  373. engine_traits.languages[sxng_tag] = eng_tag