summaryrefslogtreecommitdiff
path: root/searx/engines/searchrockit.py
blob: 90858ee4d39eea3a8a3e2593507217f8e950891f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# SPDX-License-Identifier: AGPL-3.0-or-later
"""SearchRockit is an American search engine. It allegedly has its own index,
but the results seem to come from Google."""

import typing as t
from urllib.parse import urlencode
from lxml import html
from dateutil import parser

from searx.result_types import EngineResults
from searx.utils import (
    eval_xpath_list,
    extract_text,
    eval_xpath,
)

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

about = {
    "website": "https://searchrockit.com",
    "official_api_documentation": None,
    "use_official_api": False,
    "require_api_key": False,
    "results": "HTML",
}

categories = ["general"]
paging = True

SearchrockitCateg = t.Literal["web", "images", "news"]
searchrockit_categ: SearchrockitCateg = "web"

base_url = "https://searchrockit.com"


def setup(_):
    if searchrockit_categ not in t.get_args(SearchrockitCateg):
        raise ValueError("invalid search category: %s" % searchrockit_categ)


def request(query: str, params: "OnlineParams") -> None:
    args = {"q": query, "p": params["pageno"]}
    params["url"] = f"{base_url}/results/{searchrockit_categ}?{urlencode(args)}"


def response(resp: "SXNG_Response") -> EngineResults:
    doc = html.fromstring(resp.text)
    res = EngineResults()

    match searchrockit_categ:
        case "web" | "news":
            for result in eval_xpath_list(
                doc, "//div[contains(@class, 'results-list')]/div[contains(@class, 'result-item')]"
            ):
                publishedDate = None
                try:
                    d = extract_text(eval_xpath(result, ".//span[contains(@class, 'result-item--publishedAt')]")) or ""
                    publishedDate = parser.parse(d)
                except parser.ParserError:
                    pass
                res.add(
                    res.types.MainResult(
                        url=extract_text(eval_xpath(result, ".//a[contains(@class, 'result-item--title')]/@href")),
                        title=extract_text(eval_xpath(result, ".//a[contains(@class, 'result-item--title')]")) or "",
                        content=extract_text(eval_xpath(result, ".//a[contains(@class, 'result-item--desc')]")) or "",
                        thumbnail=extract_text(
                            eval_xpath(result, ".//a[contains(@class, 'result-item--thumb')]/img/@src")
                        )
                        or "",
                        publishedDate=publishedDate,
                    )
                )
        case "images":
            for result in eval_xpath_list(
                doc, "//div[contains(@class, 'image-grid')]/a[contains(@class, 'image-card')]"
            ):
                res.add(
                    res.types.Image(
                        url=extract_text(eval_xpath(result, "./@href")),
                        title=extract_text(eval_xpath(result, "./div[contains(@class, 'image-title')]")) or "",
                        thumbnail_src=extract_text(eval_xpath(result, "./img/@src")) or "",
                        img_src=extract_text(eval_xpath(result, "./@data-full-url")) or "",
                    )
                )

    return res