Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ytdl.js #60

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions assets/plugins/ytdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
getJson,
validateQuality,
} = require("../../lib");
const { yta, ytv, ytsdl } = require("../../lib/ytdl");
const { yta, ytv } = require("../../lib/ytdl");

command(
{
Expand Down Expand Up @@ -87,25 +87,30 @@ command(
{
pattern: "song",
fromMe: isPrivate,
desc: "Download audio from youtube",
},
async (message, match) => {
match = match || message.reply_message.text;
if (!match) return await message.reply("Give me a query");
let { dlink, title } = await ytsdl(match);
await message.reply(`_Downloading ${title}_`);
let buff = await getBuffer(dlink);
return await message.sendMessage(
message.jid,
buff,
{
mimetype: "audio/mpeg",
filename: title + ".mp3",
},
"audio"
);
}
);
desc: "Download audio from Tubidy",
}, async (message, match) => {
match = match || message.reply_message.text;
if (!match) return await message.reply("Give me a query");
let find = `https://diegoson-naxordeve.hf.space/tubidy/search?q=${match}`;
let search = await fetch(find);
let dlink = await search.json();
if (!dlink || !dlink.length) return;
let toBuffer = dlink[0];
if (!toBuffer.link) return;
await message.reply(`_Downloading ${toBuffer.title}_`);
let toBuffu = `https://diegoson-naxordeve.hf.space/tubidy/dl?url=${toBuffer.link}`;
let get = await fetch(toBuffu);
let toAudio = await get.json();
if (!toAudio.media || !toAudio.media.length) return;
let buff = toAudio.media.find(m => m.type === 'download')?.link;
if (!buff) return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Maximum effort needed here! 💪 Your error handling is missing in action!

Just like my healing factor, your code needs some regeneration capabilities! You're making another fetch call without any protection!

Let's wrap this bad boy in a try-catch:

-        let toBuffu = `https://diegoson-naxordeve.hf.space/tubidy/dl?url=${toBuffer.link}`;
-        let get = await fetch(toBuffu);
-        let toAudio = await get.json();
-        if (!toAudio.media || !toAudio.media.length) return;
-        let buff = toAudio.media.find(m => m.type === 'download')?.link;
-        if (!buff) return;
+        try {
+            const toBuffu = `https://diegoson-naxordeve.hf.space/tubidy/dl?url=${encodeURIComponent(toBuffer.link)}`;
+            const get = await fetch(toBuffu);
+            if (!get.ok) throw new Error(`HTTP error! status: ${get.status}`);
+            const toAudio = await get.json();
+            if (!toAudio.media || !toAudio.media.length) {
+                return await message.reply("No audio found! Did someone steal it? 🤔");
+            }
+            const buff = toAudio.media.find(m => m.type === 'download')?.link;
+            if (!buff) {
+                return await message.reply("Found the audio but can't get the download link! Talk about a tease! 😅");
+            }
+        } catch (error) {
+            return await message.reply(`Holy macaroni! Something went wrong: ${error.message}`);
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let toBuffu = `https://diegoson-naxordeve.hf.space/tubidy/dl?url=${toBuffer.link}`;
let get = await fetch(toBuffu);
let toAudio = await get.json();
if (!toAudio.media || !toAudio.media.length) return;
let buff = toAudio.media.find(m => m.type === 'download')?.link;
if (!buff) return;
try {
const toBuffu = `https://diegoson-naxordeve.hf.space/tubidy/dl?url=${encodeURIComponent(toBuffer.link)}`;
const get = await fetch(toBuffu);
if (!get.ok) throw new Error(`HTTP error! status: ${get.status}`);
const toAudio = await get.json();
if (!toAudio.media || !toAudio.media.length) {
return await message.reply("No audio found! Did someone steal it? 🤔");
}
const buff = toAudio.media.find(m => m.type === 'download')?.link;
if (!buff) {
return await message.reply("Found the audio but can't get the download link! Talk about a tease! 😅");
}
} catch (error) {
return await message.reply(`Holy macaroni! Something went wrong: ${error.message}`);
}

return await message.sendMessage(message.jid, {
audio: { url: buff},
mimetype: 'audio/mpeg',
});
});



command(
{
Expand Down