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) {
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", {
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]);
if (streamData.data.timelength > env.durationLimit * 1000) {
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
}
const [ video, audio ] = extractBestQuality(streamData.data.dash);
@ -92,12 +87,7 @@ async function tv_download(id) {
}
if (video.duration > env.durationLimit * 1000) {
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
}
return {

View file

@ -74,12 +74,7 @@ export default async function({ id }) {
}
if (media.duration > env.durationLimit) {
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
}
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) {
const cookie = getCookie('instagram');
if (!cookie) return {
error: "link.unsupported",
context: {
service: "instagram"
}
}
if (!cookie) return { error: "link.unsupported" };
const userId = await usernameToId(username, cookie);
if (!userId) return { error: "fetch.empty" };
@ -343,12 +338,7 @@ export default function(obj) {
}
}
return {
error: "link.unsupported",
context: {
service: "instagram"
}
}
return { error: "link.unsupported" };
}
const { postId, storyId, username } = obj;

View file

@ -38,12 +38,7 @@ export default async function(o) {
return { error: "content.video.live" };
if (videoData.movie.duration > env.durationLimit)
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
let videos = videoData.videos.filter(v => !v.disallowed);
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" };
if (data.secure_media?.reddit_video?.duration > env.durationLimit)
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
let audio = false,
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.duration > env.durationLimit * 1000)
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
let m3u8 = await fetch(play.video_balancer.m3u8)
.then(r => r.text())

View file

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

View file

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

View file

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

View file

@ -77,12 +77,7 @@ const getHLS = async (configURL, obj) => {
if (!api) return { error: "fetch.fail" };
if (api.video?.duration > env.durationLimit) {
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
}
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) {
return {
error: "content.too_long",
context: {
limit: env.durationLimit / 60
}
}
return { error: "content.too_long" };
}
for (let i in resolutions) {

View file

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