diff options
author | Michael Ilsaas <mikeri@mikeri.net> | 2020-08-08 19:22:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-08 19:22:53 +0200 |
commit | a1ce141c99bd31017a131eb6bc57107b886f1442 (patch) | |
tree | 297d8ffba7910ba1d295e92c700bcb3c2c898009 /searx/engines/peertube.py | |
parent | c3054a10a6cf974493a8f27b4f0f820a8d415ead (diff) | |
download | searxng-a1ce141c99bd31017a131eb6bc57107b886f1442.tar.gz searxng-a1ce141c99bd31017a131eb6bc57107b886f1442.zip |
add peertube engine (#2109)
Diffstat (limited to 'searx/engines/peertube.py')
-rw-r--r-- | searx/engines/peertube.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/searx/engines/peertube.py b/searx/engines/peertube.py new file mode 100644 index 000000000..b3795bf83 --- /dev/null +++ b/searx/engines/peertube.py @@ -0,0 +1,95 @@ +""" + peertube (Videos) + + @website https://www.peertube.live + @provide-api yes (https://docs.joinpeertube.org/api-rest-reference.html) + + @using-api yes + @results JSON + @stable yes + @parse url, title, thumbnail, publishedDate, embedded + + @todo implement time range support +""" + +from json import loads +from datetime import datetime +from searx.url_utils import urlencode +from searx.utils import html_to_text + +# engine dependent config +categories = ["videos"] +paging = True +language_support = True +base_url = "https://peer.tube/" +supported_languages_url = base_url + "api/v1/videos/languages" + + +# do search-request +def request(query, params): + pageno = (params["pageno"] - 1) * 15 + search_url = base_url + "api/v1/search/videos/?pageno={pageno}&{query}" + query_dict = {"search": query} + language = params["language"].split("-")[0] + # pylint: disable=undefined-variable + if "all" != language and language in supported_languages: + query_dict["languageOneOf"] = language + params["url"] = search_url.format( + query=urlencode(query_dict), pageno=pageno + ) + return params + + +def _get_offset_from_pageno(pageno): + return (pageno - 1) * 15 + 1 + + +# get response from search-request +def response(resp): + results = [] + + search_res = loads(resp.text) + + embedded_url = ( + '<iframe width="560" height="315" sandbox="allow-same-origin allow-scripts allow-popups" ' + + 'src="' + + base_url + + '{embed_path}" frameborder="0" allowfullscreen></iframe>' + ) + # 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 = base_url + "/videos/watch/" + res["uuid"] + description = res["description"] + if description: + content = html_to_text(res["description"]) + else: + content = None + thumbnail = base_url + res["thumbnailPath"] + publishedDate = datetime.strptime(res["publishedAt"], "%Y-%m-%dT%H:%M:%S.%fZ") + embedded = embedded_url.format(embed_path=res["embedPath"][1:]) + + results.append( + { + "template": "videos.html", + "url": url, + "title": title, + "content": content, + "publishedDate": publishedDate, + "embedded": embedded, + "thumbnail": thumbnail, + } + ) + + # return results + return results + + +def _fetch_supported_languages(resp): + ret_val = {} + peertube_languages = list(loads(resp.text).keys()) + return peertube_languages |