Clean download code

This commit is contained in:
Blondel Mondésir 2023-10-12 23:03:02 -04:00
parent dd99701117
commit 4638da278c
4 changed files with 12 additions and 146 deletions

View File

@ -342,19 +342,10 @@ def youtube():
log.error(error_message)
return False, error_message
# def process_youtube_download(youtube_url, video_quality):
def process_youtube_download(youtube_url):
yb_executable = get_yb_executable()
if youtube_url:
# youtube_id = extract_youtube_url(youtube_url)
# download_args = [
# yb_executable,
# youtube_id,
# "/output",
# video_quality,
# ]
download_args = [
yb_executable,
youtube_url,
@ -364,15 +355,6 @@ def youtube():
log.info("Subprocess result: {}".format(subprocess_result))
if subprocess_result[0]:
# log.info("Renaming files in /output/{}/videos".format(youtube_id))
# # make a list of requested files with names of directories found in /output/<youtube_id>/videos
# requested_files = os.listdir("/output/{}/videos".format(youtube_id))
# # remove "youtube-nsig" from the list of requested files
# requested_files.remove("youtube-nsig")
# make a list of requested files with paths found in media table reading /var/tmp/download.db
# select each "path" from media table to get a list of requested files
# each "path" is an absolute path to a file in /var/tmp/Youtube/<author>/<title>/<file>
requested_files = []
download_db_path = "/var/tmp/download.db"
conn = sqlite3.connect(download_db_path)
@ -383,8 +365,6 @@ def youtube():
conn.close()
log.info("Requested files: {}".format(requested_files))
# renamed_files = rename_files(requested_files, youtube_id)
# if renamed_files:
for requested_file in requested_files:
requested_file = open(requested_file, "rb")
requested_file.filename = os.path.basename(requested_file.name)
@ -472,9 +452,7 @@ def youtube():
if request.method == "POST" and "youtubeURL" in request.form:
youtube_url = request.form["youtubeURL"]
# video_quality = request.form.get("videoQuality", "720")
# if process_youtube_download(youtube_url, video_quality):
if process_youtube_download(youtube_url):
response = {
"success": "Downloaded YouTube media successfully",
@ -486,66 +464,6 @@ def youtube():
}
return jsonify(response), 500
# def extract_youtube_url(url):
# try:
# if "youtube.com" in url:
# if "watch?v=" in url:
# return url.split("watch?v=")[1]
# elif "playlist?list=" in url:
# return url.split("playlist?list=")[1]
# elif "channel/" in url:
# return url.split("channel/")[1]
# elif "user/" in url:
# return url.split("user/")[1]
# elif "@" in url:
# return extract_channel_id_from_handle(url)
# elif "youtu.be" in url:
# return url.split("youtu.be/")[1]
# flash("Error: Invalid YouTube URL", category="error")
# return None
# except Exception as e:
# flash("An error occurred while processing the YouTube URL: {}".format(e), category="error")
# return None
# def extract_channel_id_from_handle(url):
# handle = url.split("@")[1]
# operational_api_url = "https://yt.lemnoslife.com/channels?handle=" + handle
# response = requests.get(operational_api_url)
# if response.status_code == 200:
# return response.json()["items"][0]["id"]
# else:
# flash("Error: Failed to retrieve YouTube channel ID from API", category="error")
# return None
# def rename_files(requested_files, youtube_id):
# # cache_directory_path = "/output/{}/cache".format(youtube_id)
# video_dir_path = "/output/{}/videos".format(youtube_id)
# renamed_files = []
# if not os.path.exists("/tmp/calibre_web"):
# os.makedirs("/tmp/calibre_web")
# for video_id in requested_files:
# # video_json_path = os.path.join(cache_directory_path, "videos.json")
# video_webm_path = os.path.join(video_dir_path, video_id, "video.webm")
# thumbnail_path = os.path.join(video_dir_path, video_id, "video.webp")
# try:
# thumbnail_path_new = os.path.join("/tmp", "calibre_web", "{}.webp".format(video_id + youtube_id))
# move(thumbnail_path, thumbnail_path_new)
# video_webm_path_new = os.path.join("/tmp", "calibre_web", "{}.webm".format(video_id + youtube_id))
# move(video_webm_path, video_webm_path_new)
# renamed_files.append(video_webm_path_new)
# if not os.listdir(video_dir_path):
# os.rmdir(video_dir_path)
# except Exception as e:
# flash("An error occurred while renaming the YouTube video file: {}".format(e), category="error")
# return None
# return renamed_files
@editbook.route("/admin/book/convert/<int:book_id>", methods=['POST'])
@login_required_if_no_ano
@ -996,19 +914,10 @@ def move_coverfile(meta, db_book):
try:
os.makedirs(new_cover_path, exist_ok=True)
# image = Image.open(cover_file)
# # crop image to square 150x150 using smartcrop
# cropper = smartcrop.SmartCrop()
# result = cropper.crop(image, 150, 150)
# x, y, width, height = result['top_crop']['x'], result['top_crop']['y'], \
# result['top_crop']['width'], result['top_crop']['height']
# cropped_image = image.crop((x, y, x + width, y + height))
# cropped_image.save(os.path.join(new_cover_path, "cover.jpg"))
move(cover_file, os.path.join(new_cover_path, "cover.jpg"))
if meta.cover:
os.unlink(meta.cover)
# if meta.cover:
# os.unlink(meta.cover)
except OSError as e:
log.error("Failed to move cover file %s: %s", new_cover_path, e)
flash(_("Failed to Move Cover File %(file)s: %(error)s", file=new_cover_path,

View File

@ -180,9 +180,9 @@ $(document).ready(function() {
maxVideos = maxVideos === "" ? 0 : parseInt(maxVideos);
maxVideosSize = maxVideosSize === "" ? 0 : parseInt(maxVideosSize);
// Check if the input URL is a valid YouTube URL
if (!isValidYoutubeURL(url)) {
alert("Invalid YouTube URL");
// Check if the input URL is a valid URL
if (!isValidURL(url)) {
alert("Invalid URL.");
return;
}
@ -270,10 +270,10 @@ $(document).ready(function() {
// Handle change event
});
// Function to validate YouTube URL (updated to handle https://youtube.com/@handle)
function isValidYoutubeURL(url) {
var youtubeURLPattern = /^(https?:\/\/)?(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/playlist\?list=|youtube\.com\/channel\/|youtube\.com\/@)([a-zA-Z0-9_-]{11}|[a-zA-Z0-9_-]{34})/;
return youtubeURLPattern.test(url);
// Function to validate URL
function isValidURL(url) {
var urlPattern = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
return urlPattern.test(url);
}
});

View File

@ -148,7 +148,7 @@
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="youtubeDownloadModalLabel">{{_('Download a video to your
<h4 class="modal-title" id="youtubeDownloadModalLabel">{{_('Download video(s) to your
Internet-in-a-Box')}}</h4>
</div>
<div class="modal-body">

View File

@ -250,34 +250,8 @@ def pdf_preview(tmp_file_path, tmp_dir):
def video_metadata(tmp_file_path, original_file_name, original_file_extension):
# video_id = os.path.splitext(original_file_name)[0][:11]
# youtube_id = os.path.splitext(original_file_name)[0][11:]
# json_file_path = os.path.join("/output", youtube_id, "cache", "videos.json")
# coverfile_path = os.path.splitext(original_file_name)[0] + '.webp'
# if os.path.isfile(coverfile_path):
# coverfile_path = os.path.splitext(tmp_file_path)[0] + '.cover.webp'
# os.rename(os.path.splitext(original_file_name)[0] + '.webp', coverfile_path)
# os.remove(os.path.splitext(original_file_name)[0] + '.webm')
# return coverfile_path
# if os.path.isfile(json_file_path):
# with open(json_file_path) as json_file:
# data = json.load(json_file)
# title = data[video_id]['snippet']['title']
# author = data[video_id]['snippet']['videoOwnerChannelTitle']
# description = data[video_id]['snippet']['description']
# publisher = 'YouTube'
# pubdate = data[video_id]['contentDetails']['videoPublishedAt'][0:10]
# original file name example: Baby_Calm_Down_FULL_HD_Selena_Gomez_Rema_Official_Music_Video_2023_600.22k_[yJ_qhwk2X2c].webm
# extract video id from original file name by extracting the string between the brackets "[...]"
# if there is a "]" before the extension:
if ']' in original_file_name:
video_id = original_file_name.split('[')[1].split(']')[0]
# with the video_id, we can get the video info from /var/tmp/download.db (sqlite3 database)
# Here are the columns from the database's media table:
# id, size, duration, time_uploaded, time_created, time_modified, time_deleted, time_downloaded, fps, view_count, path, webpath, extractor_id, title, live_status, uploader, width, height, type, video_count, audio_count, language, description, playlist_id, chapter_count, chapters
# we need the extractor_id (the video_id), title, uploader, description, time_uploaded
# we need to check if the video_id is in the database and access the columns we need to get the metadata for BookMeta
download_db_path = "/var/tmp/download.db"
if os.path.isfile(download_db_path):
conn = sqlite3.connect(download_db_path)
@ -286,7 +260,7 @@ def video_metadata(tmp_file_path, original_file_name, original_file_extension):
row = c.fetchone()
if row is not None:
title = row[13]
author = row[15]
author = row[10].split('/Youtube/')[1].replace('_', ' ')
description = row[22]
publisher = 'YouTube'
# example of time_uploaded: 1696464000
@ -316,23 +290,6 @@ def video_metadata(tmp_file_path, original_file_name, original_file_extension):
else:
log.warning('Cannot find download.db, using default')
else:
# ffmpeg_executable = os.getenv('FFMPEG_PATH', 'ffmpeg')
# ffmpeg_output_file = os.path.splitext(tmp_file_path)[0] + '.cover.jpg'
# ffmpeg_args = [
# ffmpeg_executable,
# '-i', tmp_file_path,
# '-vframes', '1',
# '-y', ffmpeg_output_file
# ]
# try:
# ffmpeg_result = run(ffmpeg_args, capture_output=True, check=True)
# log.debug(f"ffmpeg output: {ffmpeg_result.stdout}")
# except Exception as e:
# log.warning(f"ffmpeg failed: {e}")
# return None
meta = BookMeta(
file_path=tmp_file_path,
extension=original_file_extension,