diff --git a/config.default.toml b/config.default.toml index 85ef49f..f85c8b0 100644 --- a/config.default.toml +++ b/config.default.toml @@ -4,6 +4,7 @@ video_storage_directory_path = "/path/to/videos/" # Path to video vault. is_proxied = false # Set to true if running behind reverse proxy. public_facing_url = "http://localhost:5000/" # Used for URL rewriting. Note the trailing backslash /. debug = false # Whether to print verbose, debug info on API endpoints. +cache = true # Whether to cache requests for 3 hours (temporary solution to long load times). [api] api_key = "" # Leave empty API key for public access to non-sensitive backend diff --git a/ythdd_globals.py b/ythdd_globals.py index f96c533..c3fa4b3 100644 --- a/ythdd_globals.py +++ b/ythdd_globals.py @@ -22,6 +22,7 @@ configfile = "config.toml" version = "0.0.1" apiVersion = "1" randomly_generated_passcode = 0 +video_cache = {} def getConfig(configfile): @@ -30,7 +31,7 @@ def getConfig(configfile): global randomly_generated_passcode if not os.path.exists(configfile): - dummy_config = {'general': {'db_file_path': 'ythdd_db.sqlite', 'video_storage_directory_path': 'videos/', 'is_proxied': False, 'public_facing_url': 'http://localhost:5000/', 'debug': False}, 'api': {'api_key': 'CHANGEME'}, 'extractor': {'user-agent': '', 'cookies_path': ''}, 'admin': {'admins': ['admin']}, 'yt_dlp': {}, 'postprocessing': {'presets': [{'name': 'recommended: [N][<=720p] best V+A', 'format': 'bv[height<=720]+ba', 'reencode': ''}, {'name': '[N][1080p] best V+A', 'format': 'bv[height=1080]+ba', 'reencode': ''}, {'name': '[R][1080p] webm', 'format': 'bv[height=1080]+ba', 'reencode': 'webm'}, {'name': '[N][720p] best V+A', 'format': 'bv[height=720]+ba', 'reencode': ''}, {'name': '[R][720p] webm', 'format': 'bv[height=720]+ba', 'reencode': 'webm'}, {'name': '[N][480p] best V+A', 'format': 'bv[height=480]+ba', 'reencode': ''}, {'name': '[480p] VP9 webm/reencode', 'format': 'bv*[height=480][ext=webm]+ba/bv[height=480]+ba', 'reencode': 'webm'}, {'name': '[N][1080p] best video only', 'format': 'bv[height=1080]', 'reencode': ''}, {'name': '[N][opus] best audio only', 'format': 'ba', 'reencode': 'opus'}]}} + dummy_config = {'general': {'db_file_path': 'ythdd_db.sqlite', 'video_storage_directory_path': 'videos/', 'is_proxied': False, 'public_facing_url': 'http://localhost:5000/', 'debug': False, 'cache': True}, 'api': {'api_key': 'CHANGEME'}, 'extractor': {'user-agent': '', 'cookies_path': ''}, 'admin': {'admins': ['admin']}, 'yt_dlp': {}, 'postprocessing': {'presets': [{'name': 'recommended: [N][<=720p] best V+A', 'format': 'bv[height<=720]+ba', 'reencode': ''}, {'name': '[N][1080p] best V+A', 'format': 'bv[height=1080]+ba', 'reencode': ''}, {'name': '[R][1080p] webm', 'format': 'bv[height=1080]+ba', 'reencode': 'webm'}, {'name': '[N][720p] best V+A', 'format': 'bv[height=720]+ba', 'reencode': ''}, {'name': '[R][720p] webm', 'format': 'bv[height=720]+ba', 'reencode': 'webm'}, {'name': '[N][480p] best V+A', 'format': 'bv[height=480]+ba', 'reencode': ''}, {'name': '[480p] VP9 webm/reencode', 'format': 'bv*[height=480][ext=webm]+ba/bv[height=480]+ba', 'reencode': 'webm'}, {'name': '[N][1080p] best video only', 'format': 'bv[height=1080]', 'reencode': ''}, {'name': '[N][opus] best audio only', 'format': 'ba', 'reencode': 'opus'}]}} # if a passcode has not been provided by the user (config file doesn't exist, and user didn't specify it using an argument) print(f"{colors.WARNING}WARNING{colors.ENDC}: Using default, baked in config data. {colors.ENDL}" f" Consider copying and editing the provided example file ({colors.OKCYAN}config.default.toml{colors.ENDC}).") diff --git a/ythdd_inv_tl.py b/ythdd_inv_tl.py index cd5810a..9d3d64e 100644 --- a/ythdd_inv_tl.py +++ b/ythdd_inv_tl.py @@ -349,6 +349,14 @@ def videos(data): time_start = time() + if ythdd_globals.config['general']['cache'] and data[3] in ythdd_globals.video_cache: + if ythdd_globals.video_cache[data[3]]['cacheTime'] + 3 * 60 > time(): + response = ythdd_globals.video_cache[data[3]] + response['fromCache'] = True + return send(200, response) + else: + del ythdd_globals.video_cache[data[3]] + ydata = ythdd_extractor.extract(data[3], manifest_fix=True) wdata = ythdd_extractor.WEBextractSinglePage(data[3]) @@ -587,6 +595,10 @@ def videos(data): response["ydata"] = ydata response["wdata"] = wdata + if ythdd_globals.config['general']['cache']: + ythdd_globals.video_cache[data[3]] = response + ythdd_globals.video_cache[data[3]]['cacheTime'] = time() + # for debugging: #return send(200, ythdd_extractor.WEBextractSinglePage(data[3])) #return send(200, ythdd_extractor.IOSextract(data[3]))