From d1b9f90e7ed76798837f9f9a62a69088559ec806 Mon Sep 17 00:00:00 2001 From: sherl Date: Fri, 28 Feb 2025 00:56:13 +0100 Subject: [PATCH] add support for GUC proxy, move safeTraverse to ythdd_globals --- views.py | 10 ++++++++++ ythdd.py | 1 + ythdd_extractor.py | 1 + ythdd_globals.py | 15 ++++++++++++++- ythdd_inv_tl.py | 12 +----------- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/views.py b/views.py index fecb393..c09b126 100644 --- a/views.py +++ b/views.py @@ -41,3 +41,13 @@ def ggphtProxy(received_request): response = Response(ggpht.raw, mimetype=ggpht.headers['content-type'], status=ggpht.status_code) return response + +def gucProxy(received_request): + + prefix = "https://yt3.googleusercontent.com/" + + guc = requests.get(prefix + received_request, headers=ythdd_globals.getHeaders(caller='proxy'), stream=True) + guc.raw.decode_content = True + response = Response(guc.raw, mimetype=guc.headers['content-type'], status=guc.status_code) + + return response \ No newline at end of file diff --git a/ythdd.py b/ythdd.py index bd0426f..f1ea867 100644 --- a/ythdd.py +++ b/ythdd.py @@ -62,6 +62,7 @@ def setup(): app.add_url_rule('/api/', view_func=ythdd_api.api_global_catchall) app.add_url_rule('/vi/', view_func=views.thumbnailProxy) app.add_url_rule('/ggpht/', view_func=views.ggphtProxy) + app.add_url_rule('/guc/', view_func=views.gucProxy) db = ythdd_db.initDB(app, config) with app.app_context(): diff --git a/ythdd_extractor.py b/ythdd_extractor.py index 19c892a..deb1674 100644 --- a/ythdd_extractor.py +++ b/ythdd_extractor.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 import brotli, yt_dlp, requests, json, time +from ythdd_globals import safeTraverse import ythdd_globals ytdl_opts = { diff --git a/ythdd_globals.py b/ythdd_globals.py index ed18f18..3638acd 100644 --- a/ythdd_globals.py +++ b/ythdd_globals.py @@ -86,8 +86,21 @@ def translateLinks(link): link = link.replace("https://i.ytimg.com/", config['general']['public_facing_url']) link = link.replace("https://yt3.ggpht.com/", config['general']['public_facing_url'] + "ggpht/") + link = link.replace("https://yt3.googleusercontent.com/", config['general']['public_facing_url'] + "guc/") return link def getUptime(): - return int(time.time()) - starttime \ No newline at end of file + return int(time.time()) - starttime + +def safeTraverse(obj: dict, path: list, default=None): + result = obj + try: + for x in path: + #print(f"traversing {result} with respect to {x}") + result = result[x] + except KeyError: + result = default + print(f"error reading: {' -> '.join(path)} - returning: {default}") + finally: + return result diff --git a/ythdd_inv_tl.py b/ythdd_inv_tl.py index 9217849..01b55b2 100644 --- a/ythdd_inv_tl.py +++ b/ythdd_inv_tl.py @@ -6,6 +6,7 @@ from flask import Response, request, redirect from markupsafe import escape from time import strftime, gmtime, time +from ythdd_globals import safeTraverse import json, datetime import invidious_formats import ythdd_globals @@ -78,17 +79,6 @@ def trending(): def popular(): return send(200, [{}]) -def safeTraverse(obj: dict, path: list, default=None): - result = obj - try: - for x in path: - result = result[x] - except KeyError: - result = default - print(f"error reading: {' -> '.join(path)} - returning: {default}") - finally: - return result - def getError(idata: dict): unknown_error = {"status": "Unknown error", "reason": "This is a generic ythdd error."} error = ""