#!/usr/bin/python3 from flask import render_template, Response from flask_sqlalchemy import SQLAlchemy from markupsafe import escape import requests, json import ythdd_globals def homepage(): return "homepage" def home(): return "welcome home!" def index(): return "index" def thumbnailProxy(received_request): # apparently, this can be set to # https://img.youtube.com/ as well prefix = "https://i.ytimg.com/" if received_request.count("/") < 1 or received_request.index("/") != 11: return Response(json.dumps({ 'status': 400, 'error_msg': 'invalid request. pretend this is a thumbnail :D' }), mimetype='application/json', status=400) quality_urls = ['maxresdefault', 'sddefault', 'hqdefault', 'mqdefault', 'default', '1', '2', '3'] video_id, requested_quality = received_request.split('/') thumbnail = requests.get(prefix + "vi/" + video_id + "/" + requested_quality, headers=ythdd_globals.getHeaders(caller='proxy'), stream=True) thumbnail.raw.decode_content = True quality_id = 0 if requested_quality == "maxres.jpg": # if requested quality is maxres, # provide the best quality possible while thumbnail.status_code != 200: thumbnail = requests.get(prefix + "vi/" + video_id + "/" + quality_urls[quality_id] + ".jpg", headers=ythdd_globals.getHeaders(caller='proxy'), stream=True) thumbnail.raw.decode_content = True quality_id += 1 response = Response(thumbnail.raw, mimetype=thumbnail.headers['content-type'], status=thumbnail.status_code) return response def ggphtProxy(received_request): prefix = "https://yt3.ggpht.com/" # fix for how materialious fetches avatars if received_request.startswith("guc/"): return gucProxy(received_request.removeprefix("guc/")) ggpht = requests.get(prefix + received_request, headers=ythdd_globals.getHeaders(caller='proxy'), stream=True) ggpht.raw.decode_content = True 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 def imgProxy(received_request): # will proxy /img/no_thumbnail.jpg prefix = "https://i.ytimg.com/" thumbnail = requests.get(prefix + "img/" + received_request, headers=ythdd_globals.getHeaders(caller='proxy'), stream=True) thumbnail.raw.decode_content = True response = Response(thumbnail.raw, mimetype=thumbnail.headers['content-type'], status=thumbnail.status_code) return response