Skip to content

Commit

Permalink
Update yt.js
Browse files Browse the repository at this point in the history
  • Loading branch information
sataniceypz authored Jan 8, 2025
1 parent 863986f commit be71201
Showing 1 changed file with 34 additions and 85 deletions.
119 changes: 34 additions & 85 deletions plugins/yt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,30 @@ const fetch = require("node-fetch");
const yts = require("yt-search");

izumi({
pattern: "song ?(.*)",
pattern: "audio ?(.*)",
fromMe: mode,
desc: "Search and download audio from YouTube.",
type: "downloader",
}, async (message, match, client) => {
if (!match) {
return await message.reply("Please provide a search query or YouTube URL.");
}

try {
const { baseUrl, apiKey } = await getApiConfig();
let videoUrl, videoTitle;

if (match.startsWith("http")) {
videoUrl = match;
} else {
const { videos } = await yts(match);
if (!videos || videos.length === 0) {
return await message.reply("No results found for your search query.");
}

const firstVideo = videos[0];
videoUrl = firstVideo.url;
videoTitle = firstVideo.title;
}

const encodedUrl = encodeURIComponent(videoUrl);
const ytApi = `${baseUrl}api/download/yt?url=${encodedUrl}&apikey=${apiKey}`;
const response = await fetch(ytApi);
const data = await response.json();

const { result } = data;
if (!result || !result.mp4 || !result.title) {
return await message.reply("Failed to fetch audio details. Please try again.");
}

const mp4 = result.mp4;
const title = videoTitle || result.title;
await message.reply(`Downloading: ${title}...`);
await client.sendMessage(
message.jid,
{
audio: { url: mp4 },
mimetype: "audio/mpeg",
fileName: `${title}.mp3`,
},
{ quoted: message.data }
);
try {
const { videos } = await yts(match);
const firstVideo = videos[0];
url = firstVideo.url;
const api = `https://api.siputzx.my.id/api/d/ytmp4?url=${url}`;
const response = await getJson(api);
const data = response.data;
const dl = data.dl;
const title = data.title;
await m.reply(`_Downloading ${title}_`);
await message.client.sendMessage(message.jid, {
audio: { url: dl},
caption: title,
mimetype: "audio/mpeg",
},
{ quoted: message.data })
} catch (error) {
console.error("Error:", error);
await message.reply("An error occurred while processing your request. Please try again later.");
Expand Down Expand Up @@ -94,55 +70,28 @@ await message.client.sendMessage(message.jid, {
izumi({
pattern: "video ?(.*)",
fromMe: mode,
desc: "Search and download audio from YouTube.",
desc: "Search and download video from YouTube.",
type: "downloader",
}, async (message, match, client) => {
if (!match) {
return await message.reply("Please provide a search query or YouTube URL.");
}

try {
const { baseUrl, apiKey } = await getApiConfig();
let videoUrl, videoTitle;

if (match.startsWith("http")) {
videoUrl = match;
} else {
const { videos } = await yts(match);
if (!videos || videos.length === 0) {
return await message.reply("No results found for your search query.");
}

const firstVideo = videos[0];
videoUrl = firstVideo.url;
videoTitle = firstVideo.title;
}


const encodedUrl = encodeURIComponent(videoUrl);
const ytApi = `${baseUrl}api/download/yt?url=${encodedUrl}&apikey=${apiKey}`;
const response = await fetch(ytApi);
const data = await response.json();


const { result } = data;
if (!result || !result.mp4 || !result.title) {
return await message.reply("Failed to fetch audio details. Please try again.");
}

const mp4 = result.mp4;
const title = videoTitle || result.title;

await message.reply(`Downloading: ${title}...`);
await client.sendMessage(
message.jid,
{
video: { url: mp4 },
mimetype: "video/mp4",
fileName: `${title}.mp3`,
},
{ quoted: message.data }
);
try {
const { videos } = await yts(match);
const firstVideo = videos[0];
url = firstVideo.url;
const api = `https://api.siputzx.my.id/api/d/ytmp4?url=${url}`;
const response = await getJson(api);
const data = response.data;
const dl = data.dl;
const title = data.title;
await m.reply(`_Downloading ${title}_`);
await message.client.sendMessage(message.jid, {
video: { url: dl},
caption: title,
mimetype: "video/mp4",
},
{ quoted: message.data })
} catch (error) {
console.error("Error:", error);
await message.reply("An error occurred while processing your request. Please try again later.");
Expand Down

0 comments on commit be71201

Please sign in to comment.