all repos — FixYouTube-legacy @ 426c6dead1e6a32ff0ed779990527d5e08011927

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

video_ext = mp4 as it's the only supported type
Marco Andronaco andronacomarco@gmail.com
Tue, 22 Aug 2023 14:17:43 +0200
commit

426c6dead1e6a32ff0ed779990527d5e08011927

parent

83b48d6e202262d67446f68e0edf5357fcf215a4

M fxyoutube/constants.pyfxyoutube/constants.py

@@ -7,3 +7,4 @@ 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" }
M fxyoutube/db.pyfxyoutube/db.py

@@ -10,12 +10,10 @@ id TEXT PRIMARY KEY,

title TEXT NOT NULL, description TEXT NOT NULL, uploader TEXT NOT NULL, - uploader_id TEXT NOT NULL, duration int NOT NULL, - video_ext TEXT NOT NULL, height TEXT NOT NULL, width TEXT NOT NULL, - url TEXT NOT NULL, + url TEXT, timestamp DATETIME DEFAULT (datetime('now','localtime')) );'''

@@ -28,7 +26,7 @@ def get_video_db(video_id):

return execute_query("SELECT * FROM videos WHERE id = (?);", [ video_id ]) def cache_video(info): - return execute_query("INSERT OR REPLACE INTO videos (id, title, description, uploader, uploader_id, duration, video_ext, height, width, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", list(info.values())) + return execute_query("INSERT OR REPLACE INTO videos (id, title, description, uploader, duration, height, width, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", list(info.values())) def get_video_from_cache(video): result = get_video_db(video)

@@ -36,7 +34,7 @@ try:

temp = result[0] except IndexError: return None - timestamp = datetime.strptime(temp[10], c.TS_FORMAT) + timestamp = datetime.strptime(temp[8], c.TS_FORMAT) delta = datetime.now() - timestamp if delta > timedelta(minutes=c.YT_TTL_MINUTES):

@@ -47,12 +45,10 @@ "id": temp[0],

"title": temp[1], "description": temp[2], "uploader": temp[3], - "uploader_id": temp[4], - "duration": temp[5], - "video_ext": temp[6], - "height": temp[7], - "width": temp[8], - "url": temp[9], + "duration": temp[4], + "height": temp[5], + "width": temp[6], + "url": temp[7], } def get_info(video):
M fxyoutube/templates/base.htmlfxyoutube/templates/base.html

@@ -18,7 +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'] }}" /> - <meta property="twitter:player:stream:content_type" content="video/{{ info['video_ext'] }}" /> + <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'] }}" /> <meta property="og:video" content="/proxy/{{ info['id'] }}" />

@@ -26,7 +26,7 @@ <meta property="og:video:secure_url" content="/proxy/{{ info['id'] }}" />

<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/{{ info['video_ext'] }}" /> + <meta property="og:video:type" content="video/mp4" /> <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) - if result["video_ext"] is None: + url = result["url"] + if url is None: return abort(400) - req = get(result["url"]) - return Response(req.content, headers={ "Content-Type": "video/" + result["video_ext"] }) + return Response(get(url).content, headers=c.PROXY_HEADERS)
M fxyoutube/yt_info.pyfxyoutube/yt_info.py

@@ -14,6 +14,9 @@

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

@@ -34,7 +37,6 @@ "id": info["id"],

"title": info["title"], "description": truncate_lines(info["description"]), "uploader": info["uploader"], - "uploader_id": info["uploader_id"], "duration": info["duration"], }

@@ -44,7 +46,6 @@ try:

max_format = max(formats, key=lambda x:x["quality"]) except ValueError: yt_info.update({ - "video_ext": None, "height": 0, "width": 0, "url": None

@@ -52,9 +53,9 @@ })

return yt_info yt_info.update({ - "video_ext": max_format["video_ext"], "height": max_format["height"], "width": max_format["width"], "url": max_format["url"], }) + return yt_info