cobalt/src/modules/sub/utils.js

121 lines
4.8 KiB
JavaScript
Raw Normal View History

import { normalizeURL } from "../processing/url.js";
2022-07-08 20:17:56 +02:00
import { createStream } from "../stream/manage.js";
const apiVar = {
2022-12-06 20:21:07 +01:00
allowed: {
2023-02-26 17:49:25 +01:00
vCodec: ["h264", "av1", "vp9"],
vQuality: ["max", "4320", "2160", "1440", "1080", "720", "480", "360", "240", "144"],
aFormat: ["best", "mp3", "ogg", "wav", "opus"],
filenamePattern: ["classic", "pretty", "basic", "nerdy"]
2022-12-06 20:21:07 +01:00
},
booleanOnly: ["isAudioOnly", "isNoTTWatermark", "isTTFullAudio", "isAudioMuted", "dubLang", "vimeoDash", "disableMetadata", "twitterGif"]
2022-12-06 20:21:07 +01:00
}
const forbiddenChars = ['}', '{', '(', ')', '\\', '>', '<', '^', '*', '!', '~', ';', ':', ',', '`', '[', ']', '#', '$', '"', "'", "@", '=='];
const forbiddenCharsString = ['}', '{', '%', '>', '<', '^', ';', '`', '$', '"', "@", '='];
2022-12-06 20:21:07 +01:00
2022-07-08 20:17:56 +02:00
export function apiJSON(type, obj) {
try {
switch (type) {
case 0:
return { status: 400, body: { status: "error", text: obj.t } };
case 1:
return { status: 200, body: { status: "redirect", url: obj.u } };
2022-08-16 09:14:19 +02:00
case 2:
2022-07-08 20:17:56 +02:00
return { status: 200, body: { status: "stream", url: createStream(obj) } };
case 3:
return { status: 200, body: { status: "success", text: obj.t } };
case 4:
return { status: 429, body: { status: "rate-limit", text: obj.t } };
2022-09-03 17:32:39 +02:00
case 5:
2022-10-09 19:44:00 +02:00
let pickerType = "various", audio = false
switch (obj.service) {
case "douyin":
case "tiktok":
2023-08-13 22:07:05 +02:00
audio = obj.u
2022-10-09 19:44:00 +02:00
pickerType = "images"
break;
}
return { status: 200, body: { status: "picker", pickerType: pickerType, picker: obj.picker, audio: audio } };
case 6: // critical error, action should be taken by balancer/other server software
return { status: 500, body: { status: "error", text: obj.t, critical: true } };
2022-07-08 20:17:56 +02:00
default:
return { status: 400, body: { status: "error", text: "Bad Request" } };
}
} catch (e) {
return { status: 500, body: { status: "error", text: "Internal Server Error", critical: true } };
2022-07-08 20:17:56 +02:00
}
}
export function metadataManager(obj) {
let keys = Object.keys(obj);
let tags = ["album", "composer", "genre", "copyright", "encoded_by", "title", "language", "artist", "album_artist", "performer", "disc", "publisher", "track", "encoder", "compilation", "date", "creation_time", "comment"]
let commands = []
for (let i in keys) { if (tags.includes(keys[i])) commands.push('-metadata', `${keys[i]}=${obj[keys[i]]}`) }
return commands;
}
export function cleanString(string) {
for (let i in forbiddenCharsString) {
2023-10-15 11:11:39 +02:00
string = string.replaceAll("/", "_").replaceAll(forbiddenCharsString[i], '')
}
return string;
}
2023-02-26 17:49:25 +01:00
export function verifyLanguageCode(code) {
return RegExp(/[a-z]{2}/).test(String(code.slice(0, 2).toLowerCase())) ? String(code.slice(0, 2).toLowerCase()) : "en"
}
export function languageCode(req) {
2023-02-26 17:49:25 +01:00
return req.header('Accept-Language') ? verifyLanguageCode(req.header('Accept-Language')) : "en"
}
export function unicodeDecode(str) {
return str.replace(/\\u[\dA-F]{4}/gi, (unicode) => {
return String.fromCharCode(parseInt(unicode.replace(/\\u/g, ""), 16));
});
}
export function checkJSONPost(obj) {
let def = {
url: normalizeURL(decodeURIComponent(obj.url)),
2023-02-26 17:49:25 +01:00
vCodec: "h264",
vQuality: "720",
aFormat: "mp3",
filenamePattern: "classic",
isAudioOnly: false,
isNoTTWatermark: false,
isTTFullAudio: false,
isAudioMuted: false,
disableMetadata: false,
dubLang: false,
vimeoDash: false,
twitterGif: false
}
try {
let objKeys = Object.keys(obj);
let defKeys = Object.keys(def);
if (objKeys.length > defKeys.length + 1 || !obj.url) return false;
for (let i in objKeys) {
if (String(objKeys[i]) !== "url" && defKeys.includes(objKeys[i])) {
if (apiVar.booleanOnly.includes(objKeys[i])) {
def[objKeys[i]] = obj[objKeys[i]] ? true : false;
} else {
if (apiVar.allowed[objKeys[i]] && apiVar.allowed[objKeys[i]].includes(obj[objKeys[i]])) def[objKeys[i]] = String(obj[objKeys[i]])
}
}
}
if (def.dubLang)
def.dubLang = verifyLanguageCode(obj.dubLang);
return def
} catch (e) {
return false
}
}
export function getIP(req) {
return req.header('cf-connecting-ip') ? req.header('cf-connecting-ip') : req.ip;
}
2023-08-04 20:43:12 +02:00
export function cleanHTML(html) {
let clean = html.replace(/ {4}/g, '');
clean = clean.replace(/\n/g, '');
return clean
}