summaryrefslogtreecommitdiff
path: root/searx/engines/keenable.py
blob: 5c1f11f0ebb92b3dfcd85af127bb16dc94b0cb3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Keenable is a fast web search with keyless mode support"""

import typing as t

from datetime import datetime
from searx.extended_types import SXNG_Response
from searx.result_types import EngineResults
from searx.utils import searxng_useragent

if t.TYPE_CHECKING:
    from searx.search.processors import OnlineParams

about = {
    "website": "https://keenable.ai",
    "official_api_documentation": "https://docs.keenable.ai",
    "use_official_api": True,
    "require_api_key": False,
    "results": "JSON",
}
api_key = ""
""" Optional API Key. You can create a key at `the official website
<https://keenable.ai/signup>'_ if you need higher rate limits."""

categories = ["general"]

base_url = "https://api.keenable.ai"
keenable_mode = "pro"


def request(query: str, params: "OnlineParams"):
    if api_key:
        params["url"] = f"{base_url}/v1/search"
        params["headers"]["X-API-KEY"] = api_key
    else:
        params["url"] = f"{base_url}/v1/search/public"

    params["method"] = "POST"
    params["headers"]["X-Keenable-Title"] = searxng_useragent()
    params["json"] = {"query": query, "mode": keenable_mode}


def response(resp: "SXNG_Response") -> EngineResults:
    res = EngineResults()

    results: list[dict[str, str]] = resp.json()["results"]  # type: ignore[reportAny]

    for result in results:
        published = None
        pub = result.get("published_at")
        if pub:
            try:
                published = datetime.fromisoformat(pub.rstrip("Z"))
            except ValueError:
                pass

        res.add(
            res.types.MainResult(
                url=result["url"],
                title=result["title"],
                content=result["description"] or result["snippet"],
                publishedDate=published,
            )
        )

    return res