SimpleerTube/main.py

148 lines
3.8 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-17 16:04:24 +01:00
import peertube
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
# Format:
# key: domain
# entry: [ instance_name, last_time_updated ]
2021-01-21 11:14:20 +01:00
cached_instance_names = {}
# 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):
if domain in cached_instance_names:
last_time_updated = (cached_instance_names[domain])[1]
time_diff = datetime.now() - last_time_updated
# only check once every day
if time_diff.days != 0:
cached_instance_names[domain] = [
peertube.get_instance_name(domain),
datetime.now()
]
else:
cached_instance_names[domain] = [
peertube.get_instance_name(domain),
datetime.now()
]
return (cached_instance_names[domain])[0]
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-19 12:13:21 +01:00
return await render_template("index.html")
2021-01-17 16:04:24 +01:00
2021-01-19 12:13:21 +01:00
@app.route("/<string:domain>/")
2021-01-17 16:04:24 +01:00
async def domain_main(domain):
2021-01-19 12:13:21 +01:00
return await render_template(
"domain_index.html",
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-19 12:13:21 +01:00
)
2021-01-17 16:04:24 +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)
@app.route("/<string:domain>/search/<string:term>")
2021-01-17 16:04:24 +01:00
async def search(domain, term):
amount, results = peertube.search(domain, term)
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
amount=amount,
results=results,
search_term=term,
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>/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)
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
2021-01-17 16:04:24 +01:00
if __name__ == "__main__":
app.run()