JerryCoderr

SpotifyDownloader

Aug 14th, 2025 (edited)
22
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | Source Code | 0 0
  1. const { Module } = require('../main');
  2. const config = require('../config');
  3. const axios = require('axios');
  4. const fs = require('fs');
  5. const path = require('path');
  6.  
  7. Module({
  8. pattern: 'music ?(.*)',
  9. fromMe: config.MODE !== 'public',
  10. desc: 'Search a song and download as MP3',
  11. type: 'downloader'
  12. }, async (message, match) => {
  13. const query = match[1];
  14. if (!query) return await message.sendReply('_Please provide a song name!_');
  15.  
  16. try {
  17. // Step 1: Search
  18. const searchRes = await axios.get(`https://jerrycoder.oggyapi.workers.dev/yt-search?q=${encodeURIComponent(query)}`);
  19. if (!searchRes.data.status || !searchRes.data.result.length) {
  20. return await message.sendReply('_No results found!_');
  21. }
  22.  
  23. const song = searchRes.data.result[0];
  24.  
  25. // Step 2: Send "Found & Downloading" message
  26. const downloadMsg = await message.client.sendMessage(message.jid, {
  27. text: `_Found:_ *${song.title}*\n_Downloading..._`
  28. });
  29.  
  30. // Step 3: Download MP3
  31. const dlRes = await axios.get(`https://jerrycoder.oggyapi.workers.dev/ytmp3?url=${encodeURIComponent(song.link)}`);
  32. if (!dlRes.data.status || !dlRes.data.url) {
  33. return await message.sendReply('_Failed to download audio!_');
  34. }
  35.  
  36. const mp3Url = dlRes.data.url;
  37. const tempFile = path.join(__dirname, `${Date.now()}.mp3`);
  38.  
  39. const writer = fs.createWriteStream(tempFile);
  40. const audioStream = await axios.get(mp3Url, { responseType: 'stream' });
  41. audioStream.data.pipe(writer);
  42.  
  43. await new Promise((resolve, reject) => {
  44. writer.on('finish', resolve);
  45. writer.on('error', reject);
  46. });
  47.  
  48. // Step 4: Send MP3
  49. await message.client.sendMessage(message.jid, {
  50. audio: { stream: fs.createReadStream(tempFile) },
  51. mimetype: 'audio/mp4',
  52. fileName: `${song.title}.mp3`
  53. });
  54.  
  55. fs.unlinkSync(tempFile);
  56.  
  57. // Step 5: Delete the "Downloading" message
  58. await message.client.sendMessage(message.jid, { delete: downloadMsg.key });
  59.  
  60. } catch (err) {
  61. console.error(err);
  62. await message.sendReply('_Something went wrong while fetching the song!_');
  63. }
  64. });
Advertisement
Comments
Add Comment
Please, Sign In to add comment