From d1e8929ee245c7393f0425a78a499b35932522da Mon Sep 17 00:00:00 2001 From: wukko Date: Thu, 16 May 2024 20:57:48 +0600 Subject: [PATCH] api: add DURATION_LIMIT env variable duration limit is now in seconds and customizable across instances --- src/config.json | 1 - src/modules/config.js | 3 ++- src/modules/processing/services/bilibili.js | 10 +++++----- src/modules/processing/services/dailymotion.js | 6 +++--- src/modules/processing/services/ok.js | 4 ++-- src/modules/processing/services/reddit.js | 6 +++--- src/modules/processing/services/rutube.js | 6 +++--- src/modules/processing/services/soundcloud.js | 6 +++--- src/modules/processing/services/twitch.js | 4 ++-- src/modules/processing/services/vimeo.js | 4 ++-- src/modules/processing/services/vk.js | 4 ++-- src/modules/processing/services/youtube.js | 4 ++-- 12 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/config.json b/src/config.json index 0a32d220..640b2433 100644 --- a/src/config.json +++ b/src/config.json @@ -1,6 +1,5 @@ { "streamLifespan": 90000, - "maxVideoDuration": 10800000, "genericUserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36", "authorInfo": { "support": { diff --git a/src/modules/config.js b/src/modules/config.js index a8500747..737d9716 100644 --- a/src/modules/config.js +++ b/src/modules/config.js @@ -44,6 +44,8 @@ const rateLimitWindow: (process.env.RATELIMIT_WINDOW && parseInt(process.env.RATELIMIT_WINDOW)) || 60, rateLimitMax: (process.env.RATELIMIT_MAX && parseInt(process.env.RATELIMIT_MAX)) || 20, + durationLimit: (process.env.DURATION_LIMIT && parseInt(process.env.DURATION_LIMIT)) || 10800, + processingPriority: process.platform !== 'win32' && process.env.PROCESSING_PRIORITY && parseInt(process.env.PROCESSING_PRIORITY) @@ -54,7 +56,6 @@ export const audioIgnore = servicesConfigJson.audioIgnore, version = packageJson.version, streamLifespan = config.streamLifespan, - maxVideoDuration = config.maxVideoDuration, genericUserAgent = config.genericUserAgent, repo = packageJson.bugs.url.replace('/issues', ''), authorInfo = config.authorInfo, diff --git a/src/modules/processing/services/bilibili.js b/src/modules/processing/services/bilibili.js index 6da110bf..0475a44c 100644 --- a/src/modules/processing/services/bilibili.js +++ b/src/modules/processing/services/bilibili.js @@ -1,4 +1,4 @@ -import { genericUserAgent, maxVideoDuration } from "../../config.js"; +import { genericUserAgent, env } from "../../config.js"; // TO-DO: higher quality downloads (currently requires an account) @@ -39,8 +39,8 @@ async function com_download(id) { } let streamData = JSON.parse(html.split('')[0]); - if (streamData.data.timelength > maxVideoDuration) { - return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (streamData.data.timelength > env.durationLimit * 1000) { + return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; } const [ video, audio ] = extractBestQuality(streamData.data.dash); @@ -79,8 +79,8 @@ async function tv_download(id) { return { error: 'ErrorEmptyDownload' }; } - if (video.duration > maxVideoDuration) { - return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (video.duration > env.durationLimit * 1000) { + return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; } return { diff --git a/src/modules/processing/services/dailymotion.js b/src/modules/processing/services/dailymotion.js index 1993ecad..c1fca95d 100644 --- a/src/modules/processing/services/dailymotion.js +++ b/src/modules/processing/services/dailymotion.js @@ -1,5 +1,5 @@ import HLSParser from 'hls-parser'; -import { maxVideoDuration } from '../../config.js'; +import { env } from '../../config.js'; let _token; @@ -73,8 +73,8 @@ export default async function({ id }) { return { error: 'ErrorEmptyDownload' } } - if (media.duration * 1000 > maxVideoDuration) { - return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (media.duration > env.durationLimit) { + return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; } const manifest = await fetch(media.hlsURL).then(r => r.text()).catch(() => {}); diff --git a/src/modules/processing/services/ok.js b/src/modules/processing/services/ok.js index bde163f6..52e0903f 100644 --- a/src/modules/processing/services/ok.js +++ b/src/modules/processing/services/ok.js @@ -1,4 +1,4 @@ -import { genericUserAgent, maxVideoDuration } from "../../config.js"; +import { genericUserAgent, env } from "../../config.js"; import { cleanString } from "../../sub/utils.js"; const resolutions = { @@ -29,7 +29,7 @@ export default async function(o) { if (videoData.provider !== "UPLOADED_ODKL") return { error: 'ErrorUnsupported' }; if (videoData.movie.is_live) return { error: 'ErrorLiveVideo' }; - if (videoData.movie.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (videoData.movie.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; let videos = videoData.videos.filter(v => !v.disallowed); let bestVideo = videos.find(v => resolutions[v.name] === quality) || videos[videos.length - 1]; diff --git a/src/modules/processing/services/reddit.js b/src/modules/processing/services/reddit.js index 00b35526..d64f013b 100644 --- a/src/modules/processing/services/reddit.js +++ b/src/modules/processing/services/reddit.js @@ -1,4 +1,4 @@ -import { genericUserAgent, maxVideoDuration } from "../../config.js"; +import { genericUserAgent, env } from "../../config.js"; import { getCookie, updateCookieValues } from "../cookie/manager.js"; async function getAccessToken() { @@ -79,8 +79,8 @@ export default async function(obj) { if (!data.secure_media?.reddit_video) return { error: 'ErrorEmptyDownload' }; - if (data.secure_media?.reddit_video?.duration * 1000 > maxVideoDuration) - return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (data.secure_media?.reddit_video?.duration > env.durationLimit) + return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; let audio = false, video = data.secure_media?.reddit_video?.fallback_url?.split('?')[0], diff --git a/src/modules/processing/services/rutube.js b/src/modules/processing/services/rutube.js index b26bfb19..77f13512 100644 --- a/src/modules/processing/services/rutube.js +++ b/src/modules/processing/services/rutube.js @@ -1,6 +1,6 @@ import HLS from 'hls-parser'; -import { maxVideoDuration } from "../../config.js"; +import { env } from "../../config.js"; import { cleanString } from '../../sub/utils.js'; async function requestJSON(url) { @@ -35,8 +35,8 @@ export default async function(obj) { if (play.detail || !play.video_balancer) return { error: 'ErrorEmptyDownload' }; if (play.live_streams?.hls) return { error: 'ErrorLiveVideo' }; - if (play.duration > maxVideoDuration) - return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (play.duration > env.durationLimit * 1000) + return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; let m3u8 = await fetch(play.video_balancer.m3u8) .then(r => r.text()) diff --git a/src/modules/processing/services/soundcloud.js b/src/modules/processing/services/soundcloud.js index 2288b5f3..a305d35c 100644 --- a/src/modules/processing/services/soundcloud.js +++ b/src/modules/processing/services/soundcloud.js @@ -1,4 +1,4 @@ -import { maxVideoDuration } from "../../config.js"; +import { env } from "../../config.js"; import { cleanString } from "../../sub/utils.js"; const cachedID = { @@ -77,8 +77,8 @@ export default async function(obj) { if (fileUrl.substring(0, 54) !== "https://api-v2.soundcloud.com/media/soundcloud:tracks:") return { error: 'ErrorEmptyDownload' }; - if (json.duration > maxVideoDuration) - return { error: ['ErrorLengthAudioConvert', maxVideoDuration / 60000] }; + if (json.duration > env.durationLimit * 1000) + return { error: ['ErrorLengthAudioConvert', env.durationLimit / 60] }; let file = await fetch(fileUrl).then(async (r) => { return (await r.json()).url }).catch(() => {}); if (!file) return { error: 'ErrorCouldntFetch' }; diff --git a/src/modules/processing/services/twitch.js b/src/modules/processing/services/twitch.js index a23f0139..0a26e1d3 100644 --- a/src/modules/processing/services/twitch.js +++ b/src/modules/processing/services/twitch.js @@ -1,4 +1,4 @@ -import { maxVideoDuration } from "../../config.js"; +import { env } from "../../config.js"; import { cleanString } from '../../sub/utils.js'; const gqlURL = "https://gql.twitch.tv/gql"; @@ -34,7 +34,7 @@ export default async function (obj) { let clipMetadata = req_metadata.data.clip; - if (clipMetadata.durationSeconds > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (clipMetadata.durationSeconds > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; if (!clipMetadata.videoQualities || !clipMetadata.broadcaster) return { error: 'ErrorEmptyDownload' }; let req_token = await fetch(gqlURL, { diff --git a/src/modules/processing/services/vimeo.js b/src/modules/processing/services/vimeo.js index 64c4f9c0..b59781ef 100644 --- a/src/modules/processing/services/vimeo.js +++ b/src/modules/processing/services/vimeo.js @@ -1,4 +1,4 @@ -import { maxVideoDuration } from "../../config.js"; +import { env } from "../../config.js"; import { cleanString } from '../../sub/utils.js'; const resolutionMatch = { @@ -63,7 +63,7 @@ export default async function(obj) { } } - if (api.video.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (api.video.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; let masterJSONURL = api["request"]["files"]["dash"]["cdns"]["akfire_interconnect_quic"]["url"]; let masterJSON = await fetch(masterJSONURL).then((r) => { return r.json() }).catch(() => { return false }); diff --git a/src/modules/processing/services/vk.js b/src/modules/processing/services/vk.js index d956c3b9..ac3d35e8 100644 --- a/src/modules/processing/services/vk.js +++ b/src/modules/processing/services/vk.js @@ -1,4 +1,4 @@ -import { genericUserAgent, maxVideoDuration } from "../../config.js"; +import { genericUserAgent, env } from "../../config.js"; import { cleanString } from "../../sub/utils.js"; const resolutions = ["2160", "1440", "1080", "720", "480", "360", "240"]; @@ -21,7 +21,7 @@ export default async function(o) { let js = JSON.parse('{"lang":' + html.split(`{"lang":`)[1].split(']);')[0]); if (Number(js.mvData.is_active_live) !== 0) return { error: 'ErrorLiveVideo' }; - if (js.mvData.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (js.mvData.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; for (let i in resolutions) { if (js.player.params[0][`url${resolutions[i]}`]) { diff --git a/src/modules/processing/services/youtube.js b/src/modules/processing/services/youtube.js index d11b2739..898f0884 100644 --- a/src/modules/processing/services/youtube.js +++ b/src/modules/processing/services/youtube.js @@ -1,5 +1,5 @@ import { Innertube, Session } from 'youtubei.js'; -import { maxVideoDuration } from '../../config.js'; +import { env } from '../../config.js'; import { cleanString } from '../../sub/utils.js'; import { fetch } from 'undici' @@ -92,7 +92,7 @@ export default async function(o) { if (bestQuality) bestQuality = qual(bestQuality); if (!bestQuality && !o.isAudioOnly || !hasAudio) return { error: 'ErrorYTTryOtherCodec' }; - if (info.basic_info.duration > maxVideoDuration / 1000) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + if (info.basic_info.duration > env.durationLimit) return { error: ['ErrorLengthLimit', env.durationLimit / 60] }; let checkBestAudio = (i) => (i.has_audio && !i.has_video), audio = adaptive_formats.find(i => checkBestAudio(i) && !i.is_dubbed);