Parse and return thumbnail URL in server response

This commit is contained in:
MMaker 2025-01-31 13:26:45 -05:00
parent 7111d3cfcb
commit 52de1ece88
Signed by: mmaker
GPG Key ID: CCE79B8FEDA40FB2

16
app.py
View File

@ -1,4 +1,5 @@
import http.cookiejar
import json
from flask import Flask, Response
import requests
from bs4 import BeautifulSoup
@ -27,7 +28,20 @@ def proxy(video_id):
return Response(f"Error fetching the page: {e}", status=500)
soup = BeautifulSoup(r.text, "html.parser")
og_tags = soup.find_all("meta", property=lambda x: x)
thumbnail_url = None
if soup.find("meta", {"name": "server-response"}):
params = json.loads(soup.find("meta", {"name": "server-response"})["content"])["data"]["response"] # type: ignore
thumbnail_url = ( # Use highest quality thumbnail available
params["video"]["thumbnail"]["ogp"]
or params["video"]["thumbnail"]["player"]
or params["video"]["thumbnail"]["largeUrl"]
or params["video"]["thumbnail"]["middleUrl"]
or params["video"]["thumbnail"]["url"]
)
og_tags = soup.find_all("meta", property=lambda x: x) # type: ignore
for tag in og_tags:
if tag.get("property") == "og:image":
tag["content"] = thumbnail_url
og_tags_str = "\n".join(str(tag) for tag in og_tags)
html_response = f"""
<!DOCTYPE html>