2022-07-08 20:17:56 +02:00
|
|
|
import { createStream } from "../stream/manage.js";
|
|
|
|
|
2023-08-20 14:14:15 +02:00
|
|
|
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"],
|
2023-10-12 19:14:54 +02:00
|
|
|
aFormat: ["best", "mp3", "ogg", "wav", "opus"],
|
|
|
|
filenamePattern: ["classic", "pretty", "basic", "nerdy"]
|
2022-12-06 20:21:07 +01:00
|
|
|
},
|
2023-08-24 10:31:39 +02:00
|
|
|
booleanOnly: ["isAudioOnly", "isNoTTWatermark", "isTTFullAudio", "isAudioMuted", "dubLang", "vimeoDash", "disableMetadata"]
|
2022-12-06 20:21:07 +01:00
|
|
|
}
|
2023-11-23 15:44:25 +01:00
|
|
|
const forbiddenChars = ['}', '{', '(', ')', '\\', '>', '<', '^', '*', '!', '~', ';', ':', ',', '`', '[', ']', '#', '$', '"', "'", "@", '=='];
|
2023-08-20 14:14:15 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-11-12 17:40:11 +01:00
|
|
|
return { status: 200, body: { status: "picker", pickerType: pickerType, picker: obj.picker, audio: audio } };
|
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" } };
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 15:03:11 +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;
|
|
|
|
}
|
2022-07-08 20:17:56 +02:00
|
|
|
export function cleanURL(url, host) {
|
2023-09-16 19:38:07 +02:00
|
|
|
switch (host) {
|
2023-02-13 14:44:58 +01:00
|
|
|
case "vk":
|
2023-04-29 17:30:59 +02:00
|
|
|
url = url.includes('clip') ? url.split('&')[0] : url.split('?')[0];
|
|
|
|
break;
|
2023-01-13 19:34:48 +01:00
|
|
|
case "youtube":
|
|
|
|
url = url.split('&')[0];
|
|
|
|
break;
|
|
|
|
case "tiktok":
|
|
|
|
url = url.replace(/@([a-zA-Z]+(\.[a-zA-Z]+)+)/, "@a")
|
2023-05-24 19:32:41 +02:00
|
|
|
case "pinterest":
|
|
|
|
url = url.replace(/:\/\/(?:www.)pinterest(?:\.[a-z.]+)/, "://pinterest.com")
|
2023-01-13 19:34:48 +01:00
|
|
|
default:
|
|
|
|
url = url.split('?')[0];
|
|
|
|
if (url.substring(url.length - 1) === "/") url = url.substring(0, url.length - 1);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-06 20:21:07 +01:00
|
|
|
for (let i in forbiddenChars) {
|
|
|
|
url = url.replaceAll(forbiddenChars[i], '')
|
|
|
|
}
|
|
|
|
url = url.replace('https//', 'https://')
|
|
|
|
return url.slice(0, 128)
|
2022-07-24 12:54:05 +02:00
|
|
|
}
|
2023-08-20 14:14:15 +02:00
|
|
|
export function cleanString(string) {
|
|
|
|
for (let i in forbiddenCharsString) {
|
2023-10-15 11:11:39 +02:00
|
|
|
string = string.replaceAll("/", "_").replaceAll(forbiddenCharsString[i], '')
|
2023-08-20 14:14:15 +02:00
|
|
|
}
|
|
|
|
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"
|
|
|
|
}
|
2022-07-24 12:54:05 +02:00
|
|
|
export function languageCode(req) {
|
2023-02-26 17:49:25 +01:00
|
|
|
return req.header('Accept-Language') ? verifyLanguageCode(req.header('Accept-Language')) : "en"
|
2022-07-28 18:03:17 +02:00
|
|
|
}
|
|
|
|
export function unicodeDecode(str) {
|
|
|
|
return str.replace(/\\u[\dA-F]{4}/gi, (unicode) => {
|
|
|
|
return String.fromCharCode(parseInt(unicode.replace(/\\u/g, ""), 16));
|
|
|
|
});
|
2022-08-01 17:48:37 +02:00
|
|
|
}
|
2022-11-12 17:40:11 +01:00
|
|
|
export function checkJSONPost(obj) {
|
|
|
|
let def = {
|
2023-02-26 17:49:25 +01:00
|
|
|
vCodec: "h264",
|
|
|
|
vQuality: "720",
|
2022-11-12 17:40:11 +01:00
|
|
|
aFormat: "mp3",
|
2023-10-12 19:14:54 +02:00
|
|
|
filenamePattern: "classic",
|
2022-11-12 17:40:11 +01:00
|
|
|
isAudioOnly: false,
|
|
|
|
isNoTTWatermark: false,
|
2022-12-17 12:09:49 +01:00
|
|
|
isTTFullAudio: false,
|
|
|
|
isAudioMuted: false,
|
2023-08-24 10:31:39 +02:00
|
|
|
disableMetadata: false,
|
2023-03-15 17:18:31 +01:00
|
|
|
dubLang: false,
|
|
|
|
vimeoDash: false
|
2022-11-12 17:40:11 +01:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
let objKeys = Object.keys(obj);
|
2023-02-09 15:45:17 +01:00
|
|
|
let defKeys = Object.keys(def);
|
2023-08-24 10:31:39 +02:00
|
|
|
if (objKeys.length > defKeys.length + 1 || !obj.url) return false;
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2023-02-09 15:45:17 +01:00
|
|
|
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]])
|
2022-11-12 17:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2023-02-26 17:49:25 +01:00
|
|
|
if (def.dubLang) def.dubLang = verifyLanguageCode(obj.dubLang);
|
|
|
|
|
2023-02-12 08:40:49 +01:00
|
|
|
obj["url"] = decodeURIComponent(String(obj["url"]));
|
2023-02-09 15:45:17 +01:00
|
|
|
let hostname = obj["url"].replace("https://", "").replace(' ', '').split('&')[0].split("/")[0].split("."),
|
2023-02-12 08:40:49 +01:00
|
|
|
host = hostname[hostname.length - 2];
|
|
|
|
def["url"] = encodeURIComponent(cleanURL(obj["url"], host));
|
|
|
|
|
2023-02-09 15:45:17 +01:00
|
|
|
return def
|
2022-11-12 17:40:11 +01:00
|
|
|
} catch (e) {
|
2023-02-12 08:40:49 +01:00
|
|
|
return false
|
2022-11-12 17:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-09 06:58:23 +02:00
|
|
|
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
|
|
|
|
}
|