Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/social-media/youtube.js
1126 views
1
import cheerio from 'cheerio';
2
import got from 'got';
3
import { ScraperError } from '../utils.js';
4
import { sizeFormatter } from 'human-readable';
5
const toFormat = sizeFormatter({
6
std: 'JEDEC',
7
decimalPlaces: 2,
8
keepTrailingZeroes: false,
9
render: (literal, symbol) => `${literal} ${symbol}B`
10
});
11
// https://github.com/BochilGaming/games-wabot/blob/main/lib/y2mate.js
12
const servers = ['en163', 'id90', 'en172'];
13
export async function youtubedl(url, server = 'en163') {
14
if (!servers.includes(server))
15
server = servers[0];
16
const params = {
17
url: url,
18
q_auto: 0,
19
ajax: 1
20
};
21
const json = await got
22
.post(`https://www.y2mate.com/mates/${server}/analyze/ajax`, {
23
headers: {
24
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
25
cookie: '_ga=GA1.2.1405332118.1641699259; _gid=GA1.2.70284915.1642387108; _gat_gtag_UA_84863187_23=1',
26
origin: 'https://www.y2mate.com'
27
},
28
form: params
29
})
30
.json();
31
const $ = cheerio.load(json.result);
32
const id = (/var k__id = "(.*?)"/.exec($.html()) || ['', ''])[1];
33
const v_id = (/var k_data_vid = "(.*?)"/.exec($.html()) || ['', ''])[1];
34
const thumbnail = $('.video-thumbnail > img').attr('src');
35
const title = $('div.caption > b').text().trim();
36
const video = {};
37
const audio = {};
38
$('#mp4 > table > tbody > tr').each(function () {
39
var _a, _b, _c;
40
const el = $(this).find('td');
41
const _quality = el.eq(0).find('a').text();
42
const quality = (_c = (_b = (_a = _quality.split('(')) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.trim()) === null || _c === void 0 ? void 0 : _c.toLowerCase();
43
const fileSizeH = el.eq(1).text();
44
if (!/\.3gp/i.test(_quality)) {
45
video[quality] = {
46
quality,
47
fileSizeH,
48
fileSize: parseFloat(fileSizeH) * (/MB$/.test(fileSizeH) ? 1000 : 1),
49
download: convert.bind(null, id, v_id, 'mp4', quality.replace(/p/i, ''))
50
};
51
}
52
});
53
$('#mp3 > table > tbody > tr').each(function () {
54
var _a, _b, _c, _d;
55
const el = $(this).find('td');
56
const _quality = el.eq(0).find('a').text();
57
const quality = (_d = (_c = (_b = (_a = _quality
58
.split('(')) === null || _a === void 0 ? void 0 : _a[1]) === null || _b === void 0 ? void 0 : _b.replace(')', '')) === null || _c === void 0 ? void 0 : _c.trim()) === null || _d === void 0 ? void 0 : _d.toLowerCase();
59
const fileSizeH = el.eq(1).text();
60
audio[quality] = {
61
quality,
62
fileSizeH,
63
fileSize: parseFloat(fileSizeH) * (/MB$/.test(fileSizeH) ? 1000 : 1),
64
download: convert.bind(null, id, v_id, 'mp3', quality.replace(/kbps/i, ''))
65
};
66
});
67
return {
68
id,
69
v_id,
70
thumbnail,
71
title,
72
video,
73
audio
74
};
75
}
76
export async function youtubedlv2(url) {
77
const html = await got('https://yt5s.com/en32').text();
78
const urlAjax = (/k_url_search="(.*?)"/.exec(html) || ['', ''])[1];
79
const urlConvert = (/k_url_convert="(.*?)"/.exec(html) || ['', ''])[1];
80
const params = {
81
q: url,
82
vt: 'home'
83
};
84
const json = await got(urlAjax, {
85
method: 'POST',
86
headers: {
87
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
88
cookie: '__cflb=04dToSoFRg9oqH9pYF2En9gKJK4fe8D9TcYtUD6tYu; _ga=GA1.2.1350132744.1641709803; _gid=GA1.2.1492233267.1641709803; _gat_gtag_UA_122831834_4=1',
89
origin: 'https://yt5s.com',
90
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
91
},
92
searchParams: new URLSearchParams(Object.entries(params))
93
}).json();
94
const video = {};
95
Object.values(json.links.mp4).forEach(({ k, size }) => {
96
video[k] = {
97
quality: k,
98
fileSizeH: size,
99
fileSize: parseFloat(size) * (/MB$/.test(size) ? 1000 : 1),
100
// @ts-ignore
101
download: convertv2.bind(null, urlConvert, json.vid, 'mp4', k, json.token, parseInt(json.timeExpires), json.fn)
102
};
103
});
104
const audio = {};
105
Object.values(json.links.mp3).forEach(({ key, size }) => {
106
audio[key] = {
107
quality: key,
108
fileSizeH: size,
109
fileSize: parseFloat(size) * (/MB$/.test(size) ? 1000 : 1),
110
// @ts-ignore
111
download: convertv2.bind(null, urlConvert, json.vid, 'mp3', key.replace(/kbps/i, ''), json.token, parseInt(json.timeExpires), json.fn)
112
};
113
});
114
return {
115
id: json.vid,
116
title: json.title,
117
thumbnail: `https://i.ytimg.com/vi/${json.vid}/0.jpg`,
118
video,
119
audio
120
};
121
}
122
export async function youtubedlv3(url) {
123
const payload = {
124
url
125
};
126
const { id, meta: { title }, thumb, url: results } = await got.post('https://api.onlinevideoconverter.pro/api/convert', {
127
headers: {
128
accept: 'application/json, text/plain, */*',
129
'accept-encoding': 'gzip, deflate, br',
130
'accept-language': 'en-US,en;q=0.9',
131
'content-type': 'application/json',
132
origin: 'https://onlinevideoconverter.pro',
133
referer: 'https://onlinevideoconverter.pro/',
134
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
135
},
136
body: JSON.stringify(payload)
137
}).json();
138
const video = {};
139
const audioArray = {};
140
results.forEach(({ url, info_url, attr, quality, audio, no_audio, filesize, ext }) => {
141
if (!no_audio && ext === 'mp4') {
142
video[quality] = {
143
quality,
144
fileSizeH: (filesize && toFormat(filesize)) || undefined,
145
fileSize: filesize,
146
download: async () => (url || info_url)
147
};
148
}
149
if (audio && !no_audio) {
150
audioArray[quality] = {
151
quality,
152
fileSizeH: (filesize && toFormat(filesize)) || undefined,
153
fileSize: filesize,
154
download: async () => (url || info_url)
155
};
156
}
157
});
158
return {
159
id,
160
title,
161
thumbnail: thumb,
162
video,
163
audio: audioArray
164
};
165
}
166
async function convert(_id, v_id, ftype, fquality) {
167
const params = {
168
type: 'youtube',
169
_id,
170
v_id,
171
ajax: '1',
172
token: '',
173
ftype,
174
fquality
175
};
176
const json = await got('https://www.y2mate.com/mates/convert', {
177
method: 'POST',
178
headers: {
179
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
180
cookie: '_ga=GA1.2.1405332118.1641699259; _gid=GA1.2.1117783105.1641699259; MarketGidStorage=%7B%220%22%3A%7B%7D%2C%22C702514%22%3A%7B%22page%22%3A2%2C%22time%22%3A1641701743540%7D%7D; _PN_SBSCRBR_FALLBACK_DENIED=1641701744162',
181
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
182
},
183
form: params
184
}).json();
185
const $ = cheerio.load(json.result);
186
const link = $('a[href]').attr('href');
187
if (link === 'https://app.y2mate.com/download')
188
throw new ScraperError(JSON.stringify({ link, json: json }, null, 2));
189
return link;
190
}
191
function convertv2(url, v_id, ftype, fquality, token, timeExpire, fname) {
192
return new Promise(async (resolve, reject) => {
193
const params = {
194
v_id,
195
ftype,
196
fquality,
197
token,
198
timeExpire,
199
client: 'yt5s.com'
200
};
201
const resServer = await got(url, {
202
method: 'POST',
203
headers: {
204
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
205
origin: 'https://yt5s.com',
206
referer: 'https://yt5s.com/',
207
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
208
'X-Requested-Key': 'de0cfuirtgf67a'
209
},
210
form: params
211
}).json();
212
const server = resServer.c_server;
213
if (!server && ftype === 'mp3')
214
return resolve(server || resServer.d_url || '');
215
const payload = {
216
v_id,
217
ftype,
218
fquality,
219
fname,
220
token,
221
timeExpire
222
};
223
const results = await got(`${server}/api/json/convert`, {
224
method: 'POST',
225
form: payload
226
}).json();
227
if (results.statusCode === 200)
228
return resolve(results.result);
229
else if (results.statusCode === 300) {
230
try {
231
// @ts-ignore
232
const WebSocket = (await import('ws')).default;
233
const Url = new URL(server);
234
const WSUrl = `${/https/i.test(Url.protocol) ? 'wss:' : 'ws:'}//${Url.host}/sub/${results.jobId}?fname=yt5s.com`;
235
const ws = new WebSocket(WSUrl, undefined, {
236
headers: {
237
'Accept-Encoding': 'gzip, deflate, br',
238
Host: Url.host,
239
Origin: 'https://yt5s.com',
240
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
241
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
242
}
243
});
244
ws.on('message', function incoming(message) {
245
const msg = JSON.parse(message.toString());
246
if (msg.action === 'success') {
247
try {
248
ws.close();
249
}
250
catch (e) {
251
console.error(e);
252
}
253
ws.removeAllListeners('message');
254
return resolve(msg.url);
255
}
256
else if (msg.action === 'error')
257
return reject(msg);
258
});
259
}
260
catch (e) {
261
console.error(e);
262
return reject(e);
263
}
264
}
265
else
266
return reject(results);
267
});
268
}
269
//# sourceMappingURL=youtube.js.map
270