1
0
mirror of https://git.sr.ht/~metalune/simpleweb_peertube synced 2024-12-22 13:04:52 +01:00

Add basic search page

This commit is contained in:
metalune 2021-01-19 12:13:21 +01:00
parent 49e81ef0e8
commit 104a36deea
3 changed files with 40 additions and 11 deletions

37
main.py
View File

@ -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('/<string:domain>')
@app.route("/<string:domain>/")
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('/<string:domain>/search/<string:term>')
@app.route('/<string:domain>/search', methods=["POST"])
async def search_redirect(domain):
query = (await request.form)["query"]
return redirect("/" + domain + "/search/" + query)
@app.route("/<string:domain>/search/<string:term>")
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('/<string:domain>/watch/<string:id>/')
@app.route("/<string:domain>/watch/<string:id>/")
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()

View File

@ -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)

View File

@ -0,0 +1,10 @@
<!doctype html>
<html>
<body>
<b>{{ instance_name }}</b>
<form action="/{{ domain }}/search" method="POST">
<input type="text" name="query" id="query"/>
<button type="submit">Search</button>
</form>
</body>
</html>