summaryrefslogtreecommitdiff
path: root/searx/engines/dogpile.py
blob: a5ae5480b5ffcc340776b06f62f9e350a4422274 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Dogpile is a metasearch engine by the American advertising company `System1`_.

.. _System1: https://system1.com/
"""

import typing as t
from datetime import datetime, timezone
import html

from searx.utils import format_duration, html_to_text, humanize_number
from searx.result_types import EngineResults

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

about = {
    "website": "https://www.dogpile.com",
    "wikidata_id": "Q3595363",
    "official_api_documentation": None,
    "use_official_api": False,
    "require_api_key": False,
    "results": "JSON",
}

paging = True
safesearch = True

categories = ["general"]
dogpile_categ = "search"
"""Category to search in. Can be either "search", "images", "videos" or "news"."""


base_url = "https://www.dogpile.com"
safe_search_map = {0: "none", 1: "moderate", 2: "heavy"}


def init(_):
    if dogpile_categ not in ("search", "images", "videos", "news"):
        raise ValueError("invalid search type: %s" % dogpile_categ)


def request(query: str, params: "OnlineParams"):
    params["url"] = f"{base_url}/api/{dogpile_categ}"
    params["method"] = "POST"
    params["json"] = {"q": query, "qadf": safe_search_map[params["safesearch"]], "page": params["pageno"]}
    return params


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

    json_resp = resp.json()

    for result in json_resp["results"]:
        if dogpile_categ == "search":
            res.add(
                res.types.MainResult(
                    url=result["clickUrl"],
                    title=html_to_text(result["title"]),
                    content=html_to_text(result["description"]),
                )
            )
        elif dogpile_categ == "news":
            res.add(
                res.types.MainResult(
                    url=result["clickUrl"],
                    title=html_to_text(html.unescape(result["title"])),
                    content=html_to_text(html.unescape(result["description"])),
                    thumbnail=result["thumbnailUrl"],
                    publishedDate=datetime.fromtimestamp(result["date"], tz=timezone.utc),
                )
            )
        elif dogpile_categ == "videos":
            res.add(
                res.types.LegacyResult(
                    template="videos.html",
                    url=result["clickUrl"],
                    title=html_to_text(result["title"]),
                    content=html_to_text(result["description"]),
                    thumbnail=result["thumbnailUrl"],
                    publishedDate=datetime.fromisoformat(result["publishDate"]),
                    length=format_duration(result["duration"]),
                    views=humanize_number(result["viewCount"]),
                )
            )
        elif dogpile_categ == "images":
            res.add(
                res.types.Image(
                    url=result["altClickUrl"],
                    title=html_to_text(result["title"]),
                    content=html_to_text(result["description"]),
                    img_src=result["clickUrl"],
                    thumbnail_src=result["thumbnailUrl"],
                    resolution=f"{result['width']}x{result['height']}",
                    img_format=result["format"],
                )
            )

    return res