From 104a36deea18b409437bc18b946e798f0b35fbfa Mon Sep 17 00:00:00 2001 From: metalune Date: Tue, 19 Jan 2021 12:13:21 +0100 Subject: [PATCH] Add basic search page --- main.py | 37 ++++++++++++++++++++++++++----------- peertube.py | 4 ++++ templates/domain_index.html | 10 ++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index ea27539..b38dc85 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from quart import Quart, request, render_template +from quart import Quart, request, render_template, redirect import peertube # Wrapper, only containing information that's important for us, and in some cases provides simplified ways to get information @@ -19,8 +19,6 @@ class VideoWrapper: self.likes = a["likes"] self.dislikes = a["dislikes"] - - self.resolutions = [] self.video = None @@ -36,20 +34,36 @@ class VideoWrapper: app = Quart(__name__) -@app.route('/') + +@app.route("/") async def main(): - return await render_template('index.html') + return await render_template("index.html") -@app.route('/') + +@app.route("//") async def domain_main(domain): - return await render_template('domain_index.html') + return await render_template( + "domain_index.html", + domain=domain, + instance_name="placeholder", + #instance_name=peertube.get_instance_name(domain), + ) -@app.route('//search/') +@app.route('//search', methods=["POST"]) +async def search_redirect(domain): + query = (await request.form)["query"] + return redirect("/" + domain + "/search/" + query) + + +@app.route("//search/") async def search(domain, term): amount, results = peertube.search(domain, term) - return await render_template('search_results.html', domain=domain, amount=amount, results=results) + return await render_template( + "search_results.html", domain=domain, amount=amount, results=results + ) -@app.route('//watch//') + +@app.route("//watch//") async def video(domain, id): data = peertube.video(domain, id) quality = request.args.get("quality") @@ -57,7 +71,8 @@ async def video(domain, id): quality = "best" vid = VideoWrapper(data, quality) - return await render_template('video.html', video=vid, quality=quality) + return await render_template("video.html", video=vid, quality=quality) + if __name__ == "__main__": app.run() diff --git a/peertube.py b/peertube.py index 94a113a..f40eb35 100644 --- a/peertube.py +++ b/peertube.py @@ -2,6 +2,10 @@ from bs4 import BeautifulSoup import requests import json +def get_instance_name(domain): + soup = BeautifulSoup(requests.get("https://" + domain).text) + return soup.find('span', class_="instance-name").text + def video(domain, id): video_url = "https://" + domain + "/api/v1/videos/" + id video_object = json.loads(requests.get(video_url).text) diff --git a/templates/domain_index.html b/templates/domain_index.html index e69de29..2cc70ae 100644 --- a/templates/domain_index.html +++ b/templates/domain_index.html @@ -0,0 +1,10 @@ + + + + {{ instance_name }} +
+ + +
+ +