From f814d38af980cdfe82595ed81d601dc471255d5c Mon Sep 17 00:00:00 2001 From: metalune Date: Thu, 21 Jan 2021 11:14:20 +0100 Subject: [PATCH] Implement instance name caching --- main.py | 22 ++++++++++++++++++---- templates/base.html | 3 ++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 0925405..7f48391 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from quart import Quart, request, render_template, redirect import peertube + commit = "not found" with open(".git/refs/heads/main") as file: for line in file: @@ -47,8 +48,17 @@ class VideoWrapper: 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("/") async def main(): @@ -60,7 +70,7 @@ async def domain_main(domain): return await render_template( "domain_index.html", domain=domain, - instance_name=peertube.get_instance_name(domain), + instance_name=get_instance_name(domain), commit=commit, ) @@ -77,10 +87,12 @@ async def search(domain, term): return await render_template( "search_results.html", domain=domain, + instance_name=get_instance_name(domain), + commit=commit, + amount=amount, results=results, search_term=term, - commit=commit, ) @@ -101,11 +113,13 @@ async def video(domain, id): return await render_template( "video.html", domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + video=vid, comments=comments, quality=quality, embed=embed, - commit=commit, ) diff --git a/templates/base.html b/templates/base.html index c6a95d7..c6f88b6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,7 +9,8 @@
- + +