2023-02-12 08:40:49 +01:00
|
|
|
import { audioIgnore, services, supportedAudio } from "../config.js";
|
|
|
|
import { apiJSON } from "../sub/utils.js";
|
2022-12-06 20:21:07 +01:00
|
|
|
import loc from "../../localization/manager.js";
|
2023-10-12 19:14:54 +02:00
|
|
|
import createFilename from "./createFilename.js";
|
2022-08-12 15:36:19 +02:00
|
|
|
|
2024-01-17 06:38:51 +01:00
|
|
|
export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, filenamePattern, toGif) {
|
2023-02-12 08:40:49 +01:00
|
|
|
let action,
|
|
|
|
responseType = 2,
|
|
|
|
defaultParams = {
|
|
|
|
u: r.urls,
|
2022-12-17 12:09:49 +01:00
|
|
|
service: host,
|
2023-10-12 19:14:54 +02:00
|
|
|
filename: r.filenameAttributes ?
|
|
|
|
createFilename(r.filenameAttributes, filenamePattern, isAudioOnly, isAudioMuted) : r.filename,
|
2023-08-24 10:31:39 +02:00
|
|
|
fileMetadata: !disableMetadata ? r.fileMetadata : false
|
2023-02-12 08:40:49 +01:00
|
|
|
},
|
2023-12-25 17:21:06 +01:00
|
|
|
params = {},
|
2024-01-17 06:38:51 +01:00
|
|
|
audioFormat = String(userFormat);
|
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";
|
|
|
|
else if (r.isM3U8) action = "singleM3U8";
|
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:
|
|
|
|
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') });
|
|
|
|
|
2023-08-20 15:36:56 +02:00
|
|
|
case "photo":
|
|
|
|
responseType = 1;
|
|
|
|
break;
|
2024-01-17 06:38:51 +01:00
|
|
|
|
|
|
|
case "gif":
|
|
|
|
params = { type: "gif" }
|
|
|
|
break;
|
2023-12-25 17:21:06 +01:00
|
|
|
|
|
|
|
case "singleM3U8":
|
|
|
|
params = { type: "remux" }
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "muteVideo":
|
|
|
|
params = {
|
|
|
|
type: Array.isArray(r.urls) ? "bridge" : "mute",
|
|
|
|
u: Array.isArray(r.urls) ? r.urls[0] : r.urls,
|
|
|
|
mute: true
|
|
|
|
}
|
|
|
|
if (host === "reddit" && r.typeId === 1) responseType = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "picker":
|
|
|
|
responseType = 5;
|
|
|
|
switch (host) {
|
|
|
|
case "instagram":
|
|
|
|
case "twitter":
|
|
|
|
params = { picker: r.picker };
|
|
|
|
break;
|
|
|
|
case "douyin":
|
|
|
|
case "tiktok":
|
|
|
|
let pickerType = "render";
|
|
|
|
if (audioFormat === "mp3" || audioFormat === "best") {
|
|
|
|
audioFormat = "mp3";
|
|
|
|
pickerType = "bridge"
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
type: pickerType,
|
|
|
|
picker: r.picker,
|
|
|
|
u: Array.isArray(r.urls) ? r.urls[1] : r.urls,
|
|
|
|
copy: audioFormat === "best" ? true : false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
responseType = 1;
|
|
|
|
}
|
|
|
|
break;
|
2023-12-17 18:05:43 +01:00
|
|
|
|
|
|
|
case "twitter":
|
|
|
|
if (r.type === "remux") {
|
|
|
|
params = { type: r.type };
|
|
|
|
} else {
|
|
|
|
responseType = 1;
|
|
|
|
}
|
|
|
|
break;
|
2023-02-12 08:40:49 +01:00
|
|
|
|
|
|
|
case "vk":
|
|
|
|
case "douyin":
|
|
|
|
case "tiktok":
|
|
|
|
params = { type: "bridge" };
|
|
|
|
break;
|
2023-09-16 12:27:53 +02:00
|
|
|
|
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":
|
2023-02-12 08:40:49 +01:00
|
|
|
responseType = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "audio":
|
2023-12-02 15:44:19 +01:00
|
|
|
if ((host === "reddit" && r.typeId === 1) || audioIgnore.includes(host)) {
|
|
|
|
return apiJSON(0, { t: loc(lang, 'ErrorEmptyDownload') })
|
|
|
|
}
|
2023-02-12 08:40:49 +01:00
|
|
|
|
2023-12-25 17:21:06 +01:00
|
|
|
let processType = "render",
|
|
|
|
copy = false;
|
2023-02-12 08:40:49 +01: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";
|
|
|
|
|
|
|
|
if (isBestAudioDefined || isBestHostAudio) {
|
2024-04-30 07:24:12 +02:00
|
|
|
audioFormat = serviceBestAudio;
|
2023-12-25 17:21:06 +01:00
|
|
|
processType = "bridge";
|
2024-04-30 09:22:29 +02:00
|
|
|
if (isSoundCloud) {
|
|
|
|
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
|
|
|
|
|
|
|
return apiJSON(responseType, {...defaultParams, ...params})
|
2022-08-12 15:36:19 +02:00
|
|
|
}
|