descargas-spotify.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import axios from 'axios'
  2. import fetch from 'node-fetch'
  3. import search from 'yt-search'
  4. let handler = async (m, { conn, text, usedPrefix, command }) => {
  5. if (!text) throw `*¿Que esta buscando? ingresa el nombre para descargar sus música de Spotify, Ejemplo:* ${usedPrefix + command} ozuna`
  6. m.react(`⌛`)
  7. const spotify = await fetch(`${apis}/search/spotify?q=${text}`);
  8. const song = await spotify.json();
  9. if (!song.data || song.data.length === 0) throw '⚠️ No se encontraron resultados para esa búsqueda.';
  10. const track = song.data[0];
  11. let spotifyMessage = `*• Título:* ${track.title}\n*• Artista:* ${track.artist}\n*• Álbum:* ${track.album}\n*• Duración:* ${track.duration}\n*• Publicado:* ${track.publish}\n\n> 🚀 *ᴱⁿᵛᶦᵃⁿᵈᵒ ᶜᵃⁿᶜᶦᵒ́ⁿ ᵃᵍᵘᵃʳᵈᵉ ᵘⁿ ᵐᵒᵐᵉⁿᵗᵒ....*`;
  12. await conn.sendMessage(m.chat, {text: spotifyMessage,
  13. contextInfo: {
  14. forwardingScore: 1,
  15. isForwarded: true,
  16. externalAdReply: {
  17. showAdAttribution: true,
  18. containsAutoReply: true,
  19. renderLargerThumbnail: true,
  20. title: track.title,
  21. body: "ᴱⁿᵛᶦᵃⁿᵈᵒ ᶜᵃⁿᶜᶦᵒ́ⁿ ᵃᵍᵘᵃʳᵈᵉ ᵘⁿ ᵐᵒᵐᵉⁿᵗᵒ 🚀",
  22. mediaType: 1,
  23. thumbnailUrl: track.image,
  24. mediaUrl: track.url,
  25. sourceUrl: track.url
  26. }}}, { quoted: m });
  27. try {
  28. const res = await fetch(`https://api.siputzx.my.id/api/d/spotify?url=${track.url}`);
  29. const data = await res.json();
  30. conn.sendMessage(m.chat, {audio: { url: data.data.download }, fileName: `${track.title}.mp3`,mimetype: 'audio/mpeg'}, { quoted: m });
  31. m.react('✅️');
  32. } catch {
  33. try {
  34. const res = await fetch(`${apis}/download/spotifydl?url=${track.url}`);
  35. const data = await res.json();
  36. conn.sendMessage(m.chat, { audio: { url: data.data.url }, fileName: `${track.title}.mp3`, mimetype: 'audio/mpeg' }, { quoted: m });
  37. m.react('✅️');
  38. } catch (error) {
  39. m.reply(`\`\`\`⚠️ OCURRIO UN ERROR ⚠️\`\`\`\n\n> *Reporta el siguiente error a mi creador con el comando:* #report\n\n>>> ${error} <<<< `)
  40. console.log(error)
  41. m.react('❌')
  42. handler.limit = false
  43. }}}
  44. handler.help = ['spotify']
  45. handler.tags = ['downloader']
  46. handler.command = /^(spotify|music)$/i
  47. handler.register = true
  48. handler.limit = 1
  49. //handler.level = 2
  50. export default handler
  51. async function spotifyxv(query) {
  52. let token = await tokens();
  53. try {
  54. let response = await axios({
  55. method: 'get',
  56. url: 'https://api.spotify.com/v1/search?q=' + query + '&type=track',
  57. headers: {
  58. Authorization: 'Bearer ' + token,
  59. },
  60. });
  61. const tracks = response.data.tracks.items;
  62. const results = tracks.map((track) => ({
  63. name: track.name,
  64. artista: track.artists.map((artist) => artist.name),
  65. album: track.album.name,
  66. duracion: timestamp(track.duration_ms),
  67. url: track.external_urls.spotify,
  68. imagen: track.album.images.length ? track.album.images[0].url : '',
  69. }));
  70. return results;
  71. } catch (error) {
  72. console.error(`Error en spotifyxv: ${error}`);
  73. return [];
  74. }
  75. }
  76. async function tokens() {
  77. try {
  78. const response = await axios({
  79. method: 'post',
  80. url: 'https://accounts.spotify.com/api/token',
  81. headers: {
  82. 'Content-Type': 'application/x-www-form-urlencoded',
  83. Authorization: 'Basic ' + Buffer.from('acc6302297e040aeb6e4ac1fbdfd62c3:0e8439a1280a43aba9a5bc0a16f3f009').toString('base64'),
  84. },
  85. data: 'grant_type=client_credentials',
  86. });
  87. return response.data.access_token;
  88. } catch (error) {
  89. console.error(`Error en tokens: ${error}`);
  90. throw new Error('No se pudo obtener el token de acceso');
  91. }
  92. }
  93. function timestamp(time) {
  94. const minutes = Math.floor(time / 60000);
  95. const seconds = Math.floor((time % 60000) / 1000);
  96. return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
  97. }
  98. async function getBuffer(url, options) {
  99. try {
  100. options = options || {};
  101. const res = await axios({
  102. method: 'get',
  103. url,
  104. headers: {
  105. DNT: 1,
  106. 'Upgrade-Insecure-Request': 1,
  107. },
  108. ...options,
  109. responseType: 'arraybuffer',
  110. });
  111. return res.data;
  112. } catch (err) {
  113. return err;
  114. }}
  115. async function getTinyURL(text) {
  116. try {
  117. let response = await axios.get(`https://tinyurl.com/api-create.php?url=${text}`);
  118. return response.data;
  119. } catch (error) {
  120. return text;
  121. }}