mirror of
https://github.com/searxng/searxng.git
synced 2026-09-11 16:56:05 +00:00
[fix] searchzee: bypass botblocking
requires https://github.com/searxng/searxng/pull/6620
This commit is contained in:
@@ -4,13 +4,11 @@ independent search infrastructure."""
|
|||||||
|
|
||||||
import typing as t
|
import typing as t
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
import uuid
|
||||||
|
|
||||||
from searx.exceptions import SearxEngineAPIException
|
|
||||||
from searx.extended_types import SXNG_Response
|
from searx.extended_types import SXNG_Response
|
||||||
from searx.network import get
|
|
||||||
from searx.result_types import EngineResults
|
from searx.result_types import EngineResults
|
||||||
from searx.utils import extr, html_to_text
|
from searx.utils import html_to_text
|
||||||
from searx.enginelib import EngineCache
|
|
||||||
|
|
||||||
if t.TYPE_CHECKING:
|
if t.TYPE_CHECKING:
|
||||||
from searx.search.processors import OnlineParams
|
from searx.search.processors import OnlineParams
|
||||||
@@ -34,43 +32,19 @@ SearchzeeCategType = t.Literal["web", "news"]
|
|||||||
searchzee_categ: SearchzeeCategType = None # type: ignore[reportAssignmentType]
|
searchzee_categ: SearchzeeCategType = None # type: ignore[reportAssignmentType]
|
||||||
|
|
||||||
|
|
||||||
CACHE: EngineCache
|
|
||||||
"""Cache for storing the scraped API Token."""
|
|
||||||
|
|
||||||
base_url = "https://searchzee.com"
|
base_url = "https://searchzee.com"
|
||||||
|
|
||||||
# only supports for news
|
# only supports for news
|
||||||
time_range_map = {"day": "pd", "week": "pw", "month": "pm", "year": "py"}
|
time_range_map = {"day": "pd", "week": "pw", "month": "pm", "year": "py"}
|
||||||
|
|
||||||
|
|
||||||
def setup(engine_settings: dict[str, t.Any]) -> bool:
|
def setup(_: dict[str, t.Any]):
|
||||||
if searchzee_categ not in t.get_args(SearchzeeCategType):
|
if searchzee_categ not in t.get_args(SearchzeeCategType):
|
||||||
raise ValueError("invalid category: %s" % searchzee_categ)
|
raise ValueError("invalid category: %s" % searchzee_categ)
|
||||||
|
|
||||||
global CACHE # pylint: disable=global-statement
|
|
||||||
CACHE = EngineCache(engine_settings["name"]) # type: ignore[reportAny]
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def _obtain_api_token() -> str:
|
|
||||||
token: str | None = CACHE.get("token") # type: ignore[reportAny]
|
|
||||||
if token:
|
|
||||||
return token
|
|
||||||
|
|
||||||
token_resp = get(
|
|
||||||
f"{base_url}/app.js",
|
|
||||||
)
|
|
||||||
if not token_resp.ok:
|
|
||||||
raise SearxEngineAPIException("failed to obtain api key")
|
|
||||||
|
|
||||||
token = extr(token_resp.text, "const SEARCHZEE_API_TOKEN = \"", "\";")
|
|
||||||
CACHE.set("token", token, expire=3600)
|
|
||||||
|
|
||||||
return token
|
|
||||||
|
|
||||||
|
|
||||||
def request(query: str, params: "OnlineParams"):
|
def request(query: str, params: "OnlineParams"):
|
||||||
params["headers"]["X-SearchZee-Token"] = _obtain_api_token()
|
params["cookies"]["szs"] = str(uuid.uuid4())
|
||||||
|
|
||||||
args = {"q": query, "type": searchzee_categ, "offset": params["pageno"] - 1}
|
args = {"q": query, "type": searchzee_categ, "offset": params["pageno"] - 1}
|
||||||
if params["time_range"]:
|
if params["time_range"]:
|
||||||
|
|||||||
Reference in New Issue
Block a user