Create youtube.js
Browse files- lib/youtube/youtube.js +89 -0
lib/youtube/youtube.js
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import got from 'got';
|
2 |
+
import {
|
3 |
+
ConvertResponseSchema,
|
4 |
+
LinkItem,
|
5 |
+
YoutubedlResponseSchema,
|
6 |
+
} from '../types/youtubedl-v1.js';
|
7 |
+
import {
|
8 |
+
YoutubeDownloaderArgsSchema,
|
9 |
+
YoutubeDownloaderArgs,
|
10 |
+
YoutubedlSchema,
|
11 |
+
Youtubedl
|
12 |
+
} from '../types/youtube-v1.js';
|
13 |
+
import { DEFAULT_HEADERS } from '../constant.js';
|
14 |
+
import { parseFileSize } from './util.js';
|
15 |
+
|
16 |
+
export async function youtubedl(url, server) {
|
17 |
+
YoutubeDownloaderArgsSchema.parse(arguments);
|
18 |
+
|
19 |
+
const form = {
|
20 |
+
k_query: url,
|
21 |
+
k_page: 'home',
|
22 |
+
hl: server || 'en',
|
23 |
+
q_auto: 0
|
24 |
+
};
|
25 |
+
|
26 |
+
const data = await got.post('https://www.y2mate.com/mates/analyzeV2/ajax', {
|
27 |
+
headers: {
|
28 |
+
...DEFAULT_HEADERS,
|
29 |
+
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
30 |
+
cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0',
|
31 |
+
origin: 'https://www.y2mate.com'
|
32 |
+
},
|
33 |
+
form
|
34 |
+
}).json();
|
35 |
+
|
36 |
+
const json = YoutubedlResponseSchema.parse(data);
|
37 |
+
const video = {};
|
38 |
+
const audio = {};
|
39 |
+
const other = {};
|
40 |
+
|
41 |
+
for (const key in json.links) {
|
42 |
+
for (const tag in json.links[key]) {
|
43 |
+
const data = json.links[key][tag];
|
44 |
+
const quality = data.q;
|
45 |
+
const type = data.f;
|
46 |
+
const fileSizeH = data.size;
|
47 |
+
const fileSize = parseFileSize(fileSizeH);
|
48 |
+
(type === 'mp4' ? video : type === 'mp3' ? audio : other)[quality.toLowerCase()] = {
|
49 |
+
quality,
|
50 |
+
type,
|
51 |
+
fileSizeH,
|
52 |
+
fileSize,
|
53 |
+
download: convert.bind(convert, json.vid, data.k)
|
54 |
+
};
|
55 |
+
}
|
56 |
+
}
|
57 |
+
|
58 |
+
const result = {
|
59 |
+
id: json.vid,
|
60 |
+
thumbnail: `https://i.ytimg.com/vi/${json.vid}/0.jpg`,
|
61 |
+
title: json.title,
|
62 |
+
duration: json.t,
|
63 |
+
video,
|
64 |
+
audio,
|
65 |
+
other
|
66 |
+
};
|
67 |
+
|
68 |
+
return YoutubedlSchema.parse(result);
|
69 |
+
}
|
70 |
+
|
71 |
+
export async function convert(vid, k) {
|
72 |
+
const form = {
|
73 |
+
vid,
|
74 |
+
k
|
75 |
+
};
|
76 |
+
|
77 |
+
const data = await got.post('https://www.y2mate.com/mates/convertV2/index', {
|
78 |
+
headers: {
|
79 |
+
...DEFAULT_HEADERS,
|
80 |
+
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
81 |
+
cookie: '_ga=GA1.1.1058493269.1720585210; _ga_PSRPB96YVC=GS1.1.1720585209.1.1.1720585486.0.0.0',
|
82 |
+
origin: 'https://www.y2mate.com'
|
83 |
+
},
|
84 |
+
form
|
85 |
+
}).json();
|
86 |
+
|
87 |
+
const json = ConvertResponseSchema.parse(data);
|
88 |
+
return json.dlink;
|
89 |
+
}
|