diff options
| author | Tommaso Colella <tommasocolella95@gmail.com> | 2025-12-10 13:26:43 +0100 |
|---|---|---|
| committer | Bnyro <bnyro@tutanota.com> | 2025-12-29 15:26:07 +0100 |
| commit | c57db45672efc21e971583dc68df0eb72d6d5000 (patch) | |
| tree | 7e04f850a114312c2cb800227c1b5658c37ddc00 /searx/engines | |
| parent | 9491b514c97e3282d0f8037c7ae9c0304b97df76 (diff) | |
[fix] 360search: fix engine by adding cookie caching
Co-authored-by: Bnyro <bnyro@tutanota.com>
Diffstat (limited to 'searx/engines')
| -rw-r--r-- | searx/engines/360search.py | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/searx/engines/360search.py b/searx/engines/360search.py index 5bc123bbc..5aad7eb9f 100644 --- a/searx/engines/360search.py +++ b/searx/engines/360search.py @@ -2,10 +2,18 @@ # pylint: disable=invalid-name """360Search search engine for searxng""" +import typing as t + from urllib.parse import urlencode from lxml import html +from searx import logger +from searx.enginelib import EngineCache from searx.utils import extract_text +from searx.network import get as http_get + +if t.TYPE_CHECKING: + from searx.extended_types import SXNG_Response # Metadata about = { @@ -26,6 +34,35 @@ time_range_dict = {'day': 'd', 'week': 'w', 'month': 'm', 'year': 'y'} # Base URL base_url = "https://www.so.com" +COOKIE_CACHE_KEY = "cookie" +COOKIE_CACHE_EXPIRATION_SECONDS = 3600 + +CACHE: EngineCache +"""Stores cookies from 360search to avoid re-fetching them on every request.""" + + +def setup(engine_settings: dict[str, t.Any]) -> bool: + """Initialization of the engine. + + - Instantiate a cache for this engine (:py:obj:`CACHE`). + + """ + global CACHE # pylint: disable=global-statement + # table name needs to be quoted to start with digits, so "cache" has been added to avoid sqlite complaining + CACHE = EngineCache("cache" + engine_settings["name"]) + return True + + +def get_cookie(url: str) -> str: + cookie: str | None = CACHE.get(COOKIE_CACHE_KEY) + if cookie: + return cookie + resp: SXNG_Response = http_get(url, timeout=10, allow_redirects=False) + headers = resp.headers + cookie = headers['set-cookie'].split(";")[0] + CACHE.set(key=COOKIE_CACHE_KEY, value=cookie, expire=COOKIE_CACHE_EXPIRATION_SECONDS) + + return cookie def request(query, params): @@ -36,8 +73,13 @@ def request(query, params): if time_range_dict.get(params['time_range']): query_params["adv_t"] = time_range_dict.get(params['time_range']) - params["url"] = f"{base_url}/s?{urlencode(query_params)}" + # get token by calling the query page + logger.debug("querying url: %s", params["url"]) + cookie = get_cookie(params["url"]) + logger.debug("obtained cookie: %s", cookie) + params['headers'] = {'Cookie': cookie} + return params |