@@ -1,18 +1,30 @@
11# SPDX-License-Identifier: AGPL-3.0-or-later
2+# lint: pylint
3+"""Startpage (Web)
4+
25"""
3− Startpage (Web)
4−"""
6+
7+import re
8+from time import time
59
610from urllib.parse import urlencode
11+from unicodedata import normalize, combining
12+from datetime import datetime, timedelta
713
8−from lxml import html
914from dateutil import parser
10−from datetime import datetime, timedelta
11−import re
12−from unicodedata import normalize, combining
15+from lxml import html
1316from babel import Locale
1417from babel.localedata import locale_identifiers
18+
19+from searx import logger
20+from searx.network import get
1521from searx.utils import extract_text, eval_xpath, match_language
22+from searx.exceptions import (
23+ SearxEngineResponseException,
24+ SearxEngineCaptchaException,
25+)
26+
27+logger = logger.getChild('startpage')
1628
1729# about
1830about = {
@@ -44,16 +56,74 @@ results_xpath = '//div[@class="w-gl__result__main"]'
4456link_xpath = './/a[@class="w-gl__result-title result-link"]'
4557content_xpath = './/p[@class="w-gl__description"]'
4658
59+# timestamp of the last fetch of 'sc' code
60+sc_code_ts = 0
61+sc_code = ''
62+
63+
64+def raise_captcha(resp):
65+
66+ if str(resp.url).startswith('https://www.startpage.com/sp/captcha'):
67+ # suspend CAPTCHA for 7 days
68+ raise SearxEngineCaptchaException(suspended_time=7 * 24 * 3600)
69+
70+
71+def get_sc_code(headers):
72+ """Get an actual `sc` argument from startpage's home page.
73+
74+ Startpage puts a `sc` argument on every link. Without this argument
75+ startpage considers the request is from a bot. We do not know what is
76+ encoded in the value of the `sc` argument, but it seems to be a kind of a
77+ *time-stamp*. This *time-stamp* is valid for a few hours.
78+
79+ This function scrap a new *time-stamp* from startpage's home page every hour
80+ (3000 sec).
81+
82+ """
83+
84+ global sc_code_ts, sc_code # pylint: disable=global-statement
85+
86+ if time() > (sc_code_ts + 3000):
87+ logger.debug("query new sc time-stamp ...")
88+
89+ resp = get(base_url, headers=headers)
90+ raise_captcha(resp)
91+ dom = html.fromstring(resp.text)
92+
93+ try:
94+ # href --> '/?sc=adrKJMgF8xwp20'
95+ href = eval_xpath(dom, '//a[@class="footer-home__logo"]')[0].get('href')
96+ except IndexError as exc:
97+ # suspend startpage API --> https://github.com/searxng/searxng/pull/695
98+ raise SearxEngineResponseException(
99+ suspended_time=7 * 24 * 3600, message="PR-695: query new sc time-stamp failed!"
100+ ) from exc
101+
102+ sc_code = href[5:]
103+ sc_code_ts = time()
104+ logger.debug("new value is: %s", sc_code)
105+
106+ return sc_code
107+
47108
48109# do search-request
49110def request(query, params):
50111
112+ # pylint: disable=line-too-long
113+ # The format string from Startpage's FFox add-on [1]::
114+ #
115+ # https://www.startpage.com/do/dsearch?query={searchTerms}&cat=web&pl=ext-ff&language=__MSG_extensionUrlLanguage__&extVersion=1.3.0
116+ #
117+ # [1] https://addons.mozilla.org/en-US/firefox/addon/startpage-private-search/
118+
51119 args = {
52120 'query': query,
53121 'page': params['pageno'],
54122 'cat': 'web',
123+ # 'pl': 'ext-ff',
124+ # 'extVersion': '1.3.0',
55125 # 'abp': "-1",
56− 'sc': 'Mj4jZy61QETj20',
126+ 'sc': get_sc_code(params['headers']),
57127 }
58128
59129 # set language if specified
@@ -140,10 +210,11 @@ def response(resp):
140210
141211# get supported languages from their site
142212def _fetch_supported_languages(resp):
143− # startpage's language selector is a mess
144− # each option has a displayed name and a value, either of which may represent the language name
145− # in the native script, the language name in English, an English transliteration of the native name,
146− # the English name of the writing script used by the language, or occasionally something else entirely.
213+ # startpage's language selector is a mess each option has a displayed name
214+ # and a value, either of which may represent the language name in the native
215+ # script, the language name in English, an English transliteration of the
216+ # native name, the English name of the writing script used by the language,
217+ # or occasionally something else entirely.
147218
148219 # this cases are so special they need to be hardcoded, a couple of them are mispellings
149220 language_names = {
@@ -157,7 +228,15 @@ def _fetch_supported_languages(resp):
157228 }
158229
159230 # get the English name of every language known by babel
160− language_names.update({name.lower(): lang_code for lang_code, name in Locale('en')._data['languages'].items()})
231+ language_names.update(
232+ {
233+ # fmt: off
234+ name.lower(): lang_code
235+ # pylint: disable=protected-access
236+ for lang_code, name in Locale('en')._data['languages'].items()
237+ # fmt: on
238+ }
239+ )
161240
162241 # get the native name of every language known by babel
163242 for lang_code in filter(lambda lang_code: lang_code.find('_') == -1, locale_identifiers()):
@@ -182,8 +261,8 @@ def _fetch_supported_languages(resp):
182261 if isinstance(lang_code, str):
183262 supported_languages[lang_code] = {'alias': sp_option_value}
184263 elif isinstance(lang_code, list):
185− for lc in lang_code:
186− supported_languages[lc] = {'alias': sp_option_value}
264+ for _lc in lang_code:
265+ supported_languages[_lc] = {'alias': sp_option_value}
187266 else:
188267 print('Unknown language option in Startpage: {} ({})'.format(sp_option_value, sp_option_text))
189268