-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcobalt.ts
136 lines (128 loc) · 3.18 KB
/
cobalt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { z } from "zod";
import { USER_AGENT } from "./consts";
type CobaltInstance = string;
export const COBALT_INSTANCES = (() => {
const entries = process.env.COBALT_INSTANCES?.split(",") || [];
const urls = entries.map((url) => url.trim()).filter((url) => url.length > 0);
if (urls.length === 0) {
return [
"https://cobalt.izq.nulo.in/",
"https://dorsiblancoapicobalt.nulo.in/",
];
}
return urls;
})();
// List of allowed domains for video downloads
const ALLOWED_DOMAINS = [
"tiktok.com",
"instagram.com",
"twitter.com",
"x.com",
"youtube.com",
"youtu.be",
"bsky.app",
];
export function getRealUrl(url: string): string | null {
url = url.replaceAll("ddinstagram", "instagram");
url = url.replaceAll("fixupx.com", "x.com");
url = url.replaceAll("fxtwitter.com", "x.com");
try {
const urlObj = new URL(url);
if (
!ALLOWED_DOMAINS.some(
(domain) =>
urlObj.hostname === domain ||
urlObj.hostname === `www.${domain}` ||
urlObj.hostname.endsWith(`.${domain}`)
)
)
return null;
return url;
} catch {
return null;
}
}
export const CobaltResult = z.discriminatedUnion("status", [
z.object({
status: z.literal("error"),
error: z.object({
code: z.string(),
context: z
.object({
service: z.string().optional(),
limit: z.number().optional(),
})
.optional(),
}),
}),
z.object({
status: z.literal("picker"),
audio: z.string().optional(),
audioFilename: z.string().optional(),
picker: z.array(
z.object({
type: z.enum(["photo", "video", "gif"]),
url: z.string(),
thumb: z.string().optional(),
})
),
}),
z.object({
status: z.enum(["tunnel", "redirect"]),
url: z.string(),
filename: z.string(),
}),
]);
export type CobaltResult = z.infer<typeof CobaltResult>;
export type VideoQuality =
| "144"
| "240"
| "360"
| "480"
| "720"
| "1080"
| "1440"
| "2160"
| "4320"
| "max";
export async function askCobalt(
url: string,
options?: {
videoQuality?: VideoQuality;
},
retryWith?: number
) {
retryWith ??= 0;
try {
const controller = new AbortController();
setTimeout(() => controller.abort(), 10000);
const response = await fetch(
`${COBALT_INSTANCES[retryWith % COBALT_INSTANCES.length]}/`,
{
method: "POST",
body: JSON.stringify({ url, ...options, alwaysProxy: true }),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"User-Agent": USER_AGENT,
},
signal: controller.signal,
}
);
if (!response.ok) {
throw new Error(
`Cobalt instance ${
COBALT_INSTANCES[retryWith % COBALT_INSTANCES.length]
} threw an error: ${response.status} (${await response.text()})`
);
}
const data = await response.json();
return CobaltResult.parse(data);
} catch (error) {
if (retryWith < COBALT_INSTANCES.length - 1) {
console.warn(error, `, retrying with ${COBALT_INSTANCES[retryWith + 1]}`);
return askCobalt(url, options, retryWith + 1);
}
throw error;
}
}