1
0
mirror of https://git.sr.ht/~metalune/simpleweb_peertube synced 2024-12-22 14:15:21 +01:00
SimpleerTube_peertube_minim.../main.py

407 lines
12 KiB
Python
Raw Normal View History

2021-01-19 12:13:21 +01:00
from quart import Quart, request, render_template, redirect
from datetime import datetime
2021-01-23 15:20:10 +01:00
from math import ceil
2021-01-17 16:04:24 +01:00
import peertube
import html2text
h2t = html2text.HTML2Text()
h2t.ignore_links = True
2021-01-17 16:04:24 +01:00
2021-01-20 17:10:23 +01:00
commit = "not found"
with open(".git/refs/heads/main") as file:
for line in file:
commit = line
# we only expect one line
break
2021-01-17 16:04:24 +01:00
# Wrapper, only containing information that's important for us, and in some cases provides simplified ways to get information
class VideoWrapper:
def __init__(self, a, quality):
self.name = a["name"]
self.channel = a["channel"]
self.description = a["description"]
self.thumbnailPath = a["thumbnailPath"]
self.category = a["category"]
self.licence = a["licence"]
self.language = a["language"]
self.privacy = a["privacy"]
self.tags = a["tags"]
self.views = a["views"]
self.likes = a["likes"]
self.dislikes = a["dislikes"]
self.embedPath = a["embedPath"]
self.commentsEnabled = a["commentsEnabled"]
2021-01-17 16:04:24 +01:00
self.resolutions = []
self.video = None
self.files = a["files"]
if len(self.files) == 0:
self.files = ((a["streamingPlaylists"])[0])["files"]
for entry in self.files:
2021-01-17 16:04:24 +01:00
resolution = (entry["resolution"])["id"]
self.resolutions.append(entry["resolution"])
if str(resolution) == str(quality):
self.video = entry["fileUrl"]
self.no_quality_selected = not self.video
# Helper Class for using caches
class Cache:
def __init__(self):
self.dict = {}
def get(self, arg, func):
if arg in self.dict:
last_time_updated = (self.dict[arg])[1]
time_diff = datetime.now() - last_time_updated
if time_diff.days > 0:
self.dict[arg] = [
func(arg),
datetime.now()
]
else:
self.dict[arg] = [
func(arg),
datetime.now()
]
return (self.dict[arg])[0]
cached_instance_names = Cache()
cached_account_infos = Cache()
cached_video_channel_infos = Cache()
2021-01-21 11:14:20 +01:00
# cache the instance names so we don't have to send a request to the domain every time someone
# loads any site
def get_instance_name(domain):
return cached_instance_names.get(domain, peertube.get_instance_name)
# simple wrapper that is used inside the cached_account_infos
def get_account(info):
info = info.split("@")
return peertube.account(info[1], info[0])
def get_account_info(name):
return cached_account_infos.get(name, get_account)
# simple wrapper that is used inside the cached_video_channel_infos
def get_video_channel(info):
info = info.split("@")
return peertube.video_channel(info[1], info[0])
def get_video_channel_info(name):
return cached_video_channel_infos.get(name, get_video_channel)
2021-01-17 16:04:24 +01:00
2021-01-19 12:13:21 +01:00
2021-01-21 11:14:20 +01:00
app = Quart(__name__)
2021-01-19 12:13:21 +01:00
@app.route("/")
2021-01-17 16:04:24 +01:00
async def main():
2021-01-22 05:08:04 +01:00
return await render_template(
"index.html",
commit=commit,
)
@app.route("/search", methods = ["POST"])
async def simpleer_search_redirect():
query = (await request.form)["query"]
return redirect("/search/" + query)
2021-01-23 15:20:10 +01:00
@app.route("/search/<string:query>", defaults = {"page": 1})
@app.route("/search/<string:query>/<int:page>")
async def simpleer_search(query, page):
results = peertube.sepia_search(query, (page - 1) * 10)
2021-01-22 05:08:04 +01:00
return await render_template(
"simpleer_search_results.html",
commit=commit,
2021-01-23 15:20:10 +01:00
results = results,
query = query,
# details for pagination
page=page,
pages_total=ceil(results["total"] / 10),
2021-01-22 05:08:04 +01:00
)
2021-01-19 12:13:21 +01:00
2021-01-17 16:04:24 +01:00
2021-01-19 12:13:21 +01:00
@app.route("/<string:domain>/")
2021-01-22 04:17:02 +01:00
async def instance(domain):
return redirect("/" + domain + "/videos/trending")
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/videos/local", defaults = {"page": 1})
@app.route("/<string:domain>/videos/local/<int:page>")
async def instance_videos_local(domain, page):
vids = peertube.get_videos_local(domain, (page - 1) * 10)
2021-01-19 12:13:21 +01:00
return await render_template(
2021-01-22 04:17:02 +01:00
"instance/local.html",
2021-01-19 12:13:21 +01:00
domain=domain,
2021-01-21 11:14:20 +01:00
instance_name=get_instance_name(domain),
2021-01-20 17:10:23 +01:00
commit=commit,
2021-01-22 04:17:02 +01:00
2021-01-23 15:20:10 +01:00
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/local/",
pages_total=ceil(vids["total"] / 10),
2021-01-22 04:17:02 +01:00
)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/videos/trending", defaults = {"page": 1})
@app.route("/<string:domain>/videos/trending/<int:page>")
async def instance_videos_trending(domain, page):
vids = peertube.get_videos_trending(domain, (page - 1) * 10)
2021-01-22 04:17:02 +01:00
return await render_template(
"instance/trending.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
2021-01-23 15:20:10 +01:00
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/trending/",
pages_total=ceil(vids["total"] / 10),
2021-01-22 04:17:02 +01:00
)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/videos/most-liked", defaults = {"page": 1})
@app.route("/<string:domain>/videos/most-liked/<int:page>")
async def instance_videos_most_liked(domain, page):
vids = peertube.get_videos_most_liked(domain, (page - 1) * 10)
2021-01-22 04:17:02 +01:00
return await render_template(
"instance/most-liked.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
2021-01-23 15:20:10 +01:00
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/most-liked/",
pages_total=ceil(vids["total"] / 10),
2021-01-22 04:17:02 +01:00
)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/videos/recently-added", defaults = {"page": 1})
@app.route("/<string:domain>/videos/recently-added/<int:page>")
async def instance_videos_recently_added(domain, page):
vids = peertube.get_videos_recently_added(domain, (page - 1) * 10)
2021-01-22 04:17:02 +01:00
return await render_template(
"instance/recently-added.html",
domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
2021-01-23 15:20:10 +01:00
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/videos/recently-added/",
pages_total=ceil(vids["total"] / 10),
2021-01-19 12:13:21 +01:00
)
2021-01-17 16:04:24 +01:00
2021-01-20 17:10:23 +01:00
2021-01-23 15:20:10 +01:00
2021-01-20 17:10:23 +01:00
@app.route("/<string:domain>/search", methods=["POST"])
2021-01-19 12:13:21 +01:00
async def search_redirect(domain):
query = (await request.form)["query"]
return redirect("/" + domain + "/search/" + query)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/search/<string:term>", defaults = {"page": 1})
@app.route("/<string:domain>/search/<string:term>/<int:page>")
async def search(domain, term, page):
results = peertube.search(domain, term, (page - 1) * 10)
2021-01-19 12:13:21 +01:00
return await render_template(
2021-01-20 17:10:23 +01:00
"search_results.html",
domain=domain,
2021-01-21 11:14:20 +01:00
instance_name=get_instance_name(domain),
commit=commit,
2021-01-20 17:10:23 +01:00
results=results,
search_term=term,
2021-01-23 15:20:10 +01:00
# details for pagination
page=page,
pagination_url="/" + domain + "/search/" + term + "/",
pages_total=(results["total"] / 10)
2021-01-19 12:13:21 +01:00
)
2021-01-17 16:04:24 +01:00
@app.route("/<string:domain>/videos/watch/<string:id>/")
2021-01-17 16:04:24 +01:00
async def video(domain, id):
data = peertube.video(domain, id)
quality = request.args.get("quality")
embed = request.args.get("embed")
2021-01-17 16:04:24 +01:00
if quality == None:
quality = "best"
vid = VideoWrapper(data, quality)
# only make a request for the comments if commentsEnabled
comments = ""
if data["commentsEnabled"]:
comments = peertube.get_comments(domain, id)
# Strip the HTML from the comments and convert them to plain text
new_comments = {"total": comments["total"], "data": []}
for comment in comments["data"]:
text = h2t.handle(comment["text"]).strip().strip("\n")
comment["text"] = text
new_comments["data"].append(comment)
comments = new_comments
2021-01-20 17:10:23 +01:00
return await render_template(
"video.html",
domain=domain,
2021-01-21 11:14:20 +01:00
commit=commit,
instance_name=get_instance_name(domain),
2021-01-20 17:10:23 +01:00
video=vid,
comments=comments,
2021-01-20 17:10:23 +01:00
quality=quality,
embed=embed,
)
2021-01-19 12:13:21 +01:00
def build_channel_or_account_name(domain, name):
if '@' in name:
return name
return name + "@" + domain
# --- Accounts ---
@app.route("/<string:domain>/accounts/<string:name>")
2021-01-23 15:20:10 +01:00
async def accounts_redirect(domain, name):
return redirect("/" + domain + "/accounts/" + name + "/video-channels")
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/accounts/<string:name>/video-channels", defaults = {"page": 1})
@app.route("/<string:domain>/accounts/<string:name>/video-channels/<int:page>")
async def account__video_channels(domain, name, page):
video_channels = peertube.account_video_channels(domain, name, (page - 1) * 10)
return await render_template(
"accounts/video_channels.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
account = get_account_info(build_channel_or_account_name(domain, name)),
2021-01-23 15:20:10 +01:00
video_channels = video_channels,
# details for pagination
page=page,
pagination_url="/" + domain + "/accounts/" + name + "/video-channels/",
pages_total=ceil(video_channels["total"] / 10)
)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/accounts/<string:name>/videos", defaults = {"page": 1})
@app.route("/<string:domain>/accounts/<string:name>/videos/<int:page>")
async def account__videos(domain, name, page):
vids = peertube.account_videos(domain, name, (page - 1) * 10)
return await render_template(
"accounts/videos.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
account = get_account_info(build_channel_or_account_name(domain, name)),
2021-01-23 15:20:10 +01:00
videos = vids,
# details for pagination
page=page,
pagination_url="/" + domain + "/accounts/" + name + "/videos/",
pages_total=ceil(vids["total"] / 10)
)
@app.route("/<string:domain>/accounts/<string:name>/about")
async def account__about(domain, name):
return await render_template(
"accounts/about.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
account = get_account_info(build_channel_or_account_name(domain, name)),
about = peertube.account(domain, name)
)
# --- Video-Channels ---
@app.route("/<string:domain>/video-channels/<string:name>")
2021-01-23 15:20:10 +01:00
async def video_channels_redirect(domain, name):
return redirect("/" + domain + "/video-channels/" + name + "/videos")
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/video-channels/<string:name>/videos", defaults = {"page": 1})
@app.route("/<string:domain>/video-channels/<string:name>/videos/<int:page>")
async def video_channels__videos(domain, name, page):
vids = peertube.video_channel_videos(domain, name, (page - 1) * 10)
return await render_template(
"video_channels/videos.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)),
2021-01-23 15:20:10 +01:00
page=page,
pagination_url="/" + domain + "/video-channels/" + name + "/videos/",
pages_total=ceil(vids["total"] / 10),
videos = vids,
)
2021-01-23 15:20:10 +01:00
@app.route("/<string:domain>/video-channels/<string:name>/video-playlists", defaults = {"page": 1})
@app.route("/<string:domain>/video-channels/<string:name>/video-playlists/<int:page>")
async def video_channels__video_playlists(domain, name, page):
video_playlists = peertube.video_channel_video_playlists(domain, name, (page - 1) * 10)
return await render_template(
"video_channels/video_playlists.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)),
2021-01-23 15:20:10 +01:00
video_playlists = video_playlists,
page=page,
pagination_url="/" + domain + "/video-channels/" + name + "/video-playlists/",
pages_total=ceil(video_playlists["total"] / 10)
)
@app.route("/<string:domain>/video-channels/<string:name>/about")
async def video_channels__about(domain, name):
return await render_template(
"video_channels/about.html",
domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
name = name,
video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)),
about = peertube.video_channel(domain, name)
)
2021-01-17 16:04:24 +01:00
if __name__ == "__main__":
app.run()