add support for GUC proxy, move safeTraverse to ythdd_globals

This commit is contained in:
2025-02-28 00:56:13 +01:00
parent 019e47edd9
commit d1b9f90e7e
5 changed files with 27 additions and 12 deletions

View File

@@ -41,3 +41,13 @@ def ggphtProxy(received_request):
response = Response(ggpht.raw, mimetype=ggpht.headers['content-type'], status=ggpht.status_code) response = Response(ggpht.raw, mimetype=ggpht.headers['content-type'], status=ggpht.status_code)
return response 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

View File

@@ -62,6 +62,7 @@ def setup():
app.add_url_rule('/api/<path:received_request>', view_func=ythdd_api.api_global_catchall) app.add_url_rule('/api/<path:received_request>', view_func=ythdd_api.api_global_catchall)
app.add_url_rule('/vi/<path:received_request>', view_func=views.thumbnailProxy) app.add_url_rule('/vi/<path:received_request>', view_func=views.thumbnailProxy)
app.add_url_rule('/ggpht/<path:received_request>', view_func=views.ggphtProxy) app.add_url_rule('/ggpht/<path:received_request>', view_func=views.ggphtProxy)
app.add_url_rule('/guc/<path:received_request>', view_func=views.gucProxy)
db = ythdd_db.initDB(app, config) db = ythdd_db.initDB(app, config)
with app.app_context(): with app.app_context():

View File

@@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import brotli, yt_dlp, requests, json, time import brotli, yt_dlp, requests, json, time
from ythdd_globals import safeTraverse
import ythdd_globals import ythdd_globals
ytdl_opts = { ytdl_opts = {

View File

@@ -86,8 +86,21 @@ def translateLinks(link):
link = link.replace("https://i.ytimg.com/", config['general']['public_facing_url']) 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.ggpht.com/", config['general']['public_facing_url'] + "ggpht/")
link = link.replace("https://yt3.googleusercontent.com/", config['general']['public_facing_url'] + "guc/")
return link return link
def getUptime(): def getUptime():
return int(time.time()) - starttime 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

View File

@@ -6,6 +6,7 @@
from flask import Response, request, redirect from flask import Response, request, redirect
from markupsafe import escape from markupsafe import escape
from time import strftime, gmtime, time from time import strftime, gmtime, time
from ythdd_globals import safeTraverse
import json, datetime import json, datetime
import invidious_formats import invidious_formats
import ythdd_globals import ythdd_globals
@@ -78,17 +79,6 @@ def trending():
def popular(): def popular():
return send(200, [{}]) 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): def getError(idata: dict):
unknown_error = {"status": "Unknown error", "reason": "This is a generic ythdd error."} unknown_error = {"status": "Unknown error", "reason": "This is a generic ythdd error."}
error = "" error = ""