Facebook
From your name, 2 Weeks ago, written in Bash.
Embed
Download Paste or View Raw
Hits: 183
  1. const axios = require("axios");
  2. const fs = require('fs');
  3.  
  4. function formatSize(bytes) {
  5.   const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  6.   if (bytes === 0) return '0 Byte';
  7.   const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  8.   return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
  9. }
  10.  
  11. module.exports = {
  12.   config: {
  13.     name: "play",
  14.     version: "1.0",
  15.     author: "Your Name",
  16.     countDown: 10,
  17.     role: 0,
  18.     shortDescription: "Get lyrics and audio from Spotify",
  19.     longDescription: "Get lyrics and audio from Spotify for a given song title",
  20.     category: "music",
  21.     guide: "{pn} <song title>"
  22.   },
  23.  
  24.   onStart: async function ({ api, event, args, message }) {
  25.     const query = args.join(" ");
  26.  
  27.     if (!query) {
  28.       return message.reply("Please provide a song title.");
  29.     }
  30.  
  31.     // Fetching lyrics
  32.     const lyricsApiUrl = `https://lyrist.vercel.app/api/${encodeURIComponent(query)}`;
  33.  
  34.     try {
  35.       const lyricsResponse = await axios.get(lyricsApiUrl);
  36.       const { lyrics, title, artist } = lyricsResponse.data;
  37.  
  38.       let combinedMessage = "";
  39.  
  40.       if (lyrics) {
  41.         combinedMessage += `? | Title: ${title}\n? | Artist: ${artist}\n\n${lyrics}\n\n`;
  42.       } else {
  43.         combinedMessage += `? | Sorry, lyrics for "${query}" not found!\n\n`;
  44.       }
  45.  
  46.       // Fetching Spotify audio
  47.       const searchApiUrl = `https://for-devs.onrender.com/api/spsearch?apikey=fuck&query;=${encodeURIComponent(query)}`;
  48.       const searchResponse = await axios.get(searchApiUrl);
  49.       const tracks = searchResponse.data.slice(0, 6);
  50.  
  51.       if (tracks.length === 0) {
  52.         combinedMessage += "❎ No tracks found on Spotify for the given query.";
  53.       } else {
  54.         const topTrack = tracks[0];
  55.         const dlApiUrl = `https://for-devs.onrender.com/api/spotifydl?apikey=fuck&url;=${encodeURIComponent(topTrack.url)}`;
  56.         const dlResponse = await axios.get(dlApiUrl);
  57.  
  58.         if (dlResponse.data.id) {
  59.           const { title, downloadUrl } = dlResponse.data;
  60.           const audioResponse = await axios.get(downloadUrl, { responseType: 'arraybuffer' });
  61.           fs.writeFileSync(__dirname + '/cache/spotifyAudio.mp3', Buffer.from(audioResponse.data));
  62.           const fileSize = fs.statSync(__dirname + '/cache/spotifyAudio.mp3').size;
  63.           const sizeFormatted = formatSize(fileSize);
  64.           combinedMessage += `? | Downloaded audio: ${title} (${sizeFormatted})`;
  65.           const attachment = fs.createReadStream(__dirname + '/cache/spotifyAudio.mp3');
  66.           const form = {
  67.             body: combinedMessage,
  68.             attachment: attachment
  69.           };
  70.           message.reply(form);
  71.         } else {
  72.           combinedMessage += "❎ Sorry, the Spotify content could not be downloaded.";
  73.           message.reply(combinedMessage);
  74.         }
  75.       }
  76.     } catch (error) {
  77.       console.error(error);
  78.       api.sendMessage("Error: " + error, event.threadID);
  79.     }
  80.   },
  81. };