Implement instance name caching

This commit is contained in:
metalune 2021-01-21 11:14:20 +01:00
parent 0e44673dc6
commit f814d38af9
2 changed files with 20 additions and 5 deletions

22
main.py
View file

@ -2,6 +2,7 @@ from quart import Quart, request, render_template, redirect
import peertube import peertube
commit = "not found" commit = "not found"
with open(".git/refs/heads/main") as file: with open(".git/refs/heads/main") as file:
for line in file: for line in file:
@ -47,8 +48,17 @@ class VideoWrapper:
self.no_quality_selected = not self.video self.no_quality_selected = not self.video
app = Quart(__name__) 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:
return cached_instance_names[domain]
return peertube.get_instance_name(domain)
app = Quart(__name__)
@app.route("/") @app.route("/")
async def main(): async def main():
@ -60,7 +70,7 @@ async def domain_main(domain):
return await render_template( return await render_template(
"domain_index.html", "domain_index.html",
domain=domain, domain=domain,
instance_name=peertube.get_instance_name(domain), instance_name=get_instance_name(domain),
commit=commit, commit=commit,
) )
@ -77,10 +87,12 @@ async def search(domain, term):
return await render_template( return await render_template(
"search_results.html", "search_results.html",
domain=domain, domain=domain,
instance_name=get_instance_name(domain),
commit=commit,
amount=amount, amount=amount,
results=results, results=results,
search_term=term, search_term=term,
commit=commit,
) )
@ -101,11 +113,13 @@ async def video(domain, id):
return await render_template( return await render_template(
"video.html", "video.html",
domain=domain, domain=domain,
commit=commit,
instance_name=get_instance_name(domain),
video=vid, video=vid,
comments=comments, comments=comments,
quality=quality, quality=quality,
embed=embed, embed=embed,
commit=commit,
) )

View file

@ -9,7 +9,8 @@
<body> <body>
<form action="/{{ domain }}/search" method="POST"> <form action="/{{ domain }}/search" method="POST">
<input type="text" name="query" id="query" value="{{ search_term }}"/> <label for="query"><b>{{ instance_name }}</b></label>
<input type="text" name="query" id="query" placeholder="Search" value="{{ search_term }}"/>
<button type="submit">Search</button> <button type="submit">Search</button>
</form> </form>