on master help sign in
[fix] startpage engine: fetch CAPTCHA & issues related to PR-695

In case of CAPTCHA raise a SearxEngineCaptchaException and suspend for 7 days. When get_sc_code() fails raise a SearxEngineResponseException and suspend for 7 days. [1] https://github.com/searxng/searxng/pull/695 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commita4bc089091c65a502974519846823cfb2a726f51 authorMarkus Heiser <markus.heiser@darmarit.de> dateSun 9 Jan 2022, 16:05 (5 years ago) parent1076d7e
1 file changed+21−2
searx/engines/startpage.py+21−2
searx/engines/startpage.py+21−2
@@ -18,6 +18,11 @@ from babel.localedata import locale_identifiers
1818
1919from searx import network
2020from searx.utils import extract_text, eval_xpath, match_language
21+from searx.exceptions import (
22+ SearxEngineResponseException,
23+ SearxEngineCaptchaException,
24+)
25+
2126
2227# about
2328about = {
@@ -54,6 +59,13 @@ sc_code_ts = 0
5459sc_code = ''
5560
5661
62+def raise_captcha(resp):
63+
64+ if str(resp.url).startswith('https://www.startpage.com/sp/captcha'):
65+ # suspend CAPTCHA for 7 days
66+ raise SearxEngineCaptchaException(suspended_time=7 * 24 * 3600)
67+
68+
5769def get_sc_code(headers):
5870 """Get an actual `sc` argument from startpage's home page.
5971
@@ -73,10 +85,17 @@ def get_sc_code(headers):
7385 logger.debug("query new sc time-stamp ...")
7486
7587 resp = network.get(base_url, headers=headers)
88+ raise_captcha(resp)
7689 dom = html.fromstring(resp.text)
7790
78− # href --> '/?sc=adrKJMgF8xwp20'
79− href = eval_xpath(dom, '//a[@class="footer-home__logo"]')[0].get('href')
91+ try:
92+ # href --> '/?sc=adrKJMgF8xwp20'
93+ href = eval_xpath(dom, '//a[@class="footer-home__logo"]')[0].get('href')
94+ except IndexError as exc:
95+ # suspend startpage API --> https://github.com/searxng/searxng/pull/695
96+ raise SearxEngineResponseException(
97+ suspended_time=7 * 24 * 3600, message="PR-695: query new sc time-stamp failed!"
98+ )
8099
81100 sc_code = href[5:]
82101 sc_code_ts = time()