Parse and return thumbnail URL in server response
This commit is contained in:
parent
7111d3cfcb
commit
52de1ece88
16
app.py
16
app.py
|
@ -1,4 +1,5 @@
|
||||||
import http.cookiejar
|
import http.cookiejar
|
||||||
|
import json
|
||||||
from flask import Flask, Response
|
from flask import Flask, Response
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
@ -27,7 +28,20 @@ def proxy(video_id):
|
||||||
return Response(f"Error fetching the page: {e}", status=500)
|
return Response(f"Error fetching the page: {e}", status=500)
|
||||||
|
|
||||||
soup = BeautifulSoup(r.text, "html.parser")
|
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)
|
og_tags_str = "\n".join(str(tag) for tag in og_tags)
|
||||||
html_response = f"""
|
html_response = f"""
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user