From ae271fd3c631c1b0a90cd0bb7dca5c8660802e83 Mon Sep 17 00:00:00 2001 From: wukko Date: Wed, 23 Oct 2024 18:08:50 +0600 Subject: [PATCH] api/youtube: refactor playability status handling --- api/src/processing/services/youtube.js | 79 +++++++++++++------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/api/src/processing/services/youtube.js b/api/src/processing/services/youtube.js index a6cdc9a2..95581863 100644 --- a/api/src/processing/services/youtube.js +++ b/api/src/processing/services/youtube.js @@ -1,5 +1,4 @@ import { fetch } from "undici"; - import { Innertube, Session } from "youtubei.js"; import { env } from "../../config.js"; @@ -146,41 +145,49 @@ export default async function(o) { const playability = info.playability_status; const basicInfo = info.basic_info; - if (playability.status === "LOGIN_REQUIRED") { - if (playability.reason.endsWith("bot")) { - return { error: "youtube.login" } - } - if (playability.reason.endsWith("age")) { - return { error: "content.video.age" } - } - if (playability?.error_screen?.reason?.text === "Private video") { - return { error: "content.video.private" } - } + switch(playability.status) { + case "OK": + break; + + case "LOGIN_REQUIRED": + if (playability.reason.endsWith("bot")) { + return { error: "youtube.login" } + } + if (playability.reason.endsWith("age")) { + return { error: "content.video.age" } + } + if (playability?.error_screen?.reason?.text === "Private video") { + return { error: "content.video.private" } + } + break; + + case "UNPLAYABLE": + if (playability?.reason?.endsWith("request limit.")) { + return { error: "fetch.rate" } + } + if (playability?.error_screen?.subreason?.text?.endsWith("in your country")) { + return { error: "content.video.region" } + } + if (playability?.error_screen?.reason?.text === "Private video") { + return { error: "content.video.private" } + } + break; + + case "AGE_VERIFICATION_REQUIRED": + return { error: "content.video.age" }; + + default: + return { error: "content.video.unavailable" }; } - if (playability.status === "UNPLAYABLE") { - if (playability?.reason?.endsWith("request limit.")) { - return { error: "fetch.rate" } - } - if (playability?.error_screen?.subreason?.text?.endsWith("in your country")) { - return { error: "content.video.region" } - } - if (playability?.error_screen?.reason?.text === "Private video") { - return { error: "content.video.private" } - } - } - - if (playability.status === "AGE_VERIFICATION_REQUIRED") { - return { error: "content.video.age" } - } - - if (playability.status !== "OK") { - return { error: "content.video.unavailable" }; - } if (basicInfo.is_live) { return { error: "content.video.live" }; } + if (basicInfo.duration > env.durationLimit) { + return { error: "content.too_long" }; + } + // return a critical error if returned video is "Video Not Available" // or a similar stub by youtube if (basicInfo.id !== o.id) { @@ -212,11 +219,9 @@ export default async function(o) { if (bestVideo) bestQuality = qual(bestVideo); - if ((!bestQuality && !o.isAudioOnly) || !hasAudio) + if ((!bestQuality && !o.isAudioOnly) || !hasAudio) { return { error: "youtube.codec" }; - - if (basicInfo.duration > env.durationLimit) - return { error: "content.too_long" }; + } const checkBestAudio = (i) => (i.has_audio && !i.has_video); @@ -226,9 +231,7 @@ export default async function(o) { if (o.dubLang) { let dubbedAudio = adaptive_formats.find(i => - checkBestAudio(i) - && i.language === o.dubLang - && i.audio_track + checkBestAudio(i) && i.language === o.dubLang && i.audio_track ) if (dubbedAudio && !dubbedAudio?.audio_track?.audio_is_default) { @@ -313,5 +316,5 @@ export default async function(o) { } } - return { error: "fetch.fail" } + return { error: "fetch.fail" }; }