improvement: better error handling in nicovideo

This commit is contained in:
mikhail 2024-05-22 22:54:01 +05:00
parent f956af06ed
commit 2d8bb1d103

View File

@ -17,10 +17,20 @@ const NICOVIDEO_GUEST_API_URL =
const NICOVIDEO_HLS_API_URL = const NICOVIDEO_HLS_API_URL =
"https://nvapi.nicovideo.jp/v1/watch/%s/access-rights/hls?actionTrackId=%s"; "https://nvapi.nicovideo.jp/v1/watch/%s/access-rights/hls?actionTrackId=%s";
class CobaltError extends Error {
constructor(localizatedMessage) {
this.localizatedMessage = localizatedMessage;
}
}
async function getBasicVideoInformation(id) { async function getBasicVideoInformation(id) {
const page = await fetch(util.format(NICOVIDEO_EMBED_URL, id), { const page = await fetch(util.format(NICOVIDEO_EMBED_URL, id), {
headers: { "user-agent": genericUserAgent }, headers: { "user-agent": genericUserAgent },
}).then((response) => response.text()); })
.then((response) => response.text())
.catch(() => {
throw new CobaltError("ErrorCouldntFetch");
});
const data = JSON.parse( const data = JSON.parse(
page page
@ -36,7 +46,11 @@ async function getBasicVideoInformation(id) {
{ {
headers: { "user-agent": genericUserAgent }, headers: { "user-agent": genericUserAgent },
} }
).then((response) => response.json()); )
.then((response) => response.json())
.catch(() => {
throw new CobaltError("ErrorCouldntFetch");
});
return { ...data, author }; return { ...data, author };
} }
@ -47,10 +61,14 @@ async function fetchGuestData(id, actionTrackId) {
{ {
headers: { "user-agent": genericUserAgent }, headers: { "user-agent": genericUserAgent },
} }
).then((response) => response.json()); )
.then((response) => response.json())
.catch(() => {
throw new CobaltError("ErrorCouldntFetch");
});
if (data?.meta?.status !== 200) { if (data?.meta?.status !== 200) {
throw new Error(); throw new CobaltError("ErrorBadFetch");
} }
const { videos, audios, accessRightKey } = data.data.media.domand; const { videos, audios, accessRightKey } = data.data.media.domand;
@ -82,10 +100,14 @@ async function fetchContentURL(id, actionTrackId, accessRightKey, outputs) {
}, },
body: JSON.stringify({ outputs }), body: JSON.stringify({ outputs }),
} }
).then((response) => response.json()); )
.then((response) => response.json())
.catch(() => {
throw new CobaltError("ErrorCouldntFetch");
});
if (data?.meta?.status !== 201) { if (data?.meta?.status !== 201) {
throw new Error(); throw new CobaltError("ErrorBadFetch");
} }
return data.data.contentUrl; return data.data.contentUrl;
@ -120,7 +142,6 @@ async function getHLSContent(contentURL, quality, isAudioOnly) {
}; };
} }
// TODO @synzr better error handling
export default async function nicovideo({ id, quality, isAudioOnly }) { export default async function nicovideo({ id, quality, isAudioOnly }) {
try { try {
const { actionTrackId, title, author } = await getBasicVideoInformation(id); const { actionTrackId, title, author } = await getBasicVideoInformation(id);
@ -154,6 +175,6 @@ export default async function nicovideo({ id, quality, isAudioOnly }) {
...(type === "audio" ? { isM3U8: true, bestAudio: "mp3" } : {}), ...(type === "audio" ? { isM3U8: true, bestAudio: "mp3" } : {}),
}; };
} catch (error) { } catch (error) {
return { error: "ErrorEmptyDownload" }; return { error: error.localizatedMessage ?? "ErrorSomethingWentWrong" };
} }
} }