mirror of
https://github.com/searxng/searxng.git
synced 2025-05-24 01:12:56 -04:00
[fix] duckduckgo engines: issue when get_vqd() is used by ddg-images and ddg-videos (#4809)
The global variable CACHE is not initialized when DDG images or DDG videos import the get_vqd() function (please remember: the engine modules are imported using the importlib method and not via the `import` keyword). Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
parent
749de829d5
commit
ca67f1dffe
@ -63,14 +63,16 @@ url = "https://html.duckduckgo.com/html"
|
|||||||
time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'}
|
time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'}
|
||||||
form_data = {'v': 'l', 'api': 'd.js', 'o': 'json'}
|
form_data = {'v': 'l', 'api': 'd.js', 'o': 'json'}
|
||||||
|
|
||||||
CACHE: EngineCache
|
_CACHE: EngineCache = None # type: ignore
|
||||||
"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
|
"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
|
||||||
seconds."""
|
seconds."""
|
||||||
|
|
||||||
|
|
||||||
def init(_): # pylint: disable=unused-argument
|
def get_cache():
|
||||||
global CACHE # pylint: disable=global-statement
|
global _CACHE # pylint: disable=global-statement
|
||||||
CACHE = EngineCache("duckduckgo") # type:ignore
|
if _CACHE is None:
|
||||||
|
_CACHE = EngineCache("duckduckgo") # type:ignore
|
||||||
|
return _CACHE
|
||||||
|
|
||||||
|
|
||||||
def get_vqd(query: str, region: str, force_request: bool = False) -> str:
|
def get_vqd(query: str, region: str, force_request: bool = False) -> str:
|
||||||
@ -105,8 +107,9 @@ def get_vqd(query: str, region: str, force_request: bool = False) -> str:
|
|||||||
seems the block list is a sliding window: to get my IP rid from the bot list
|
seems the block list is a sliding window: to get my IP rid from the bot list
|
||||||
I had to cool down my IP for 1h (send no requests from that IP to DDG).
|
I had to cool down my IP for 1h (send no requests from that IP to DDG).
|
||||||
"""
|
"""
|
||||||
key = CACHE.secret_hash(f"{query}//{region}")
|
cache = get_cache()
|
||||||
value = CACHE.get(key=key)
|
key = cache.secret_hash(f"{query}//{region}")
|
||||||
|
value = cache.get(key=key)
|
||||||
if value is not None and not force_request:
|
if value is not None and not force_request:
|
||||||
logger.debug("vqd: re-use cached value: %s", value)
|
logger.debug("vqd: re-use cached value: %s", value)
|
||||||
return value
|
return value
|
||||||
@ -124,15 +127,16 @@ def get_vqd(query: str, region: str, force_request: bool = False) -> str:
|
|||||||
logger.error("vqd: got HTTP %s from duckduckgo.com", resp.status_code)
|
logger.error("vqd: got HTTP %s from duckduckgo.com", resp.status_code)
|
||||||
|
|
||||||
if value:
|
if value:
|
||||||
CACHE.set(key=key, value=value)
|
cache.set(key=key, value=value)
|
||||||
else:
|
else:
|
||||||
logger.error("vqd value from duckduckgo.com ", resp.status_code)
|
logger.error("vqd value from duckduckgo.com ", resp.status_code)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def set_vqd(query: str, region: str, value: str):
|
def set_vqd(query: str, region: str, value: str):
|
||||||
key = CACHE.secret_hash(f"{query}//{region}")
|
cache = get_cache()
|
||||||
CACHE.set(key=key, value=value, expire=3600)
|
key = cache.secret_hash(f"{query}//{region}")
|
||||||
|
cache.set(key=key, value=value, expire=3600)
|
||||||
|
|
||||||
|
|
||||||
def get_ddg_lang(eng_traits: EngineTraits, sxng_locale, default='en_US'):
|
def get_ddg_lang(eng_traits: EngineTraits, sxng_locale, default='en_US'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user