From b962dacf7be6b606c23a0293bd5b64f6b714e1e6 Mon Sep 17 00:00:00 2001 From: Tangent Date: Tue, 20 Feb 2024 01:23:37 -0700 Subject: [PATCH] subtitle downloader is better --- download-subtitles.lua | 49 +++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/download-subtitles.lua b/download-subtitles.lua index e775a76..e3c5e4b 100644 --- a/download-subtitles.lua +++ b/download-subtitles.lua @@ -1,12 +1,45 @@ -local source = "URL" +#!/usr/bin/env luajit -os.execute("yt-dlp --flat-playlist --print-to-file \"%(id)s\" download-list.txt --skip-download \"" .. source .. "\"") -os.execute("yt-dlp --flat-playlist --print-to-file \"%(id)s=%(title)s\" VIDEO-TITLES.txt --skip-download \"" .. source .. "\"") +local source = arg[1] +assert(source and #source > 0, "Specify a URL!") + +local function make_safe(file_name) + -- return file_name:gsub("[%w%s%_%-%,%.%[%]%(%)%'%+]", "") -- this is the literal opposite of what I needed, oops + -- return file_name:gsub("[%\"%:%\\%!%@%#%$%%%^%&%*%=%{%}%|%;%<%>%?%/]", "") -- this should handle everything but I'm not certain! :D + + file_name = file_name:gsub("[%\"%:%\\%!%@%#%$%%%^%*%=%{%}%|%;%<%>%?%/]", "") -- everything except the & + file_name = file_name:gsub(" %&", ",") -- replacing & with a comma works for 99% of things + file_name = file_name:gsub("%&", ",") -- replacing & with a comma works for 99% of things + file_name = file_name:gsub("[%s+]", " ") -- more than one space in succession should be a single space + return file_name +end + +local function download_subtitle(id, file_name) + os.execute("sleep 6") -- up-front in case chromium isn't ready / delay from previous download to prevent rate limit issues + os.execute("chromium \"https://www.downloadyoutubesubtitles.com/?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D" .. id .. "\"") + os.execute("sleep 12") + os.execute("curl \"https://www.downloadyoutubesubtitles.com/get2.php?i=" .. id .. "&format=txt&hl=en&a=\" > \"./subtitles/" .. (file_name or id) .. ".txt\"") +end + +os.execute("pwsh -command chromium") -- open browser first, to make sure it is ready by the time we start using it + +os.execute("yt-dlp --flat-playlist --print-to-file \"%(id)s=%(title)s\" videos.txt --skip-download \"" .. source .. "\"") os.execute("mkdir subtitles") -for line in io.lines("download-list.txt") do - os.execute("chromium \"https://www.downloadyoutubesubtitles.com/?u=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D" .. line .. "\"") - os.execute("sleep 12") - os.execute("curl \"https://www.downloadyoutubesubtitles.com/get2.php?i=" .. line .. "&format=txt&hl=en&a=\" > \"./subtitles/" .. line .. ".txt\"") - os.execute("sleep 6") +local videos = {} + +local file = io.open("videos.txt", "r") +assert(file, "videos.txt doesn't exist.. somehow") +for line in file:lines() do + local i, j = line:find("%=") + videos[#videos + 1] = { + id = line:sub(1, i - 1), + title = line:sub(j + 1), + } +end +file:close() + +for index, video in ipairs(videos) do + print(index, #videos, video.title) + download_subtitle(video.id, make_safe(video.title)) end