all repos — FixYouTube-legacy @ 5cd2dc1a0ae3b189e733adba30526e75e3b171d7

A better way to embed YouTube videos everywhere (inspired by FixTweet).

use yt-dlp's filtering instead
Marco Andronaco andronacomarco@gmail.com
Wed, 23 Aug 2023 13:39:17 +0200
commit

5cd2dc1a0ae3b189e733adba30526e75e3b171d7

parent

942e0d577869657273327c2480cf05eb5b6758f7

4 files changed, 20 insertions(+), 42 deletions(-)

jump to
M fxyoutube/constants.pyfxyoutube/constants.py

@@ -6,5 +6,5 @@ UA_REGEX = r"bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node"

BASE_URL = "https://www.youtube.com/watch?v=" REPO_URL = "https://github.com/BiRabittoh/FixYouTube" TS_FORMAT = "%Y-%m-%d %H:%M:%S" -MAX_SIZE_BYTES = MAX_SIZE_MB * 1_000_000 PROXY_HEADERS = { "Content-Type": "video/mp4" } +YTDL_OPTS = { "format": f"best[ext=mp4][filesize<{ MAX_SIZE_MB }M][protocol^=http][protocol!*=dash]" }
M fxyoutube/templates/base.htmlfxyoutube/templates/base.html

@@ -18,6 +18,7 @@ <meta property="twitter:site" content="{{ info['uploader'] }}" />

<meta property="twitter:creator" content="{{ info['uploader'] }} ({{ info['uploader_id'] }})" /> <meta property="twitter:title" content="{{ info['title'] }}" /> <meta http-equiv="refresh" content="0;url={{ base_url }}{{ info['id'] }}" /> + {% if info['url'] != "" %} <meta property="twitter:player:stream:content_type" content="video/mp4" /> <meta property="twitter:player:height" content="{{ info['height'] }}" /> <meta property="twitter:player:width" content="{{ info['width'] }}" />

@@ -27,6 +28,7 @@ <meta property="og:video:height" content="{{ info['height'] }}" />

<meta property="og:video:width" content="{{ info['width'] }}" /> <meta property="og:video:duration" content="{{ info['duration'] }}"> <meta property="og:video:type" content="video/mp4" /> + {% endif %} <meta property="twitter:image" content="0" /> <meta property="og:title" content="{{ info['title'] }}" /> <meta property="og:description" content="{{ info['description'] }}" />
M fxyoutube/views.pyfxyoutube/views.py

@@ -44,8 +44,8 @@

if result is None: return abort(400) - url = result["url"] - if url is None: + url = result.url + if url == "": return abort(400) return Response(get(url).content, headers=c.PROXY_HEADERS)
M fxyoutube/yt_info.pyfxyoutube/yt_info.py

@@ -1,46 +1,22 @@

-from yt_dlp import YoutubeDL +from yt_dlp import YoutubeDL, DownloadError import fxyoutube.constants as c -ydl = YoutubeDL() -def handle_format(format): - if format["resolution"] == "audio only": - return None # audio-only - try: - if format["audio_channels"] is None: - return None # video-only - except KeyError: - return None # video-only - - if format["url"].endswith(".m3u8"): - return None # HLS stream - - if format["video_ext"] != "mp4": - return None - - try: - if format["filesize"] > c.MAX_SIZE_BYTES: - return None # too large - except TypeError: - if format["filesize_approx"] > c.MAX_SIZE_BYTES: - return None # too large - - return format +ydl_keys = ["id", "title", "description", "uploader", "duration", "height", "width", "url"] -def truncate_lines(input_str: str, max: int = 5): +def truncate_lines(input_str: str, max: int = 4): return "\n".join(input_str.splitlines()[:max]) def get_info_ytdl(yt_id: str): - info = ydl.extract_info(c.BASE_URL + yt_id, download=False) - - yt_info = { k: truncate_lines(info[k]) for k in ["id", "title", "description", "uploader", "duration"] } - yt_info.update({ "height": 0, "width": 0, "url": None }) - - formats = map(handle_format, info["formats"]) - formats = filter(lambda x: x is not None, formats) try: - max_format = max(formats, key=lambda x:x["quality"]) - yt_info.update({ k: max_format[k] for k in ["height", "width", "url"] }) - except ValueError: - pass - finally: - return yt_info + with YoutubeDL(c.YTDL_OPTS) as ydl: + info = ydl.extract_info(c.BASE_URL + yt_id, download=False) + yt_info = { k: info[k] for k in ydl_keys } + except DownloadError: + with YoutubeDL() as ydl: + info = ydl.extract_info(c.BASE_URL + yt_id, download=False) + yt_info = { k: info[k] for k in ydl_keys[:-3] } + yt_info["height"] = yt_info["width"] = 0 + yt_info["url"] = "" + + yt_info["description"] = truncate_lines(yt_info["description"]) + return yt_info