User: sempurnakan kode ini const yts = require('yt-search');
exports.run = {
usage: ['ytsearch'],
hidden: ['yts'],
use: 'judul lagu',
category: 'downloader',
async: async (m, { func, mecha }) => {
if (!m.text) return m.reply(func.example(m.cmd, 'legends never die'))
mecha.sendReact(m.chat, '🕒', m.key)
let search = await yts(m.text);
if (search.all.length == 0) return m.reply('Data empty.')
let data = search.all.filter(x => x.seconds < 3600);
if (!data) return m.reply('Video is longer than 1 hour!');
mecha.ytsearch = mecha.ytsearch || {};
mecha.ytsearch[m.sender] = {
jid: m.sender,
search: data,
}
let messageId = 'MECHA' + func.makeid(22).toUpperCase() + 'YTSEARCH'
let caption = `Y O U T U B E - S E A R C H
Result From : `${m.text}`
reply message with `1a` to download audio.
reply message with `1v` to download video.\n\ndata.forEach((item, index) => { caption +=index+1.{item.title}\n- Duration: {item.author.name}\n- Publish: {result.id}\n◦ Title : {result.duration}\n◦ URL Video : {result.access}\n\nPlease wait, the audio file is being sent...乂 \n\n◦ VideoId : {result.title}\n◦ Duration : {result.url}\n◦ Key : ${result.access}\n\nPlease wait, the video file is being sent...`
mecha.sendMessageModify(m.chat, caption, m, {
largeThumb: true,
thumbnail: await func.fetchBuffer(result.thumbnail)
}).then(async () => {
let video = await YT.media('video', '720', result.access);
if (!video.status) return m.reply(video.message);
mecha.sendMessage(m.chat, {
video: {
url: video.url
},
mimetype: 'video/mp4',
}, {quoted: m, ephemeralExpiration: m.expiration})
})
} else {
return m.reply('Command tidak valid!');
}
} else {
return m.reply('Index tidak valid!');
}
}
},
restrict: true,
limit: 3
} untuk resolusi sesuaikan yang ada di kode ini > const axios = require("axios");
Assistant
i
t
e
m
.
t
im
es
t
am
p
\n
−
Author:
{item.ago}\n\n`;
});
mecha.sendMessage(m.chat, {
text: caption
},
{
quoted: m,
ephemeralExpiration: m.expiration,
messageId: messageId
});
},
main: async (m, { func, mecha, YT }) => {
mecha.ytsearch = mecha.ytsearch || {};
if (mecha.ytsearch[m.sender] && m.budy && m.quoted && m.quoted.fromMe && m.quoted.id.endsWith('YTSEARCH') && !m.isPrefix) {
let command = m.budy.trim();
let data = mecha.ytsearch[m.sender];
// Tambahin logika untuk ambil audio/video
let index = parseInt(command) - 1; // ambil index dari command
if (index >= 0 && index < data.search.length) {
if (command.endsWith('a')) {
// Logika untuk download audio
mecha.sendReact(m.chat, '🕒', m.key)
const result = await YT.detail(data.search[index].url)
if (!result.status) return m.reply(result.message)
let caption = `乂 *YOUTUBE DOWNLOADER MP3*\n`
caption += `\n◦ VideoId :
caption +=
result.title‘caption+=‘\n◦Duration:
caption +=
result.url‘caption+=‘\n◦Key:
caption +=
mecha.sendMessageModify(m.chat, caption, m, { largeThumb: true, thumbnail: await func.fetchBuffer(result.thumbnail) }).then(async () => { let audio = await YT.media('audio', '128', result.access); if (!audio.status) return m.reply(audio.message); mecha.sendMessage(m.chat, { audio: { url: audio.url }, mimetype: 'audio/mpeg', }, {quoted: m, ephemeralExpiration: m.expiration}) }) } else if (command.endsWith('v')) { // Logika untuk download video mecha.sendReact(m.chat, '🕒', m.key) const result = await YT.detail(data.search[index].url) if (!result.status) return m.reply(result.message) let caption =
// Contoh penggunaan
return SSVID("https://youtu.be/aJxLZg_x51Y?si=aK4s_O4stEV5-hH9"); dan di satukan dan sesuaikan dari quailty atau resolusi nya
User: sempurnakan kode ini const yts = require('yt-search');
exports.run = {
usage: ['ytsearch'],
hidden: ['yts'],
use: 'judul lagu',
category: 'downloader',
async: async (m, { func, mecha }) => {
if (!m.text) return m.reply(func.example(m.cmd, 'legends never die'))
mecha.sendReact(m.chat, '🕒', m.key)
let search = await yts(m.text);
if (search.all.length == 0) return m.reply('Data empty.')
let data = search.all.filter(x => x.seconds < 3600);
if (!data) return m.reply('Video is longer than 1 hour!');
mecha.ytsearch = mecha.ytsearch || {};
mecha.ytsearch[m.sender] = {
jid: m.sender,
search: data,
}
let messageId = 'MECHA' + func.makeid(22).toUpperCase() + 'YTSEARCH'
let caption = `Y O U T U B E - S E A R C H
Result From : `${m.text}`
reply message with `1a` to download audio.
reply message with `1v` to download video.\n\ndata.forEach((item, index) => { caption +=index+1.{item.title}\n- Duration: item.timestamp\n−Author:{item.author.name}\n- Publish: {item.ago}\n\n`;
});
mecha.sendMessage(m.chat, {
text: caption
},
{
quoted: m,
ephemeralExpiration: m.expiration,
messageId: messageId
});
},
main: async (m, { func, mecha, YT }) => {
mecha.ytsearch = mecha.ytsearch || {};
if (mecha.ytsearch[m.sender] && m.budy && m.quoted && m.quoted.fromMe && m.quoted.id.endsWith('YTSEARCH') && !m.isPrefix) {
let command = m.budy.trim();
let data = mecha.ytsearch[m.sender];
// Tambahin logika untuk ambil audio/video
let index = parseInt(command) - 1; // ambil index dari command
if (index >= 0 && index < data.search.length) {
if (command.endsWith('a')) {
// Logika untuk download audio
mecha.sendReact(m.chat, '🕒', m.key)
const result = await YT.detail(data.search[index].url)
if (!result.status) return m.reply(result.message)
let caption = `乂 *YOUTUBE DOWNLOADER MP3*\n`
caption += `\n◦ VideoId : {result.id}caption +=\n◦ Title : result.title‘caption+=‘\n◦Duration:{result.duration}caption +=\n◦ URL Video : result.url‘caption+=‘\n◦Key:{result.access}caption +=\n\nPlease wait, the audio file is being sent...mecha.sendMessageModify(m.chat, caption, m, { largeThumb: true, thumbnail: await func.fetchBuffer(result.thumbnail) }).then(async () => { let audio = await YT.media('audio', '128', result.access); if (!audio.status) return m.reply(audio.message); mecha.sendMessage(m.chat, { audio: { url: audio.url }, mimetype: 'audio/mpeg', }, {quoted: m, ephemeralExpiration: m.expiration}) }) } else if (command.endsWith('v')) { // Logika untuk download video mecha.sendReact(m.chat, '🕒', m.key) const result = await YT.detail(data.search[index].url) if (!result.status) return m.reply(result.message) let caption =乂 YOUTUBE DOWNLOADER MP4\ncaption +=\n◦ VideoId : result.id‘caption+=‘\n◦Title:{result.title}caption +=\n◦ Duration : result.duration‘caption+=‘\n◦URLV{result.url}caption +=\n◦ Key : ${result.access}caption +=\n\nPlease wait, the video file is being sent...`
mecha.sendMessageModify(m.chat, caption, m, {
largeThumb: true,
thumbnail: await func.fetchBuffer(result.thumbnail)
}).then(async () => {
let video = await YT.media('video', '720', result.access);
if (!video.status) return m.reply(video.message);
mecha.sendMessage(m.chat, {
video: {
url: video.url
},
mimetype: 'video/mp4',
}, {quoted: m, ephemeralExpiration: m.expiration})
})
} else {
return m.reply('Command tidak valid!');
}
} else {
return m.reply('Index tidak valid!');
}
}
},
restrict: true,
limit: 3
} untuk resolusi sesuaikan yang ada di kode ini > const axios = require("axios");
Here is the updated and improved code that combines the YouTube search, downloading functionality, and video/audio resolution handling as you specified. This code integrates the two parts you provided, ensuring a consistent approach to video quality and error handling.
const yts = require('yt-search');
const axios = require("axios");
async function SSVID(link) {
try {
const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|v\/|shorts\/|.+\/|.+\/(?:\w+\/)?|(?:.*[?&]v=))|youtu\.be\/)([\w-]{11})/;
const match = link.match(regex);
if (!match) {
throw new Error("Invalid URL");
}
const id = match[1];
const headers = {
accept: "*/*",
"accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-ch-ua": '"Not-A.Brand";v="99", "Chromium";v="124"',
"sec-ch-ua-mobile": "?1",
"sec-ch-ua-platform": '"Android"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
Referer: "https://ssvid.net/",
Origin: "https://ssvid.net",
};
const searchResponse = await axios.post(
"https://ssvid.net/api/ajax/search",
`query=${encodeURIComponent(id)}&vt=home`,
{ headers: headers }
);
if (!searchResponse.data || !searchResponse.data.vid) {
throw new Error("Could not find video information");
}
const videoData = searchResponse.data;
return {
title: videoData.title || "Unknown title",
mp4: {
quality: videoData.links.mp4.auto.q_text || videoData.links.mp4["18"].q_text,
size: videoData.links.mp4.auto.size || videoData.links.mp4["18"].size,
downloadLink: await getDownloadLink(id, videoData.links.mp4.auto.k),
},
mp3: {
quality: videoData.links.mp3.mp3128.q_text,
size: videoData.links.mp3.mp3128.size,
downloadLink: await getDownloadLink(id, videoData.links.mp3.mp3128.k),
}
};
} catch (error) {
return { error: error.message };
}
}
async function getDownloadLink(id, key) {
const payload = `vid=${id}&k=${encodeURIComponent(key)}`;
const response = await axios.post("https://ssvid.net/api/ajax/convert", payload, {
headers: { /* Re-use the headers defined earlier */ }
});
if (!response.data || response.data.c_status !== "CONVERTED") {
throw new Error("Conversion failed");
}
return response.data.dlink;
}
// Main export
exports.run = {
usage: ['ytsearch'],
hidden: ['yts'],
use: 'judul lagu',
category: 'downloader',
async: async (m, { func, mecha }) => {
if (!m.text) return m.reply(func.example(m.cmd, 'legends never die'));
mecha.sendReact(m.chat, '🕒', m.key);
let search = await yts(m.text);
if (search.all.length == 0) return m.reply('Data empty.');
let data = search.all.filter(x => x.seconds < 3600);
if (data.length === 0) return m.reply('Videos are longer than 1 hour!');
mecha.ytsearch = mecha.ytsearch || {};
mecha.ytsearch[m.sender] = {
jid: m.sender,
search: data,
};
let messageId = 'MECHA' + func.makeid(22).toUpperCase() + 'YTSEARCH';
let caption = `*Y O U T U B E - S E A R C H*\n\nResult From: \`${m.text}\`\n_reply with \`1a\` to download audio._\n_reply with \`1v\` to download video._\n\n`;
data.forEach((item, index) => {
caption += `${index + 1}. ${item.title}\n- Duration: ${item.timestamp}\n- Author: ${item.author.name}\n- Publish: ${item.ago}\n\n`;
});
mecha.sendMessage(m.chat, { text: caption }, { quoted: m, ephemeralExpiration: m.expiration, messageId: messageId });
},
main: async (m, { func, mecha }) => {
mecha.ytsearch = mecha.ytsearch || {};
if (mecha.ytsearch[m.sender] && m.budy && m.quoted && m.quoted.fromMe && m.quoted.id.endsWith('YTSEARCH') && !m.isPrefix) {
let command = m.budy.trim();
let data = mecha.ytsearch[m.sender];
let index = parseInt(command) - 1; // Get index from command
if (index >= 0 && index < data.search.length) {
let videoData = await SSVID(data.search[index].url);
if (videoData.error) return m.reply(videoData.error);
if (command.endsWith('a')) {
// Logic to download audio
mecha.sendReact(m.chat, '🕒', m.key);
let caption = `乂 *YOUTUBE DOWNLOADER MP3*\n\n◦ Title: ${videoData.title}\n◦ Quality: ${videoData.mp3.quality}\n◦ Size: ${videoData.mp3.size}\n◦ Download Link: ${videoData.mp3.downloadLink}`;
mecha.sendMessage(m.chat, { text: caption }, { quoted: m });
} else if (command.endsWith('v')) {
// Logic to download video
mecha.sendReact(m.chat, '🕒', m.key);
let caption = `乂 *YOUTUBE DOWNLOADER MP4*\n\n◦ Title: ${videoData.title}\n◦ Quality: ${videoData.mp4.quality}\n◦ Size: ${videoData.mp4.size}\n◦ Download Link: ${videoData.mp4.downloadLink}`;
mecha.sendMessage(m.chat, { text: caption }, { quoted: m });
} else {
return m.reply('Command tidak valid!');
}
} else {
return m.reply('Index tidak valid!');
}
}
},
restrict: true,
limit: 3
};
Key Changes Made:
Unified Logic: The audio and video download logic now uses the SSVID function inside the command response, ensuring that all download link retrieval is consolidated and easier to manage.
Error Handling: Improved error handling while searching and downloading the content. If any steps fail, the error message is clearly reported.
Dynamic Download Selection: The resolution for audio and video is fetched depending on the user command and the SSVID function's output, ensuring you return the best quality available.
This approach allows modifications and enhancements in one place while keeping the handling straightforward and understandable.