From acf718c676f732840d50f9bd399bb59f60c0e205 Mon Sep 17 00:00:00 2001 From: metalune Date: Thu, 21 Jan 2021 11:22:09 +0100 Subject: [PATCH] Re-send instance name requests every day when they're needed --- main.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 7f48391..20d1c57 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,7 @@ from quart import Quart, request, render_template, redirect +from datetime import datetime import peertube - - commit = "not found" with open(".git/refs/heads/main") as file: for line in file: @@ -48,14 +47,35 @@ class VideoWrapper: self.no_quality_selected = not self.video +# Format: +# key: domain +# entry: [ instance_name, last_time_updated ] 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) + 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] + + + + app = Quart(__name__)