forked from A-d-i-t-h-y-a-n/hermit-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.js
376 lines (338 loc) · 16.4 KB
/
youtube.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const {
Function,
addAudioMetaData,
isUrl,
getBuffer,
prefix,
isPublic,
ytIdRegex,
getJson,
sendwithLinkpreview,
toAudio,
h2k
} = require('../lib/');
const { downloadYouTubeVideo, downloadYouTubeAudio, mixAudioAndVideo, combineYouTubeVideoAndAudio, getYoutubeThumbnail, video, bytesToSize } = require('../lib/youtubei.js');
const { yta, ytv, filterLinks, searchYouTube, getThumb } = require('../lib/y2mate');
const yts = require("yt-search")
const config = require('../config');
const fs = require('fs');
const t = "```";
const send = async (message, file, id) => config.SONG_THUMBNAIL ? await sendwithLinkpreview(message.client, message, file, 'https://www.youtube.com/watch?v=' + id) : await message.client.sendMessage(message.chat, { audio: file, mimetype: 'audio/mpeg' }, { quoted: message.data });
Function({
on: 'text',
fromMe: isPublic,
}, async (message, match, client) => {
if (!message.reply_message.isBaileys) return;
if (!(message.reply_message && message.reply_message.text)) return;
const text = message.reply_message.text;
const index = message.text;
const ytRegex = /(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.|m\.)?youtube\.com\/(?:watch|v|embed|shorts)(?:\.php)?(?:\?.*v=|\/))([a-zA-Z0-9\_-]+)/gi
if (text.includes('Search results') && text.includes('Format: audio')) {
const urls = message.reply_message.text.match(ytRegex);
if (!urls) return await message.send('*The replied message does not contain any YouTube search results.*');
if (isNaN(index) || index < 1 || index > urls.length) return await message.send('*Invalid index.*\n_Please provide a number within the range of search results._');
let ytId = ytIdRegex.exec(urls[index - 1]);
try {
const media = await downloadYouTubeAudio(ytId[1]);
if (media.content_length >= 10485760) return await send(message, await fs.readFileSync(media.file), ytId[1]);
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync(media.file), 'mp4'), media.thumb, media.title, `hermit-md`, 'Hermit Official');
return await send(message, writer, ytId[1]);
} catch {
const response = await getJson('https://api.adithyan.xyz/ytaudio?id=' + ytId[1]);
if (!response.status) return await message.send('*Failed to download*');
if (response.content_length >= 10485760) return await client.sendMessage(message.jid, { audio: {url: response.result }, mimetype: 'audio/mpeg', ptt: false }, { quoted: message.data });
const buffer = await getBuffer(response.result);
await fs.writeFileSync('./' + response.file, buffer);
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync('./' + response.file), 'mp4'), response.thumb, response.title, `hermit-md`, 'Hermit Official');
return await send(message, writer, ytId[1]);
}
} else if (text.includes('Search results') && text.includes('Format: video')) {
const urls = message.reply_message.text.match(ytRegex);
if (!urls) return await message.send('*The replied message does not contain any YouTube search results.*');
if (isNaN(index) || index < 1 || index > urls.length) return await message.send('*Invalid index.*\n_Please provide a number within the range of search results._');
let id = ytIdRegex.exec(urls[index - 1]);
try {
const result = await video(id[1]);
if (!result) return await message.reply('_Failed to download_');
return await message.send(result.file, 'video', { quoted: message.data, caption: result.title });
} catch (error) {
await message.send('```' + error.message + '```')
}
} else if (text.includes('Available quality')) {
const id = text.match(/\*id:\s(.*?)\*/m)[1].trim();
const qualityMatches = Array.from(text.matchAll(/(\d+)\.\s(.*?)\s-\s([\d.]+)?\s?(\w{1,2})?/mg));
const qualityOptions = qualityMatches.map(match => ({
quality: match[2]
}));
if (isNaN(index) || index < 1 || index > qualityOptions.length) return await message.send('*Invalid number.*\n_Please provide a valid number from the available options._');
const { quality } = qualityOptions[index - 1]
const result = await getVideo('https://youtu.be/' + id, quality);
if (!result) return await message.reply('_Failed to download_');
return await message.send(result.url, 'video', { quoted: message.data, caption: result.title });
} else if (/\*⬡ ID :\* (\w+)/.test(text)) {
const id = text.match(/\*⬡ ID :\* ([\w-]+)/);
if (!id) return;
if (isNaN(index) || index < 1 || index > 2) return
if (index == '2') {
const result = await video(id[1]);
return await message.send(result.file, 'video', { quoted: message.data, caption: result.title });
} else if (index == '1') {
try {
const media = await downloadYouTubeAudio(id[1]);
if (media.content_length >= 10485760) return await send(message, await fs.readFileSync(media.file), id[1]);
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync(media.file), 'mp4'), media.thumb, media.title, `hermit-md`, 'Hermit Official');
return await send(message, writer, id[1]);
} catch {
const response = await getJson('https://api.adithyan.xyz/ytaudio?id=' + id[1]);
if (!response.status) return await message.send('*Failed to download*');
if (response.content_length >= 10485760) return await client.sendMessage(message.jid, { audio: {url: response.result }, mimetype: 'audio/mpeg', ptt: false }, { quoted: message.data });
const buffer = await getBuffer(response.result);
await fs.writeFileSync('./' + response.file, buffer);
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync('./' + response.file), 'mp4'), response.thumb, response.title, `hermit-md`, 'Hermit Official');
return await send(message, writer, id[1]);
}
}
} else if (text.includes('the desired ringtone number')) {
const urls = message.reply_message.text.match(/https?:\/\/btones\.b-cdn\.net\/[^ ]+\.mp3/g);
if (!urls) return
if (isNaN(index) || index < 1 || index > urls.length) return await message.send('*Invalid index.*\n_Please provide a number within the range of search results._');
await message.send(urls[index - 1], 'audio', { quoted: message.data, mimetype: 'audio/mpeg' });
}
});
Function({
pattern: 'play ?(.*)',
fromMe: isPublic,
desc: 'play youtube audio and video',
type: 'download'
}, async (message, match, client) => {
match = match || message.reply_message.text
if (!match) return await message.reply('*Need text!*\n_Example: .play astronaut in the ocean_');
const search = await yts(match)
const response = await searchYouTube(search.videos[0].url);
const data = filterLinks(response, { type: 'mp4' });
const mp3 = filterLinks(response, { type: 'mp3' });
const msg = `*${search.videos[0].title}*
*⬡ Duration :* ${search.videos[0].timestamp}
*⬡ Viewers :* ${h2k(search.videos[0].views)}
*⬡ Author :* ${search.videos[0].author.name}`
data.sort((a, b) => {
if (a.quality === 'auto') return 1;
if (b.quality === 'auto') return -1;
return parseInt(a.quality) - parseInt(b.quality);
});
const buttons = data.map(item => {
if (item.quality !== 'auto') {
return {
type: "button",
display_text: `${item.quality} - ${item.size}`,
id: `${prefix}ytv ${search.videos[0].url} q:${item.quality}`
};
}
return null;
}).filter(button => button !== null);
const mp3b = mp3.map(item => {
return {
type: "button",
display_text: `mp3 - ${item.size}`,
id: `${prefix}yta ${search.videos[0].url}`,
};
});
const interactiveMessage = {
title: msg,
text: "Please select the desired video quality:",
footer: "hermit-md",
subtitle: "Subtitle text",
buttons: [...mp3b, ...buttons],
image: { url: await getThumb(search.videos[0].videoId) }
};
await client.interactiveMessage(message.jid, interactiveMessage);
// await message.send(await getYoutubeThumbnail(search.videos[0].videoId), 'image', { caption: msg})
})
Function({
pattern: 'song ?(.*)',
fromMe: isPublic,
desc: 'Download and send a song from YouTube',
type: 'download'
}, async (message, match, client) => {
match = match || message.reply_message.text;
if (!match) return message.reply('Please provide a YouTube link or search query.');
if (isUrl(match) && match.includes('youtu')) {
let ytId = ytIdRegex.exec(match);
try {
const media = await downloadYouTubeAudio(ytId[1]);
if (media.content_length >= 10485760) return await send(message, await fs.readFileSync(media.file), ytId[1]);
const thumb = await getBuffer(await getYoutubeThumbnail(ytId[1]));
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync(media.file)), thumb, media.title, `${config.BOT_INFO.split(";")[0]}`, 'Hermit Official');
return await send(message, writer, ytId[1]);
} catch {
const response = await getJson('https://api.adithyan.xyz/ytaudio?id=' + ytId[1]);
if (!response.status) return await message.send('Failed to download audio.');
if (response.content_length >= 10485760) return await client.sendMessage(message.jid, { audio: { url: response.result }, mimetype: 'audio/mpeg', ptt: false }, { quoted: message.data });
const buffer = await getBuffer(response.result);
await fs.writeFileSync('./' + response.file, buffer);
const writer = await addAudioMetaData(await toAudio(await fs.readFileSync('./' + response.file), 'mp4'), response.thumb, response.title, 'hermit-md', 'Hermit Official');
return await send(message, writer, ytId[1]);
}
}
const search = await yts(match);
if (search.all.length < 1) return await message.reply('No results found.');
const buttons = search.all.filter(result => result.type === 'video').map((result, index) => ({
type: 'list',
title: result.title,
id: `${prefix}song ${result.url}`,
}));
await client.interactiveMessage(message.jid, {
title: search.videos[0].title,
footer: 'hermit-md',
subtitle: 'hermit-md',
text: `And ${search.all.length - 1} more results...`,
buttons: buttons
});
});
Function({
pattern: 'video ?(.*)',
fromMe: isPublic,
desc: 'Download and send a video from YouTube',
type: 'download'
}, async (message, match, client) => {
match = match || message.reply_message.text;
if (!match) return message.reply('Please provide a YouTube video URL or search query.');
if (isUrl(match) && match.includes('youtu')) {
const id = ytIdRegex.exec(match);
try {
const result = await video(id[1]);
if (!result) return await message.reply('Failed to download video.');
return await message.send(result.file, 'video', { quoted: message.data, caption: result.title });
} catch (error) {
return await message.send('```' + error.message + '```');
}
}
const search = await yts(match);
if (search.all.length < 1) return await message.reply('No results found.');
const buttons = search.all.filter(result => result.type === 'video').map((result, index) => ({
type: 'list',
title: result.title,
id: `${prefix}video ${result.url}`,
}));
await client.interactiveMessage(message.jid, {
title: search.videos[0].title,
text: `And ${search.all.length - 1} more results...`,
buttons: buttons
});
});
Function({
pattern: 'yta ?(.*)',
fromMe: isPublic,
desc: 'Download audios from YouTube',
type: 'download'
}, async (message, match, client) => {
match = match || message.reply_message.text;
if (!match) return message.reply('_Need URL or song name!_\n*Example: .yta URL/song name*');
try {
if (isUrl(match) && match.includes('youtu')) {
const ytId = ytIdRegex.exec(match);
const apiUrl = `https://api.adithyan.in.net/api/yta?url=https://youtu.be/${ytId[1]}`;
const result = await getJson(apiUrl);
const fileSizeInMB = parseFloat(result.size);
if (fileSizeInMB > 10) {
const audioBuffer = await getBuffer(result.url);
return await message.client.sendMessage(message.jid, {
audio: audioBuffer,
mimetype: 'audio/mpeg'
}, { quoted: message.data });
} else {
const thumb = await getBuffer(await getYoutubeThumbnail(ytId[1]));
const audioBuffer = await getBuffer(result.url);
const writer = await addAudioMetaData(
await toAudio(audioBuffer),
thumb,
result.title,
`${config.BOT_INFO.split(";")[0]}`,
'Hermit Official'
);
return await message.client.sendMessage(message.jid, {
audio: writer,
mimetype: 'audio/mpeg'
}, { quoted: message.data });
}
} else {
const search = await yts(match);
if (search.all.length < 1) return await message.reply('_Not Found_');
const videoUrl = search.videos[0].url;
const ytId = ytIdRegex.exec(videoUrl);
const apiUrl = `https://api.adithyan.in.net/api/yta?url=https://youtu.be/${ytId[1]}`;
const result = await getJson(apiUrl);
const fileSizeInMB = parseFloat(result.size);
if (fileSizeInMB > 10) {
const audioBuffer = await getBuffer(result.url);
return await message.client.sendMessage(message.jid, {
audio: audioBuffer,
mimetype: 'audio/mpeg'
}, { quoted: message.data });
} else {
const thumb = await getBuffer(await getYoutubeThumbnail(ytId[1]));
const audioBuffer = await getBuffer(result.url);
const file = await addAudioMetaData(
audioBuffer,
thumb,
result.title,
`${config.BOT_INFO.split(";")[0]}`,
'Hermit Official'
);
return await message.client.sendMessage(message.jid, {
audio: file,
mimetype: 'audio/mpeg'
}, { quoted: message.data });
}
}
} catch (error) {
console.error('Error:', error);
return message.reply(`Error: ${error.message || 'Unknown error occurred'}`);
}
});
Function({
pattern: 'ytv ?(.*)',
fromMe: isPublic,
desc: 'download videos from youtube',
type: 'download'
}, async (message, match, client) => {
match = match || message.reply_message.text
if (!match) return message.reply('_Need url or video name!_\n*Example: .ytv url/video name*')
try {
if (isUrl(match) && match.includes('youtu')) {
const apiUrl = `https://api.adithyan.in.net/api/ytv?url=${encodeURIComponent(match)}`;
const response = await getJson(apiUrl);
const result = response;
if (!result.status) {
return await message.reply('_Failed to download video_');
}
const qualities = result.availableQuality;
let msg = '*Available Qualities*\n\n';
qualities.forEach((quality, index) => {
msg += `${index + 1}. ${quality}p\n`;
});
if (result.url && result.filename) {
return await message.send(result.url, 'video', {
quoted: message.data,
caption: result.filename
});
}
} else {
const search = await yts(match)
if (search.all.length < 1) return await message.reply('_Not Found_');
const videoUrl = `https://youtu.be/${search.videos[0].videoId}`;
const apiUrl = `https://api.adithyan.in.net/api/ytv?url=${encodeURIComponent(videoUrl)}`;
const response = await getJson(apiUrl);
const result = response;
if (!result.status) {
return await message.reply('_Failed to download video_');
}
return await message.send(result.url, 'video', {
quoted: message.data,
caption: result.filename
});
}
} catch (error) {
return await message.send('```' + error.message + '```');
}
});