feat: add yt-dlp auto-updater #151 #225

This commit is contained in:
Jelle Glebbeek
2021-10-17 23:53:09 +02:00
parent d866ae2201
commit b28ead4ad3
2 changed files with 52 additions and 37 deletions

View File

@@ -13,36 +13,67 @@ class BinaryUpdater {
async checkUpdate() {
const transaction = Sentry.startTransaction({ name: "checkUpdate" });
const span = transaction.startChild({ op: "task" });
console.log("Checking for a new version of ytdl.");
console.log("Checking for a new version of yt-dlp.");
const localVersion = await this.getLocalVersion();
const [remoteUrl, remoteVersion] = await this.getRemoteVersion();
const { remoteUrl, remoteVersion } = await this.getRemoteVersion();
if(remoteVersion === localVersion) {
transaction.setTag("download", "up-to-data");
console.log(`Binaries were already up-to-date! Version: ${localVersion}`);
} else if(localVersion == null) {
transaction.setTag("download", "corrupted");
console.log("Binaries may have been corrupted, downloading binaries to be on the safe side.");
console.log("Downloading missing yt-dlp binary.");
this.win.webContents.send("binaryLock", {lock: true, placeholder: `Re-installing ytdl version ${remoteVersion}...`})
await this.downloadUpdate(remoteUrl, remoteVersion);
} else if(remoteVersion == null) {
transaction.setTag("download", "down");
console.log("Unable to check for new updates, yt-dl.org may be down.");
console.log("Unable to check for new updates, GitHub may be down.");
} else {
console.log(`New version ${remoteVersion} found. Updating...`);
transaction.setTag("download", "update");
this.win.webContents.send("binaryLock", {lock: true, placeholder: `Updating ytdl to version ${remoteVersion}...`})
this.win.webContents.send("binaryLock", {lock: true, placeholder: `Updating yt-dlp to version ${remoteVersion}...`})
await this.downloadUpdate(remoteUrl, remoteVersion);
}
span.finish();
transaction.finish();
}
//Returns the currently downloaded version of youtube-dl
async getLocalVersion() {
async getRemoteVersion() {
try {
const url = process.platform === "win32" ? "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe" : "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp"
await axios.get(url, {
responseType: 'document',
maxRedirects: 0,
})
} catch (err) {
const res = err.response;
if (err.response == null) {
console.error('An error occurred while retrieving the latest yt-dlp version data.')
return null;
}
if (res.status === 302) {
const versionRegex = res.data.match(/[0-9]+\.[0-9]+\.[0-9]+/);
const urlRegex = res.data.match(/(?<=").+?(?=")/);
return {
remoteVersion: versionRegex ? versionRegex[0] : null,
remoteUrl: urlRegex ? urlRegex[0] : null,
};
} else {
console.error('Did not get redirect for the latest version link. Status: ' + err.response.status);
return null;
}
}
return null;
}
//Returns the currently downloaded version of yt-dlp
async getLocalVersion() {
let data;
try {
const result = await fs.promises.readFile(this.paths.ytdlVersion);
data = JSON.parse(result);
if (!data.ytdlp) {
data = null;
}
} catch (err) {
console.error(err);
data = null;
@@ -55,30 +86,11 @@ class BinaryUpdater {
if(data == null) {
return null;
} else {
console.log("Current version: " + data.version);
console.log("Current yt-dlp version: " + data.version);
return data.version;
}
}
//Returns an array containing the latest available remote version and the download link to it.
async getRemoteVersion() {
const url = (process.platform === "win32") ? "https://yt-dl.org/downloads/latest/youtube-dl.exe" : "https://yt-dl.org/downloads/latest/youtube-dl";
try {
await axios.get(url, {maxRedirects: 0})
} catch (err) {
if(err == null || err.response == null) {
console.error(err);
return [null, null];
} else if (err.response.status !== 302) {
console.error('Did not get redirect for the latest version link. Status: ' + err.response.status);
return [null, null];
} else {
return [err.response.headers.location, /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(err.response.headers.location)[1]];
}
}
return [null, null];
}
//Downloads the file at the given url and saves it to the ytdl path.
async downloadUpdate(remoteUrl, remoteVersion) {
const writer = fs.createWriteStream(this.paths.ytdl);
@@ -102,7 +114,10 @@ class BinaryUpdater {
//Writes the new version number to the ytdlVersion file
async writeVersionInfo(version) {
const data = {version: version};
const data = {
version: version,
ytdlp: true
};
await fs.promises.writeFile(this.paths.ytdlVersion, JSON.stringify(data));
console.log("New version data written to ytdlVersion.");
}

View File

@@ -16,7 +16,7 @@ class Filepaths {
this.unpackedPrefix = path.join(path.dirname(this.appPath), "app.asar.unpacked");
this.packedPrefix = this.appPath;
this.ffmpeg = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ffmpeg.exe") : "binaries/ffmpeg.exe";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/youtube-dl.exe") : "binaries/youtube-dl.exe";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/yt-dlp.exe") : "binaries/yt-dlp.exe";
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.unpackedPrefix, "userSettings") : "userSettings";
this.taskList = this.app.isPackaged ? path.join(this.unpackedPrefix, "taskList") : "taskList";
@@ -30,7 +30,7 @@ class Filepaths {
this.packedPrefix = this.appPath;
await this.createAppDataFolder();
this.ffmpeg = path.join(this.binaryPath, "ffmpeg.exe");
this.ytdl = path.join(this.binaryPath, "youtube-dl.exe");
this.ytdl = path.join(this.binaryPath, "yt-dlp.exe");
this.icon = path.join(this.packedPrefix, "renderer/img/icon.png");
this.settings = path.join(this.binaryPath, "userSettings");
this.taskList = path.join(this.binaryPath, "taskList");
@@ -43,7 +43,7 @@ class Filepaths {
this.packedPrefix = this.appPath;
await this.createPortableFolder();
this.ffmpeg = path.join(this.persistentPath, "ffmpeg.exe");
this.ytdl = path.join(this.persistentPath, "youtube-dl.exe");
this.ytdl = path.join(this.persistentPath, "yt-dlp.exe");
this.icon = path.join(this.packedPrefix, "renderer/img/icon.png");
this.settings = path.join(this.persistentPath, "userSettings");
this.taskList = path.join(this.persistentPath, "taskList");
@@ -54,7 +54,7 @@ class Filepaths {
this.packedPrefix = this.appPath;
this.unpackedPrefix = this.appPath + ".unpacked";
this.ffmpeg = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ffmpeg") : "binaries/ffmpeg";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/youtube-dl-unix") : "binaries/youtube-dl-unix";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/yt-dlp-unix") : "binaries/yt-dlp-unix";
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.unpackedPrefix, "userSettings") : "userSettings";
this.taskList = this.app.isPackaged ? path.join(this.unpackedPrefix, "taskList") : "taskList";
@@ -66,7 +66,7 @@ class Filepaths {
this.packedPrefix = this.appPath;
this.unpackedPrefix = this.appPath + ".unpacked";
if(this.app.isPackaged) await this.createHomeFolder()
this.ytdl = this.app.isPackaged ? path.join(this.persistentPath, "youtube-dl-unix") : "binaries/youtube-dl-unix";
this.ytdl = this.app.isPackaged ? path.join(this.persistentPath, "yt-dlp-unix") : "binaries/yt-dlp-unix";
this.ffmpeg = this.app.isPackaged ? path.join(this.persistentPath, "ffmpeg") : "binaries/ffmpeg";
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.persistentPath, "userSettings") : "userSettings";
@@ -126,7 +126,7 @@ class Filepaths {
async createAppDataFolder() {
const from = path.join(this.unpackedPrefix, "binaries");
const toCopy = ["youtube-dl.exe", "ffmpeg.exe", "ytdlVersion", "AtomicParsley.exe"];
const toCopy = ["yt-dlp.exe", "ffmpeg.exe", "ytdlVersion", "AtomicParsley.exe"];
await this.copyFiles(from, this.persistentPath, toCopy);
}
@@ -135,7 +135,7 @@ class Filepaths {
await fs.promises.access(process.env.PORTABLE_EXECUTABLE_DIR, fs.constants.W_OK);
if(await this.migrateExistingAppDataFolder()) return;
const from = path.join(this.unpackedPrefix, "binaries");
const toCopy = ["youtube-dl.exe", "ffmpeg.exe", "ytdlVersion", "AtomicParsley.exe"];
const toCopy = ["yt-dlp.exe", "ffmpeg.exe", "ytdlVersion", "AtomicParsley.exe"];
await this.copyFiles(from, this.persistentPath, toCopy);
} catch (e) {
setTimeout(() => console.error(e), 5000);
@@ -148,7 +148,7 @@ class Filepaths {
const from = path.join(this.app.getPath("appData"), "youtube-dl-gui-portable");
try {
await fs.promises.access(from, fs.constants.W_OK);
const toCopy = ["youtube-dl.exe", "ffmpeg.exe", "ytdlVersion", "userSettings", "taskList"];
const toCopy = ["yt-dlp.exe", "ffmpeg.exe", "ytdlVersion", "userSettings", "taskList"];
await this.copyFiles(from, this.persistentPath, toCopy);
try {
await fs.promises.rmdir(from, {recursive: true});
@@ -163,7 +163,7 @@ class Filepaths {
async createHomeFolder() {
const from = path.join(this.unpackedPrefix, "binaries");
const toCopy = ["youtube-dl-unix", "ffmpeg-linux", "ytdlVersion"];
const toCopy = ["yt-dlp-unix", "ffmpeg-linux", "ytdlVersion"];
await this.copyFiles(from, this.persistentPath, toCopy);
}