searxngRebrandZaclys/searx/engines/peertube.py

88 lines
2.4 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
2020-08-08 17:22:53 +00:00
"""
peertube (Videos)
"""
from json import loads
from datetime import datetime
from urllib.parse import urlencode
2020-08-08 17:22:53 +00:00
from searx.utils import html_to_text
# about
about = {
"website": 'https://joinpeertube.org',
"wikidata_id": 'Q50938515',
"official_api_documentation": 'https://docs.joinpeertube.org/api-rest-reference.html',
"use_official_api": True,
"require_api_key": False,
"results": 'JSON',
}
2020-08-08 17:22:53 +00:00
# engine dependent config
categories = ["videos"]
paging = True
base_url = "https://peer.tube"
supported_languages_url = 'https://peer.tube/api/v1/videos/languages'
2020-08-08 17:22:53 +00:00
# do search-request
def request(query, params):
sanitized_url = base_url.rstrip("/")
2020-08-08 17:22:53 +00:00
pageno = (params["pageno"] - 1) * 15
search_url = sanitized_url + "/api/v1/search/videos/?pageno={pageno}&{query}"
2020-08-08 17:22:53 +00:00
query_dict = {"search": query}
language = params["language"].split("-")[0]
if "all" != language and language in supported_languages:
query_dict["languageOneOf"] = language
params["url"] = search_url.format(query=urlencode(query_dict), pageno=pageno)
2020-08-08 17:22:53 +00:00
return params
def _get_offset_from_pageno(pageno):
return (pageno - 1) * 15 + 1
# get response from search-request
def response(resp):
sanitized_url = base_url.rstrip("/")
2020-08-08 17:22:53 +00:00
results = []
search_res = loads(resp.text)
# return empty array if there are no results
if "data" not in search_res:
return []
# parse results
for res in search_res["data"]:
title = res["name"]
url = sanitized_url + "/videos/watch/" + res["uuid"]
2020-08-08 17:22:53 +00:00
description = res["description"]
if description:
content = html_to_text(res["description"])
else:
content = ""
thumbnail = sanitized_url + res["thumbnailPath"]
2020-08-08 17:22:53 +00:00
publishedDate = datetime.strptime(res["publishedAt"], "%Y-%m-%dT%H:%M:%S.%fZ")
results.append(
{
"template": "videos.html",
"url": url,
"title": title,
"content": content,
"publishedDate": publishedDate,
"iframe_src": sanitized_url + res["embedPath"],
2020-08-08 17:22:53 +00:00
"thumbnail": thumbnail,
}
)
# return results
return results
def _fetch_supported_languages(resp):
videolanguages = resp.json()
peertube_languages = list(videolanguages.keys())
2020-08-08 17:22:53 +00:00
return peertube_languages