summaryrefslogtreecommitdiff
path: root/searx/engines/360search_videos.py
blob: d359d534fb48f685811fa5f968a72649e90fd579 (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
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=invalid-name
"""360Search-Videos: A search engine for retrieving videos from 360Search."""

from urllib.parse import urlencode
from datetime import datetime

from searx.exceptions import SearxEngineAPIException
from searx.result_types import EngineResults
from searx.utils import html_to_text

about = {
    "website": "https://tv.360kan.com/",
    "use_official_api": False,
    "require_api_key": False,
    "results": "JSON",
}

paging = True
results_per_page = 10
categories = ["videos"]

base_url = "https://tv.360kan.com"


def request(query, params):
    query_params = {"count": 10, "q": query, "start": params["pageno"] * 10}

    params["url"] = f"{base_url}/v1/video/list?{urlencode(query_params)}"
    return params


def response(resp) -> EngineResults:
    try:
        data = resp.json()
    except Exception as e:
        raise SearxEngineAPIException(f"Invalid response: {e}") from e
    res = EngineResults()

    if "data" not in data or "result" not in data["data"]:
        raise SearxEngineAPIException("Invalid response")

    for entry in data["data"]["result"]:
        if not entry.get("title") or not entry.get("play_url"):
            continue

        published_date = None
        if entry.get("publish_time"):
            try:
                published_date = datetime.fromtimestamp(int(entry["publish_time"]))
            except (ValueError, TypeError):
                published_date = None

        res.add(
            res.types.LegacyResult(
                url=entry["play_url"],
                title=html_to_text(entry["title"]),
                content=html_to_text(entry["description"]),
                template='videos.html',
                publishedDate=published_date,
                thumbnail=entry["cover_img"],
            )
        )

    return res