2023-12-14 23:04:05 +00:00
|
|
|
import { strict as assert } from "node:assert";
|
|
|
|
|
2024-08-03 14:47:13 +06:00
|
|
|
import { env } from "../config.js";
|
2024-05-15 21:39:44 +06:00
|
|
|
import { createResponse } from "../processing/request.js";
|
2022-12-07 01:21:07 +06:00
|
|
|
|
2024-08-03 14:47:13 +06:00
|
|
|
import { testers } from "./service-patterns.js";
|
2024-08-03 15:02:59 +06:00
|
|
|
import matchAction from "./match-action.js";
|
2022-08-04 17:22:40 +06:00
|
|
|
|
2024-09-01 15:28:29 +06:00
|
|
|
import { friendlyServiceName } from "./service-alias.js";
|
|
|
|
|
2023-02-12 13:40:49 +06:00
|
|
|
import bilibili from "./services/bilibili.js";
|
|
|
|
import reddit from "./services/reddit.js";
|
2023-08-15 14:37:59 +06:00
|
|
|
import twitter from "./services/twitter.js";
|
2023-02-12 13:40:49 +06:00
|
|
|
import youtube from "./services/youtube.js";
|
|
|
|
import vk from "./services/vk.js";
|
2024-01-17 13:02:52 +06:00
|
|
|
import ok from "./services/ok.js";
|
2023-02-12 13:40:49 +06:00
|
|
|
import tiktok from "./services/tiktok.js";
|
|
|
|
import tumblr from "./services/tumblr.js";
|
|
|
|
import vimeo from "./services/vimeo.js";
|
|
|
|
import soundcloud from "./services/soundcloud.js";
|
2023-04-25 01:01:25 +06:00
|
|
|
import instagram from "./services/instagram.js";
|
2023-05-24 12:32:41 -05:00
|
|
|
import pinterest from "./services/pinterest.js";
|
2023-08-19 17:42:10 +01:00
|
|
|
import streamable from "./services/streamable.js";
|
2023-04-29 14:33:36 -05:00
|
|
|
import twitch from "./services/twitch.js";
|
2023-09-16 23:38:07 +06:00
|
|
|
import rutube from "./services/rutube.js";
|
2024-02-21 00:50:48 +00:00
|
|
|
import dailymotion from "./services/dailymotion.js";
|
2024-07-24 10:06:10 -05:00
|
|
|
import snapchat from "./services/snapchat.js";
|
2024-05-29 13:12:52 +06:00
|
|
|
import loom from "./services/loom.js";
|
2024-07-24 22:05:21 +07:00
|
|
|
import facebook from "./services/facebook.js";
|
2024-09-01 14:34:44 +06:00
|
|
|
import bluesky from "./services/bluesky.js";
|
2025-01-20 19:10:02 +06:00
|
|
|
import xiaohongshu from "./services/xiaohongshu.js";
|
2022-07-09 00:17:56 +06:00
|
|
|
|
2024-05-12 16:12:45 +00:00
|
|
|
let freebind;
|
2024-05-15 22:36:24 +06:00
|
|
|
|
2024-09-02 08:27:31 +06:00
|
|
|
export default async function({ host, patternMatch, params }) {
|
|
|
|
const { url } = params;
|
2023-12-14 23:04:05 +00:00
|
|
|
assert(url instanceof URL);
|
2024-05-12 16:12:45 +00:00
|
|
|
let dispatcher, requestIP;
|
|
|
|
|
|
|
|
if (env.freebindCIDR) {
|
|
|
|
if (!freebind) {
|
|
|
|
freebind = await import('freebind');
|
|
|
|
}
|
|
|
|
|
|
|
|
requestIP = freebind.ip.random(env.freebindCIDR);
|
|
|
|
dispatcher = freebind.dispatcherFromIP(requestIP, { strict: false });
|
|
|
|
}
|
2023-12-14 23:04:05 +00:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
try {
|
2024-05-15 22:36:24 +06:00
|
|
|
let r,
|
2024-09-02 08:27:31 +06:00
|
|
|
isAudioOnly = params.downloadMode === "audio",
|
|
|
|
isAudioMuted = params.downloadMode === "mute";
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-05-15 22:36:24 +06:00
|
|
|
if (!testers[host]) {
|
|
|
|
return createResponse("error", {
|
2024-08-19 21:51:45 +06:00
|
|
|
code: "error.api.service.unsupported"
|
2024-05-15 22:36:24 +06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!(testers[host](patternMatch))) {
|
|
|
|
return createResponse("error", {
|
2024-08-24 16:13:42 +06:00
|
|
|
code: "error.api.link.unsupported",
|
2024-08-03 13:51:09 +06:00
|
|
|
context: {
|
2024-09-01 15:28:29 +06:00
|
|
|
service: friendlyServiceName(host),
|
2024-08-03 13:51:09 +06:00
|
|
|
}
|
2024-05-15 22:36:24 +06:00
|
|
|
});
|
|
|
|
}
|
2022-08-04 17:22:40 +06:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
switch (host) {
|
|
|
|
case "twitter":
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await twitter({
|
2024-01-05 12:21:59 +00:00
|
|
|
id: patternMatch.id,
|
2024-01-17 11:38:51 +06:00
|
|
|
index: patternMatch.index - 1,
|
2024-09-02 08:27:31 +06:00
|
|
|
toGif: !!params.twitterGif,
|
|
|
|
alwaysProxy: params.alwaysProxy,
|
2024-05-23 09:22:33 +06:00
|
|
|
dispatcher
|
2022-08-04 17:22:40 +06:00
|
|
|
});
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
case "vk":
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await vk({
|
2024-11-28 16:01:26 +06:00
|
|
|
ownerId: patternMatch.ownerId,
|
2024-01-17 13:02:52 +06:00
|
|
|
videoId: patternMatch.videoId,
|
2024-11-28 16:01:26 +06:00
|
|
|
accessKey: patternMatch.accessKey,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality
|
2024-01-17 13:02:52 +06:00
|
|
|
});
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-01-17 13:02:52 +06:00
|
|
|
case "ok":
|
|
|
|
r = await ok({
|
|
|
|
id: patternMatch.id,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality
|
2022-08-04 17:22:40 +06:00
|
|
|
});
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
case "bilibili":
|
2024-02-15 01:42:15 +00:00
|
|
|
r = await bilibili(patternMatch);
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
case "youtube":
|
2022-08-04 17:22:40 +06:00
|
|
|
let fetchInfo = {
|
2024-10-28 15:17:54 +06:00
|
|
|
dispatcher,
|
2024-01-17 13:02:52 +06:00
|
|
|
id: patternMatch.id.slice(0, 11),
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
|
|
|
format: params.youtubeVideoCodec,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
|
|
|
isAudioMuted,
|
2024-09-02 08:27:31 +06:00
|
|
|
dubLang: params.youtubeDubLang,
|
2024-10-28 15:17:54 +06:00
|
|
|
youtubeHLS: params.youtubeHLS,
|
2023-02-26 22:49:25 +06:00
|
|
|
}
|
2023-12-14 22:43:57 +00:00
|
|
|
|
2024-08-03 23:06:32 +06:00
|
|
|
if (url.hostname === "music.youtube.com" || isAudioOnly) {
|
2023-02-26 22:49:25 +06:00
|
|
|
fetchInfo.quality = "max";
|
|
|
|
fetchInfo.format = "vp9";
|
2024-08-03 23:06:32 +06:00
|
|
|
fetchInfo.isAudioOnly = true;
|
|
|
|
fetchInfo.isAudioMuted = false;
|
2022-08-04 17:22:40 +06:00
|
|
|
}
|
2023-12-14 22:43:57 +00:00
|
|
|
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await youtube(fetchInfo);
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
case "reddit":
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await reddit({
|
2024-01-17 13:02:52 +06:00
|
|
|
sub: patternMatch.sub,
|
2024-05-03 14:09:46 +06:00
|
|
|
id: patternMatch.id,
|
|
|
|
user: patternMatch.user
|
2022-08-04 17:22:40 +06:00
|
|
|
});
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-07-28 22:03:17 +06:00
|
|
|
case "tiktok":
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await tiktok({
|
2024-01-17 13:02:52 +06:00
|
|
|
postId: patternMatch.postId,
|
2024-10-12 22:06:54 +06:00
|
|
|
shortLink: patternMatch.shortLink,
|
2024-09-02 08:27:31 +06:00
|
|
|
fullAudio: params.tiktokFullAudio,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
2024-09-02 08:27:31 +06:00
|
|
|
h265: params.tiktokH265,
|
|
|
|
alwaysProxy: params.alwaysProxy,
|
2022-08-04 17:22:40 +06:00
|
|
|
});
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-08-01 23:53:44 +06:00
|
|
|
case "tumblr":
|
2022-08-04 17:22:40 +06:00
|
|
|
r = await tumblr({
|
2023-12-14 22:43:57 +00:00
|
|
|
id: patternMatch.id,
|
|
|
|
user: patternMatch.user,
|
|
|
|
url
|
2022-08-04 17:22:40 +06:00
|
|
|
});
|
2022-08-12 19:36:19 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-08-13 17:14:09 +06:00
|
|
|
case "vimeo":
|
|
|
|
r = await vimeo({
|
2024-01-17 13:02:52 +06:00
|
|
|
id: patternMatch.id.slice(0, 11),
|
2024-03-04 23:26:14 +00:00
|
|
|
password: patternMatch.password,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
2022-08-13 17:14:09 +06:00
|
|
|
});
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2022-08-22 20:10:54 +06:00
|
|
|
case "soundcloud":
|
2023-02-12 13:40:49 +06:00
|
|
|
isAudioOnly = true;
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioMuted = false;
|
2022-08-22 20:10:54 +06:00
|
|
|
r = await soundcloud({
|
2023-12-14 23:04:05 +00:00
|
|
|
url,
|
2024-01-17 13:02:52 +06:00
|
|
|
author: patternMatch.author,
|
|
|
|
song: patternMatch.song,
|
2024-09-02 08:27:31 +06:00
|
|
|
format: params.audioFormat,
|
2024-01-17 13:02:52 +06:00
|
|
|
shortLink: patternMatch.shortLink || false,
|
|
|
|
accessKey: patternMatch.accessKey || false
|
2022-08-22 20:10:54 +06:00
|
|
|
});
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2023-04-25 01:01:25 +06:00
|
|
|
case "instagram":
|
2023-08-26 14:58:38 +00:00
|
|
|
r = await instagram({
|
|
|
|
...patternMatch,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
|
|
|
alwaysProxy: params.alwaysProxy,
|
2024-05-12 16:12:45 +00:00
|
|
|
dispatcher
|
2023-08-26 14:58:38 +00:00
|
|
|
})
|
2023-04-29 22:26:49 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2023-05-24 12:32:41 -05:00
|
|
|
case "pinterest":
|
2023-11-07 22:37:47 +06:00
|
|
|
r = await pinterest({
|
2024-01-17 15:05:39 +06:00
|
|
|
id: patternMatch.id,
|
|
|
|
shortLink: patternMatch.shortLink || false
|
2023-11-07 22:37:47 +06:00
|
|
|
});
|
2023-05-24 12:32:41 -05:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2023-08-19 17:42:10 +01:00
|
|
|
case "streamable":
|
|
|
|
r = await streamable({
|
2024-01-17 13:02:52 +06:00
|
|
|
id: patternMatch.id,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
2023-08-19 17:42:10 +01:00
|
|
|
});
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2023-04-29 14:33:36 -05:00
|
|
|
case "twitch":
|
|
|
|
r = await twitch({
|
2024-01-17 13:02:52 +06:00
|
|
|
clipId: patternMatch.clip || false,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
2023-04-29 14:33:36 -05:00
|
|
|
});
|
2023-09-16 16:27:53 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2023-09-16 23:38:07 +06:00
|
|
|
case "rutube":
|
|
|
|
r = await rutube({
|
2024-01-17 13:02:52 +06:00
|
|
|
id: patternMatch.id,
|
2024-04-29 21:36:35 +06:00
|
|
|
yappyId: patternMatch.yappyId,
|
2024-05-29 13:02:05 +06:00
|
|
|
key: patternMatch.key,
|
2024-09-02 08:27:31 +06:00
|
|
|
quality: params.videoQuality,
|
2024-08-03 23:06:32 +06:00
|
|
|
isAudioOnly,
|
2023-09-16 23:38:07 +06:00
|
|
|
});
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-02-21 00:50:48 +00:00
|
|
|
case "dailymotion":
|
|
|
|
r = await dailymotion(patternMatch);
|
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-07-24 10:06:10 -05:00
|
|
|
case "snapchat":
|
2024-08-31 14:52:12 +06:00
|
|
|
r = await snapchat({
|
|
|
|
...patternMatch,
|
2024-09-02 08:27:31 +06:00
|
|
|
alwaysProxy: params.alwaysProxy,
|
2024-08-31 14:52:12 +06:00
|
|
|
});
|
2024-07-24 15:17:58 +00:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-05-29 13:12:52 +06:00
|
|
|
case "loom":
|
|
|
|
r = await loom({
|
|
|
|
id: patternMatch.id
|
|
|
|
});
|
2024-07-24 15:17:58 +00:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-07-24 22:05:21 +07:00
|
|
|
case "facebook":
|
|
|
|
r = await facebook({
|
2024-07-25 11:57:02 +06:00
|
|
|
...patternMatch
|
2024-07-24 22:05:21 +07:00
|
|
|
});
|
2024-05-29 13:12:52 +06:00
|
|
|
break;
|
2024-08-03 23:06:32 +06:00
|
|
|
|
2024-09-01 14:34:44 +06:00
|
|
|
case "bsky":
|
|
|
|
r = await bluesky({
|
2024-09-01 16:37:24 +06:00
|
|
|
...patternMatch,
|
2024-11-11 12:23:53 +06:00
|
|
|
alwaysProxy: params.alwaysProxy,
|
|
|
|
dispatcher
|
2024-09-01 14:34:44 +06:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2025-01-20 19:10:02 +06:00
|
|
|
case "xiaohongshu":
|
|
|
|
r = await xiaohongshu({
|
|
|
|
...patternMatch,
|
|
|
|
h265: params.tiktokH265,
|
|
|
|
isAudioOnly,
|
|
|
|
dispatcher,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2022-07-09 00:17:56 +06:00
|
|
|
default:
|
2024-05-15 22:36:24 +06:00
|
|
|
return createResponse("error", {
|
2024-08-19 21:51:45 +06:00
|
|
|
code: "error.api.service.unsupported"
|
2024-05-15 22:36:24 +06:00
|
|
|
});
|
2022-07-09 00:17:56 +06:00
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-08-03 23:06:32 +06:00
|
|
|
if (r.isAudioOnly) {
|
|
|
|
isAudioOnly = true;
|
|
|
|
isAudioMuted = false;
|
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-05-15 22:36:24 +06:00
|
|
|
if (r.error && r.critical) {
|
|
|
|
return createResponse("critical", {
|
2024-08-20 21:10:37 +06:00
|
|
|
code: `error.api.${r.error}`,
|
2024-05-15 22:36:24 +06:00
|
|
|
})
|
|
|
|
}
|
2024-08-03 13:51:09 +06:00
|
|
|
|
2024-05-15 22:36:24 +06:00
|
|
|
if (r.error) {
|
2024-08-24 16:56:07 +06:00
|
|
|
let context;
|
|
|
|
switch(r.error) {
|
|
|
|
case "content.too_long":
|
|
|
|
context = {
|
|
|
|
limit: env.durationLimit / 60,
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "fetch.fail":
|
|
|
|
case "fetch.rate":
|
2024-08-24 17:28:43 +06:00
|
|
|
case "fetch.critical":
|
2024-08-24 16:56:07 +06:00
|
|
|
case "link.unsupported":
|
2024-08-24 17:24:51 +06:00
|
|
|
case "content.video.unavailable":
|
2024-08-24 16:56:07 +06:00
|
|
|
context = {
|
2024-09-01 15:28:29 +06:00
|
|
|
service: friendlyServiceName(host),
|
2024-08-24 16:56:07 +06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-05-15 21:39:44 +06:00
|
|
|
return createResponse("error", {
|
2024-08-20 21:10:37 +06:00
|
|
|
code: `error.api.${r.error}`,
|
2024-09-01 15:28:29 +06:00
|
|
|
context,
|
2023-12-25 12:59:00 +06:00
|
|
|
})
|
2024-05-15 22:36:24 +06:00
|
|
|
}
|
2023-02-12 13:40:49 +06:00
|
|
|
|
2024-08-03 15:02:59 +06:00
|
|
|
return matchAction({
|
|
|
|
r,
|
|
|
|
host,
|
2024-09-02 08:27:31 +06:00
|
|
|
audioFormat: params.audioFormat,
|
2024-08-03 15:02:59 +06:00
|
|
|
isAudioOnly,
|
|
|
|
isAudioMuted,
|
2024-09-02 08:27:31 +06:00
|
|
|
disableMetadata: params.disableMetadata,
|
|
|
|
filenameStyle: params.filenameStyle,
|
|
|
|
twitterGif: params.twitterGif,
|
2024-08-22 19:35:17 +06:00
|
|
|
requestIP,
|
2024-09-02 08:27:31 +06:00
|
|
|
audioBitrate: params.audioBitrate,
|
|
|
|
alwaysProxy: params.alwaysProxy,
|
2025-01-29 15:00:50 +06:00
|
|
|
localProcessing: params.localProcessing,
|
2024-08-03 15:02:59 +06:00
|
|
|
})
|
2024-05-15 21:39:44 +06:00
|
|
|
} catch {
|
2024-05-15 22:36:24 +06:00
|
|
|
return createResponse("error", {
|
2024-08-19 21:51:45 +06:00
|
|
|
code: "error.api.fetch.critical",
|
2024-08-03 13:51:09 +06:00
|
|
|
context: {
|
2024-09-01 15:28:29 +06:00
|
|
|
service: friendlyServiceName(host),
|
2024-08-03 13:51:09 +06:00
|
|
|
}
|
2024-05-15 22:36:24 +06:00
|
|
|
})
|
2022-07-09 00:17:56 +06:00
|
|
|
}
|
2022-08-01 21:48:37 +06:00
|
|
|
}
|