yt-dlp download/update logic fixes

release
aandrew-me 6 days ago
parent 217c10967b
commit f81ed1c7ac

@ -277,49 +277,43 @@ class YtDownloaderApp {
const defaultYtDlpName = platform() === "win32" ? "ytdlp.exe" : "ytdlp"; const defaultYtDlpName = platform() === "win32" ? "ytdlp.exe" : "ytdlp";
const defaultYtDlpPath = join(hiddenDir, defaultYtDlpName); const defaultYtDlpPath = join(hiddenDir, defaultYtDlpName);
const isMacOS = platform() === "darwin"; const isMacOS = platform() === "darwin";
const isFreeBSD = platform() === "freebsd";
// Priority 1: Environment Variable let executablePath = null;
// PRIORITY 1: Environment Variable
if (process.env.YTDOWNLOADER_YTDLP_PATH) { if (process.env.YTDOWNLOADER_YTDLP_PATH) {
if (existsSync(process.env.YTDOWNLOADER_YTDLP_PATH)) { if (existsSync(process.env.YTDOWNLOADER_YTDLP_PATH)) {
localStorage.setItem( executablePath = process.env.YTDOWNLOADER_YTDLP_PATH;
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH, } else {
process.env.YTDOWNLOADER_YTDLP_PATH throw new Error(
"YTDOWNLOADER_YTDLP_PATH is set, but no file exists there."
); );
return process.env.YTDOWNLOADER_YTDLP_PATH;
} }
throw new Error(
"YTDOWNLOADER_YTDLP_PATH is set, but no file exists there."
);
} }
// Priority 2: System-installed versions (macOS, BSD) // PRIORITY 2: macOS homebrew
if (isMacOS) { else if (isMacOS) {
const possiblePaths = [ const possiblePaths = [
"/opt/homebrew/bin/yt-dlp", "/opt/homebrew/bin/yt-dlp", // Apple Silicon
"/usr/local/bin/yt-dlp", "/usr/local/bin/yt-dlp", // Intel
]; ];
const foundPath = possiblePaths.find((p) => existsSync(p));
if (foundPath) { executablePath = possiblePaths.find((p) => existsSync(p));
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
foundPath
);
return foundPath; // If Homebrew check fails, show popup and abort
} if (!executablePath) {
$(CONSTANTS.DOM_IDS.POPUP_BOX_MAC).style.display = "block";
console.warn("Homebrew yt-dlp not found. Prompting user.");
$(CONSTANTS.DOM_IDS.POPUP_BOX_MAC).style.display = "block"; return "";
}
}
return "" // PRIORITY 3: FreeBSD
} else if (platform() === "freebsd") { else if (isFreeBSD) {
try { try {
const foundPath = execSync("which yt-dlp").toString().trim(); executablePath = execSync("which yt-dlp").toString().trim();
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
foundPath
);
} catch { } catch {
throw new Error( throw new Error(
"No yt-dlp found in PATH on FreeBSD. Please install it." "No yt-dlp found in PATH on FreeBSD. Please install it."
@ -327,52 +321,54 @@ class YtDownloaderApp {
} }
} }
// Priority 3: localStorage // PRIORITY 4: LocalStorage or Download (Windows/Linux)
const storedPath = localStorage.getItem( else {
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH const storedPath = localStorage.getItem(
); CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH
);
if (isMacOS) {
const brewPaths = ["/opt/homebrew/bin/brew", "/usr/local/bin/brew"];
let brewFound = false; if (storedPath && existsSync(storedPath)) {
executablePath = storedPath;
}
// Download if missing
else {
executablePath = await this.ensureYtDlpBinary(defaultYtDlpPath);
}
}
for (const brewPath of brewPaths) { localStorage.setItem(
try { CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
await access(brewPath, constants.X_OK); executablePath
);
const brewUpdate = spawn(brewPath, ["upgrade", "yt-dlp"], { // Auto update
shell: true, this._runBackgroundUpdate(executablePath, isMacOS);
});
brewUpdate.on("error", (err) => return executablePath;
console.error( }
"Failed to run 'brew upgrade yt-dlp':",
err
)
);
brewUpdate.stdout.on("data", (data) => /**
console.log("yt-dlp brew update:", data.toString()) * yt-dlp background update
); */
_runBackgroundUpdate(executablePath, isMacOS) {
try {
if (isMacOS) {
const brewPaths = [
"/opt/homebrew/bin/brew",
"/usr/local/bin/brew",
];
const brewExec = brewPaths.find((p) => existsSync(p)) || "brew";
brewFound = true; const brewUpdate = spawn(brewExec, ["upgrade", "yt-dlp"]);
break;
} catch {}
}
if (!brewFound) { brewUpdate.on("error", (err) =>
console.error( console.error("Failed to run 'brew upgrade yt-dlp':", err)
"Homebrew not found in expected locations (/opt/homebrew/bin/brew or /usr/local/bin/brew)."
); );
} brewUpdate.stdout.on("data", (data) =>
} else if (storedPath) { console.log("yt-dlp brew update:", data.toString())
try { );
await access(storedPath, constants.F_OK); } else {
const updateProc = spawn(executablePath, ["-U"]);
const updateProc = spawn(`"${storedPath}"`, ["-U"], {
shell: true,
});
updateProc.on("error", (err) => updateProc.on("error", (err) =>
console.error( console.error(
@ -382,32 +378,21 @@ class YtDownloaderApp {
); );
updateProc.stdout.on("data", (data) => { updateProc.stdout.on("data", (data) => {
console.log("yt-dlp update check:", data.toString()); const output = data.toString();
console.log("yt-dlp update check:", output);
if (data.toString().toLowerCase().includes("updating to")) { if (output.toLowerCase().includes("updating to")) {
this._showPopup(i18n.__("updatingYtdlp")); this._showPopup(i18n.__("updatingYtdlp"));
} else if ( } else if (
data output.toLowerCase().includes("updated yt-dlp to")
.toString()
.toLowerCase()
.includes("updated yt-dlp to")
) { ) {
this._showPopup(i18n.__("updatedYtdlp")); this._showPopup(i18n.__("updatedYtdlp"));
} }
}); });
} catch {
console.warn("yt-dlp path not found, no update performed.");
} }
} catch (err) {
console.warn("Error initiating background update:", err);
} }
// Priority 4: Default location or download
const ytDlpPath = await this.ensureYtDlpBinary(defaultYtDlpPath);
localStorage.setItem(
CONSTANTS.LOCAL_STORAGE_KEYS.YT_DLP_PATH,
ytDlpPath
);
return ytDlpPath;
} }
/** /**
@ -535,7 +520,7 @@ class YtDownloaderApp {
const possiblePaths = [ const possiblePaths = [
"/opt/homebrew/bin/deno", "/opt/homebrew/bin/deno",
"/usr/local/bin/deno", "/usr/local/bin/deno",
] ];
for (const p of possiblePaths) { for (const p of possiblePaths) {
if (existsSync(p)) { if (existsSync(p)) {
@ -623,7 +608,9 @@ class YtDownloaderApp {
document.addEventListener("keydown", (event) => { document.addEventListener("keydown", (event) => {
if ( if (
((event.ctrlKey && event.key === "v") || ((event.ctrlKey && event.key === "v") ||
(event.metaKey && event.key === "v" && platform() === "darwin")) && (event.metaKey &&
event.key === "v" &&
platform() === "darwin")) &&
document.activeElement.tagName !== "INPUT" && document.activeElement.tagName !== "INPUT" &&
document.activeElement.tagName !== "TEXTAREA" document.activeElement.tagName !== "TEXTAREA"
) { ) {

Loading…
Cancel
Save