all repos — FixYouTube-legacy @ 64fb6246eb911cf967c9b04224ccbbab5e3839aa

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

add duration
Marco Andronaco andronacomarco@gmail.com
Mon, 21 Aug 2023 23:53:29 +0200
commit

64fb6246eb911cf967c9b04224ccbbab5e3839aa

parent

f3d2077af5474ab76e6286695a0c6e8880073d57

4 files changed, 31 insertions(+), 13 deletions(-)

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

@@ -11,6 +11,7 @@ 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,

@@ -27,7 +28,7 @@ def get_video(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, video_ext, height, width, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", list(info.values())) + return execute_query("INSERT OR REPLACE INTO videos (id, title, description, uploader, uploader_id, duration, video_ext, height, width, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", list(info.values())) def get_info(video): result = get_video(video)

@@ -46,10 +47,11 @@ "title": temp[1],

"description": temp[2], "uploader": temp[3], "uploader_id": temp[4], - "video_ext": temp[5], - "height": temp[6], - "width": temp[7], - "url": temp[8], + "duration": temp[5], + "video_ext": temp[6], + "height": temp[7], + "width": temp[8], + "url": temp[9], } except IndexError:
M fxyoutube/templates/base.htmlfxyoutube/templates/base.html

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

<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="twitter:image" content="0" /> <meta property="og:title" content="{{ info['title'] }}" />
M fxyoutube/views.pyfxyoutube/views.py

@@ -46,5 +46,8 @@ ext = result[0][5]

except IndexError: return abort(400) + if ext is None: + return abort(400) + result = get(url) return Response(result.content, headers={ "Content-Type": "video/" + ext })
M fxyoutube/yt_info.pyfxyoutube/yt_info.py

@@ -29,21 +29,33 @@ return "\n".join(input_str.splitlines()[:5])

def get_info_ytdl(yt_id: str): info = ydl.extract_info(c.BASE_URL + yt_id, download=False) - 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"]) - except ValueError: - return None - return { + yt_info = { "id": info["id"], "title": info["title"], "description": truncate_lines(info["description"]), "uploader": info["uploader"], "uploader_id": info["uploader_id"], + "duration": info["duration"], + } + + 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"]) + except ValueError: + yt_info.update({ + "video_ext": None, + "height": 0, + "width": 0, + "url": None + }) + + yt_info.update({ "video_ext": max_format["video_ext"], "height": max_format["height"], "width": max_format["width"], "url": max_format["url"], - } + }) + + return yt_info