api: move error context to matcher

This commit is contained in:
wukko 2024-08-24 16:56:07 +06:00
parent 37efa035a2
commit 7ac0726f37
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
13 changed files with 33 additions and 85 deletions

View file

@ -246,9 +246,27 @@ export default async function(host, patternMatch, obj) {
} }
if (r.error) { if (r.error) {
let context;
switch(r.error) {
case "content.too_long":
context = {
limit: env.durationLimit / 60,
}
break;
case "fetch.fail":
case "fetch.rate":
case "content.video.unavailable":
case "link.unsupported":
context = {
service: host,
}
break;
}
return createResponse("error", { return createResponse("error", {
code: `error.api.${r.error}`, code: `error.api.${r.error}`,
context: r?.context context
}) })
} }

View file

@ -47,12 +47,7 @@ async function com_download(id) {
let streamData = JSON.parse(html.split('<script>window.__playinfo__=')[1].split('</script>')[0]); let streamData = JSON.parse(html.split('<script>window.__playinfo__=')[1].split('</script>')[0]);
if (streamData.data.timelength > env.durationLimit * 1000) { if (streamData.data.timelength > env.durationLimit * 1000) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
const [ video, audio ] = extractBestQuality(streamData.data.dash); const [ video, audio ] = extractBestQuality(streamData.data.dash);
@ -92,12 +87,7 @@ async function tv_download(id) {
} }
if (video.duration > env.durationLimit * 1000) { if (video.duration > env.durationLimit * 1000) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
return { return {

View file

@ -74,12 +74,7 @@ export default async function({ id }) {
} }
if (media.duration > env.durationLimit) { if (media.duration > env.durationLimit) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
const manifest = await fetch(media.hlsURL).then(r => r.text()).catch(() => {}); const manifest = await fetch(media.hlsURL).then(r => r.text()).catch(() => {});

View file

@ -295,12 +295,7 @@ export default function(obj) {
async function getStory(username, id) { async function getStory(username, id) {
const cookie = getCookie('instagram'); const cookie = getCookie('instagram');
if (!cookie) return { if (!cookie) return { error: "link.unsupported" };
error: "link.unsupported",
context: {
service: "instagram"
}
}
const userId = await usernameToId(username, cookie); const userId = await usernameToId(username, cookie);
if (!userId) return { error: "fetch.empty" }; if (!userId) return { error: "fetch.empty" };
@ -343,12 +338,7 @@ export default function(obj) {
} }
} }
return { return { error: "link.unsupported" };
error: "link.unsupported",
context: {
service: "instagram"
}
}
} }
const { postId, storyId, username } = obj; const { postId, storyId, username } = obj;

View file

@ -38,12 +38,7 @@ export default async function(o) {
return { error: "content.video.live" }; return { error: "content.video.live" };
if (videoData.movie.duration > env.durationLimit) if (videoData.movie.duration > env.durationLimit)
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
let videos = videoData.videos.filter(v => !v.disallowed); let videos = videoData.videos.filter(v => !v.disallowed);
let bestVideo = videos.find(v => resolutions[v.name] === quality) || videos[videos.length - 1]; let bestVideo = videos.find(v => resolutions[v.name] === quality) || videos[videos.length - 1];

View file

@ -82,12 +82,7 @@ export default async function(obj) {
return { error: "fetch.empty" }; return { error: "fetch.empty" };
if (data.secure_media?.reddit_video?.duration > env.durationLimit) if (data.secure_media?.reddit_video?.duration > env.durationLimit)
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
let audio = false, let audio = false,
video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0], video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0],

View file

@ -39,12 +39,7 @@ export default async function(obj) {
if (play.live_streams?.hls) return { error: "content.video.live" }; if (play.live_streams?.hls) return { error: "content.video.live" };
if (play.duration > env.durationLimit * 1000) if (play.duration > env.durationLimit * 1000)
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
let m3u8 = await fetch(play.video_balancer.m3u8) let m3u8 = await fetch(play.video_balancer.m3u8)
.then(r => r.text()) .then(r => r.text())

View file

@ -82,12 +82,7 @@ export default async function(obj) {
return { error: "fetch.empty" }; return { error: "fetch.empty" };
if (json.duration > env.durationLimit * 1000) { if (json.duration > env.durationLimit * 1000) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
let file = await fetch(fileUrl) let file = await fetch(fileUrl)

View file

@ -22,12 +22,7 @@ export default async function(input) {
let { subdomain } = psl.parse(input.url.hostname); let { subdomain } = psl.parse(input.url.hostname);
if (subdomain?.includes('.')) { if (subdomain?.includes('.')) {
return { return { error: "link.unsupported" };
error: "link.unsupported",
context: {
service: "tumblr"
}
}
} else if (subdomain === 'www' || subdomain === 'at') { } else if (subdomain === 'www' || subdomain === 'at') {
subdomain = undefined subdomain = undefined
} }

View file

@ -36,12 +36,7 @@ export default async function (obj) {
const clipMetadata = req_metadata.data.clip; const clipMetadata = req_metadata.data.clip;
if (clipMetadata.durationSeconds > env.durationLimit) { if (clipMetadata.durationSeconds > env.durationLimit) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
if (!clipMetadata.videoQualities || !clipMetadata.broadcaster) { if (!clipMetadata.videoQualities || !clipMetadata.broadcaster) {
return { error: "fetch.empty" }; return { error: "fetch.empty" };

View file

@ -77,12 +77,7 @@ const getHLS = async (configURL, obj) => {
if (!api) return { error: "fetch.fail" }; if (!api) return { error: "fetch.fail" };
if (api.video?.duration > env.durationLimit) { if (api.video?.duration > env.durationLimit) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
const urlMasterHLS = api.request?.files?.hls?.cdns?.akfire_interconnect_quic?.url; const urlMasterHLS = api.request?.files?.hls?.cdns?.akfire_interconnect_quic?.url;

View file

@ -29,12 +29,7 @@ export default async function(o) {
} }
if (js.mvData.duration > env.durationLimit) { if (js.mvData.duration > env.durationLimit) {
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
} }
for (let i in resolutions) { for (let i in resolutions) {

View file

@ -210,12 +210,7 @@ export default async function(o) {
return { error: "youtube.codec" }; return { error: "youtube.codec" };
if (basicInfo.duration > env.durationLimit) if (basicInfo.duration > env.durationLimit)
return { return { error: "content.too_long" };
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
const checkBestAudio = (i) => (i.has_audio && !i.has_video); const checkBestAudio = (i) => (i.has_audio && !i.has_video);