more polish
Marco Andronaco andronacomarco@gmail.com
Tue, 22 Aug 2023 08:54:26 +0200
4 files changed,
52 insertions(+),
41 deletions(-)
M
fxyoutube/db.py
→
fxyoutube/db.py
@@ -24,40 +24,46 @@ with sqlite3.connect(c.DB_URL) as db_connection:
with closing(db_connection.cursor()) as db_cursor: return list(db_cursor.execute(query, attributes)) -def get_video(video_id): +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())) -def get_info(video): - result = get_video(video) - +def get_video_from_cache(video): + result = get_video_db(video) try: temp = result[0] - timestamp = datetime.strptime(temp[10], c.TS_FORMAT) - delta = datetime.now() - timestamp + except IndexError: + return None + timestamp = datetime.strptime(temp[10], c.TS_FORMAT) + delta = datetime.now() - timestamp - if delta > timedelta(minutes=c.YT_TTL_MINUTES): - raise IndexError + if delta > timedelta(minutes=c.YT_TTL_MINUTES): + raise IndexError - info = { - "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], - } + return { + "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], + } + +def get_info(video): + info = get_video_from_cache(video) - except IndexError: - info = get_info_ytdl(video) - if info is not None: - cache_video(info) + if info is not None: + return info + + info = get_info_ytdl(video) + if info is not None: + cache_video(info) return info
M
fxyoutube/templates/base.html
→
fxyoutube/templates/base.html
@@ -1,5 +1,15 @@
<!DOCTYPE html> -<html lang="und"> +<html lang="und"><!-- + +███████ ██ ██ ██ ██ ██ ███ ██ ██ ██████ ██ ██ ██████ ███████ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +█████ ██ ███ ████ ██ ██ ██ ██ ██ ██ ██ ██████ █████ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +██ ██ ██ ██ ██ ███ ███ ██ ███ ██████ ███████ +██ +██ A better way to embed YouTube videos on Telegram (inspired by FixTweet). +██ +--> <head> <link rel="canonical" href="{{ base_url }}{{ info['id'] }}" /> <meta property="theme-color" content="0000FF" />@@ -7,7 +17,7 @@ <meta property="twitter:card" content="player" />
<meta property="twitter:site" content="{{ info['uploader'] }}" /> <meta property="twitter:creator" content="{{ info['uploader'] }} ({{ info['uploader_id'] }})" /> <meta property="twitter:title" content="{{ info['title'] }}" /> - <me ta http-equiv="refresh" content="0;url={{ base_url }}{{ info['id'] }}" /> + <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:height" content="{{ info['height'] }}" /> <meta property="twitter:player:width" content="{{ info['width'] }}" />@@ -21,6 +31,4 @@ <meta property="twitter:image" content="0" />
<meta property="og:title" content="{{ info['title'] }}" /> <meta property="og:description" content="{{ info['description'] }}" /> <meta property="og:site_name" content="FixYouTube ({{ info['uploader'] }})" /> -</head> -<body></body> -</html> +</head><body></body></html>
M
fxyoutube/views.py
→
fxyoutube/views.py
@@ -1,5 +1,5 @@
from fxyoutube import app -from fxyoutube.db import get_video, get_info, clear_cache +from fxyoutube.db import get_video_from_cache, get_info, clear_cache import fxyoutube.constants as c from flask import request, redirect, abort, render_template, Response@@ -39,15 +39,13 @@
@app.route('/proxy/', defaults={'path': ''}) @app.route('/proxy/<path:path>') def proxy(path): - result = get_video(path) - try: - url = result[0][9] - ext = result[0][6] - except IndexError: + result = get_video_from_cache(path) + + if result is None: return abort(400) - if ext is None: + if result["video_ext"] is None: return abort(400) - - result = get(url) - return Response(result.content, headers={ "Content-Type": "video/" + ext }) + + req = get(result["url"]) + return Response(req.content, headers={ "Content-Type": "video/" + result["video_ext"] })
M
fxyoutube/yt_info.py
→
fxyoutube/yt_info.py
@@ -3,7 +3,6 @@ import fxyoutube.constants as c
ydl = YoutubeDL() def handle_format(format): - if format["resolution"] == "audio only": return None # audio-only try:@@ -50,6 +49,7 @@ "height": 0,
"width": 0, "url": None }) + return yt_info yt_info.update({ "video_ext": max_format["video_ext"],@@ -57,5 +57,4 @@ "height": max_format["height"],
"width": max_format["width"], "url": max_format["url"], }) - print(max_format["url"]) return yt_info