2023-02-12 08:40:49 +01:00
|
|
|
import { audioIgnore, services, supportedAudio } from "../config.js";
|
2024-08-03 10:47:13 +02:00
|
|
|
import { createResponse } from "./request.js";
|
|
|
|
import createFilename from "./create-filename.js";
|
2024-05-21 22:41:43 +02:00
|
|
|
import { createStream } from "../stream/manage.js";
|
2022-08-12 15:36:19 +02:00
|
|
|
|
2024-08-03 11:02:59 +02:00
|
|
|
export default function({ r, host, audioFormat, isAudioOnly, isAudioMuted, disableMetadata, filenameStyle, toGif, requestIP }) {
|
2023-02-12 08:40:49 +01:00
|
|
|
let action,
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "stream",
|
2023-02-12 08:40:49 +01:00
|
|
|
defaultParams = {
|
|
|
|
u: r.urls,
|
2024-05-23 18:29:39 +02:00
|
|
|
headers: r.headers,
|
2022-12-17 12:09:49 +01:00
|
|
|
service: host,
|
2023-10-12 19:14:54 +02:00
|
|
|
filename: r.filenameAttributes ?
|
2024-08-03 11:02:59 +02:00
|
|
|
createFilename(r.filenameAttributes, filenameStyle, isAudioOnly, isAudioMuted) : r.filename,
|
2024-05-12 18:35:52 +02:00
|
|
|
fileMetadata: !disableMetadata ? r.fileMetadata : false,
|
|
|
|
requestIP
|
2023-02-12 08:40:49 +01:00
|
|
|
},
|
2024-08-03 11:02:59 +02:00
|
|
|
params = {};
|
2023-08-24 10:31:39 +02:00
|
|
|
|
2023-08-20 18:30:26 +02:00
|
|
|
if (r.isPhoto) action = "photo";
|
|
|
|
else if (r.picker) action = "picker"
|
2024-01-17 15:34:31 +01:00
|
|
|
else if (r.isGif && toGif) action = "gif";
|
2023-08-20 18:11:42 +02:00
|
|
|
else if (isAudioMuted) action = "muteVideo";
|
2023-08-20 18:30:26 +02:00
|
|
|
else if (isAudioOnly) action = "audio";
|
2024-06-23 18:13:36 +02:00
|
|
|
else if (r.isM3U8) action = "m3u8";
|
2023-08-20 18:11:42 +02:00
|
|
|
else action = "video";
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
if (action === "picker" || action === "audio") {
|
2023-10-12 19:14:54 +02:00
|
|
|
if (!r.filenameAttributes) defaultParams.filename = r.audioFilename;
|
2023-02-12 08:40:49 +01:00
|
|
|
defaultParams.isAudioOnly = true;
|
|
|
|
defaultParams.audioFormat = audioFormat;
|
|
|
|
}
|
2023-10-12 19:14:54 +02:00
|
|
|
if (isAudioMuted && !r.filenameAttributes) {
|
|
|
|
defaultParams.filename = r.filename.replace('.', '_mute.')
|
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
switch (action) {
|
2023-12-25 17:21:06 +01:00
|
|
|
default:
|
2024-08-03 09:51:09 +02:00
|
|
|
return createResponse("error", {
|
|
|
|
code: "ErrorEmptyDownload"
|
|
|
|
});
|
2023-12-25 17:21:06 +01:00
|
|
|
|
2023-08-20 15:36:56 +02:00
|
|
|
case "photo":
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "redirect";
|
2023-08-20 15:36:56 +02:00
|
|
|
break;
|
2024-05-21 22:41:43 +02:00
|
|
|
|
2024-01-17 06:38:51 +01:00
|
|
|
case "gif":
|
|
|
|
params = { type: "gif" }
|
|
|
|
break;
|
2023-12-25 17:21:06 +01:00
|
|
|
|
2024-06-23 18:13:36 +02:00
|
|
|
case "m3u8":
|
|
|
|
params = {
|
|
|
|
type: Array.isArray(r.urls) ? "render" : "remux"
|
|
|
|
}
|
2023-12-25 17:21:06 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "muteVideo":
|
2024-06-23 18:13:36 +02:00
|
|
|
let muteType = "mute";
|
|
|
|
if (Array.isArray(r.urls) && !r.isM3U8) {
|
|
|
|
muteType = "bridge";
|
|
|
|
}
|
2023-12-25 17:21:06 +01:00
|
|
|
params = {
|
2024-06-23 18:13:36 +02:00
|
|
|
type: muteType,
|
2023-12-25 17:21:06 +01:00
|
|
|
u: Array.isArray(r.urls) ? r.urls[0] : r.urls,
|
|
|
|
mute: true
|
|
|
|
}
|
2024-05-15 17:39:44 +02:00
|
|
|
if (host === "reddit" && r.typeId === "redirect")
|
|
|
|
responseType = "redirect";
|
2023-12-25 17:21:06 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "picker":
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "picker";
|
2023-12-25 17:21:06 +01:00
|
|
|
switch (host) {
|
|
|
|
case "instagram":
|
|
|
|
case "twitter":
|
2024-07-24 17:06:10 +02:00
|
|
|
case "snapchat":
|
2023-12-25 17:21:06 +01:00
|
|
|
params = { picker: r.picker };
|
|
|
|
break;
|
|
|
|
case "tiktok":
|
2024-05-21 22:41:43 +02:00
|
|
|
let audioStreamType = "render";
|
|
|
|
if (r.bestAudio === "mp3" && (audioFormat === "mp3" || audioFormat === "best")) {
|
2023-12-25 17:21:06 +01:00
|
|
|
audioFormat = "mp3";
|
2024-05-21 22:41:43 +02:00
|
|
|
audioStreamType = "bridge"
|
2023-12-25 17:21:06 +01:00
|
|
|
}
|
|
|
|
params = {
|
|
|
|
picker: r.picker,
|
2024-05-21 22:41:43 +02:00
|
|
|
u: createStream({
|
|
|
|
service: "tiktok",
|
|
|
|
type: audioStreamType,
|
|
|
|
u: r.urls,
|
2024-05-23 18:29:39 +02:00
|
|
|
headers: r.headers,
|
2024-05-21 22:41:43 +02:00
|
|
|
filename: r.audioFilename,
|
|
|
|
isAudioOnly: true,
|
|
|
|
audioFormat,
|
|
|
|
}),
|
|
|
|
copy: audioFormat === "best"
|
2023-12-25 17:21:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-02-12 08:40:49 +01:00
|
|
|
case "video":
|
|
|
|
switch (host) {
|
|
|
|
case "bilibili":
|
2023-08-20 14:14:15 +02:00
|
|
|
params = { type: "render" };
|
2023-02-12 08:40:49 +01:00
|
|
|
break;
|
|
|
|
case "youtube":
|
2023-08-20 14:14:15 +02:00
|
|
|
params = { type: r.type };
|
2023-02-12 08:40:49 +01:00
|
|
|
break;
|
|
|
|
case "reddit":
|
|
|
|
responseType = r.typeId;
|
|
|
|
params = { type: r.type };
|
|
|
|
break;
|
|
|
|
case "vimeo":
|
|
|
|
if (Array.isArray(r.urls)) {
|
|
|
|
params = { type: "render" }
|
|
|
|
} else {
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "redirect";
|
2023-02-12 08:40:49 +01:00
|
|
|
}
|
|
|
|
break;
|
2024-05-21 22:41:43 +02:00
|
|
|
|
2023-12-17 18:05:43 +01:00
|
|
|
case "twitter":
|
|
|
|
if (r.type === "remux") {
|
|
|
|
params = { type: r.type };
|
|
|
|
} else {
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "redirect";
|
2023-12-17 18:05:43 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
case "vk":
|
|
|
|
case "tiktok":
|
|
|
|
params = { type: "bridge" };
|
|
|
|
break;
|
2023-09-16 12:27:53 +02:00
|
|
|
|
2024-07-24 17:05:21 +02:00
|
|
|
case "facebook":
|
2023-04-29 18:26:49 +02:00
|
|
|
case "vine":
|
2023-04-24 21:01:25 +02:00
|
|
|
case "instagram":
|
2023-02-12 08:40:49 +01:00
|
|
|
case "tumblr":
|
2023-05-24 19:32:41 +02:00
|
|
|
case "pinterest":
|
2023-08-19 18:42:10 +02:00
|
|
|
case "streamable":
|
2024-07-24 17:06:10 +02:00
|
|
|
case "snapchat":
|
2024-05-29 09:12:52 +02:00
|
|
|
case "loom":
|
2024-05-15 17:39:44 +02:00
|
|
|
responseType = "redirect";
|
2023-02-12 08:40:49 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-05-21 22:41:43 +02:00
|
|
|
case "audio":
|
2024-08-03 09:51:09 +02:00
|
|
|
if (audioIgnore.includes(host) || (host === "reddit" && r.typeId === "redirect")) {
|
|
|
|
return createResponse("error", {
|
|
|
|
code: "ErrorEmptyDownload"
|
|
|
|
})
|
2023-12-02 15:44:19 +01:00
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2023-12-25 17:21:06 +01:00
|
|
|
let processType = "render",
|
|
|
|
copy = false;
|
2024-05-21 22:41:43 +02:00
|
|
|
|
2023-12-25 17:21:06 +01:00
|
|
|
if (!supportedAudio.includes(audioFormat)) {
|
|
|
|
audioFormat = "best"
|
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2024-04-30 07:24:12 +02:00
|
|
|
const serviceBestAudio = r.bestAudio || services[host]["bestAudio"];
|
2023-12-25 17:21:06 +01:00
|
|
|
const isBestAudio = audioFormat === "best";
|
2024-04-30 09:22:29 +02:00
|
|
|
const isBestOrMp3 = isBestAudio || audioFormat === "mp3";
|
2024-04-30 07:24:12 +02:00
|
|
|
const isBestAudioDefined = isBestAudio && serviceBestAudio;
|
|
|
|
const isBestHostAudio = serviceBestAudio && (audioFormat === serviceBestAudio);
|
2023-12-25 17:21:06 +01:00
|
|
|
|
2024-02-21 12:48:00 +01:00
|
|
|
const isTumblrAudio = host === "tumblr" && !r.filename;
|
2023-12-25 17:21:06 +01:00
|
|
|
const isSoundCloud = host === "soundcloud";
|
2024-05-21 22:41:43 +02:00
|
|
|
const isTiktok = host === "tiktok";
|
2023-12-25 17:21:06 +01:00
|
|
|
|
|
|
|
if (isBestAudioDefined || isBestHostAudio) {
|
2024-04-30 07:24:12 +02:00
|
|
|
audioFormat = serviceBestAudio;
|
2023-12-25 17:21:06 +01:00
|
|
|
processType = "bridge";
|
2024-05-21 22:41:43 +02:00
|
|
|
if (isSoundCloud || (isTiktok && audioFormat === "m4a")) {
|
2024-04-30 09:22:29 +02:00
|
|
|
processType = "render"
|
|
|
|
copy = true
|
|
|
|
}
|
2023-12-25 17:21:06 +01:00
|
|
|
} else if (isBestAudio && !isSoundCloud) {
|
2023-02-12 08:40:49 +01:00
|
|
|
audioFormat = "m4a";
|
2023-12-25 17:21:06 +01:00
|
|
|
copy = true
|
2022-08-12 15:36:19 +02:00
|
|
|
}
|
2023-12-25 17:21:06 +01:00
|
|
|
|
2024-02-21 12:48:00 +01:00
|
|
|
if (isTumblrAudio && isBestOrMp3) {
|
|
|
|
audioFormat = "mp3";
|
|
|
|
processType = "bridge"
|
|
|
|
}
|
|
|
|
|
2023-03-15 17:18:31 +01:00
|
|
|
if (r.isM3U8 || host === "vimeo") {
|
|
|
|
copy = false;
|
|
|
|
processType = "render"
|
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
params = {
|
|
|
|
type: processType,
|
|
|
|
u: Array.isArray(r.urls) ? r.urls[1] : r.urls,
|
|
|
|
audioFormat: audioFormat,
|
2023-08-20 14:14:15 +02:00
|
|
|
copy: copy
|
2022-12-06 20:21:07 +01:00
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
break;
|
2022-08-12 15:36:19 +02:00
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2024-05-15 17:39:44 +02:00
|
|
|
return createResponse(responseType, {...defaultParams, ...params})
|
2022-08-12 15:36:19 +02:00
|
|
|
}
|