diff --git a/utils/metadata.js b/utils/metadata.js index 17bb8db..9d6ab97 100644 --- a/utils/metadata.js +++ b/utils/metadata.js @@ -4,6 +4,7 @@ const https = require('https') const maxRetries = 5 const platforms = ['WEB', 'ANDROID', 'iOS'] +const cobalt = ['http://cobalt-api:9000', 'https://co.wuk.sh', 'http://cobalt-api:9000'] const ignoreSsl = new https.Agent({ rejectUnauthorized: false, @@ -125,17 +126,29 @@ async function getPlaylistVideos(id) { } async function getVideoDownload(url, quality) { - const json = await (await fetch('http://cobalt-api:9000/api/json', { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - 'url': url, - 'vQuality': quality - }) - })).json() + let json + + for (let retries = 0; retries < maxRetries; retries++) { + try { + const instance = cobalt[retries % cobalt.length]; + json = await (await fetch(`${instance}/api/json`, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + 'url': url, + 'vQuality': quality + }) + })).json() + + if (json.error) continue + return json + } catch (error) { + continue + } + } return json } diff --git a/utils/ytdlp.js b/utils/ytdlp.js index 7a69d77..1b060d4 100644 --- a/utils/ytdlp.js +++ b/utils/ytdlp.js @@ -8,17 +8,16 @@ async function downloadVideo(url, ws, id) { let quality = '720p' const video = await metadata.getVideoMetadata(id) if (video.error) { - resolve({ + return resolve({ message: `Failed to request Youtube with error ${video.error}. Please retry...`, fail: true }) } + if (video.basic_info.duration >= 900) quality = '360p' // 15 minutes - if (video.basic_info.duration > 1200) quality = '480p' // 20 minutes - if (video.basic_info.duration > 2100) quality = '360p' // 35 minutes const downloadJson = await metadata.getVideoDownload(url, quality) if (downloadJson.status == 'error') { - resolve({ + return resolve({ message: 'Failed to request Youtube. Please retry...', fail: true }) @@ -61,11 +60,11 @@ async function downloadVideo(url, ws, id) { download.on('end', output => { if (output == 'Finished writing to disk') { ws.send(`DATA - Download has finished`) - resolve({ + return resolve({ fail: false }) } else { - resolve({ + return resolve({ fail: true }) }