Upload spotify (1).js
Browse files- lib/spotify (1).js +144 -0
lib/spotify (1).js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require("axios");
|
| 2 |
+
|
| 3 |
+
const client_id = "6dc6b46d39c24a078ff5946b1295bd81";
|
| 4 |
+
const client_secret = "f30d47e4b2ed46ec82ddc8e861e6cc99";
|
| 5 |
+
const basic = Buffer.from(`${client_id}:${client_secret}`).toString("base64");
|
| 6 |
+
const TOKEN_ENDPOINT = "https://accounts.spotify.com/api/token";
|
| 7 |
+
|
| 8 |
+
async function spotifyCreds() {
|
| 9 |
+
try {
|
| 10 |
+
const response = await axios.post(
|
| 11 |
+
TOKEN_ENDPOINT,
|
| 12 |
+
"grant_type=client_credentials", {
|
| 13 |
+
headers: {
|
| 14 |
+
Authorization: "Basic " + basic,
|
| 15 |
+
},
|
| 16 |
+
},
|
| 17 |
+
);
|
| 18 |
+
return {
|
| 19 |
+
status: true,
|
| 20 |
+
data: response.data,
|
| 21 |
+
};
|
| 22 |
+
} catch (error) {
|
| 23 |
+
return {
|
| 24 |
+
status: false,
|
| 25 |
+
msg: "Failed to retrieve Spotify credentials.",
|
| 26 |
+
};
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const toTime = (ms) => {
|
| 31 |
+
let h = Math.floor(ms / 3600000);
|
| 32 |
+
let m = Math.floor(ms / 60000) % 60;
|
| 33 |
+
let s = Math.floor(ms / 1000) % 60;
|
| 34 |
+
return [h, m, s].map((v) => v.toString().padStart(2, "0")).join(":");
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
class Spotify {
|
| 38 |
+
search = async (query, type = "track", limit = 20) => {
|
| 39 |
+
try {
|
| 40 |
+
const creds = await spotifyCreds();
|
| 41 |
+
if (!creds.status) return creds;
|
| 42 |
+
|
| 43 |
+
const response = await axios.get(
|
| 44 |
+
`https://api.spotify.com/v1/search?query=${encodeURIComponent(query)}&type=${type}&offset=0&limit=${limit}`, {
|
| 45 |
+
headers: {
|
| 46 |
+
Authorization: "Bearer " + creds.data.access_token,
|
| 47 |
+
},
|
| 48 |
+
},
|
| 49 |
+
);
|
| 50 |
+
|
| 51 |
+
if (
|
| 52 |
+
!response.data[type + "s"] ||
|
| 53 |
+
!response.data[type + "s"].items.length
|
| 54 |
+
) {
|
| 55 |
+
return {
|
| 56 |
+
msg: "Music not found!",
|
| 57 |
+
};
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return response.data[type + "s"].items.map((item) => ({
|
| 61 |
+
title: item.name,
|
| 62 |
+
id: item.id,
|
| 63 |
+
duration: toTime(item.duration_ms),
|
| 64 |
+
artist: item.artists.map((artist) => artist.name).join(" & "),
|
| 65 |
+
url: item.external_urls.spotify,
|
| 66 |
+
thumbnail: item.album.images[0]?.url,
|
| 67 |
+
//...item
|
| 68 |
+
}));
|
| 69 |
+
} catch (error) {
|
| 70 |
+
return {
|
| 71 |
+
status: false,
|
| 72 |
+
msg: "Error searching for music. " + error.message,
|
| 73 |
+
};
|
| 74 |
+
}
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
download = async (url) => {
|
| 78 |
+
const BASEURL = "https://api.fabdl.com";
|
| 79 |
+
const headers = {
|
| 80 |
+
Accept: "application/json, text/plain, */*",
|
| 81 |
+
"Content-Type": "application/json",
|
| 82 |
+
"User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36",
|
| 83 |
+
};
|
| 84 |
+
|
| 85 |
+
try {
|
| 86 |
+
const {
|
| 87 |
+
data: info
|
| 88 |
+
} = await axios.get(`${BASEURL}/spotify/get?url=${url}`, {
|
| 89 |
+
headers
|
| 90 |
+
});
|
| 91 |
+
const {
|
| 92 |
+
gid,
|
| 93 |
+
id,
|
| 94 |
+
name,
|
| 95 |
+
image,
|
| 96 |
+
duration_ms
|
| 97 |
+
} = info.result;
|
| 98 |
+
|
| 99 |
+
const {
|
| 100 |
+
data: download
|
| 101 |
+
} = await axios.get(`${BASEURL}/spotify/mp3-convert-task/${gid}/${id}`, {
|
| 102 |
+
headers
|
| 103 |
+
});
|
| 104 |
+
if (download.result.download_url) {
|
| 105 |
+
return {
|
| 106 |
+
title: name,
|
| 107 |
+
duration: duration_ms,
|
| 108 |
+
cover: image,
|
| 109 |
+
download: `${BASEURL}${download.result.download_url}`
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
} catch (error) {
|
| 113 |
+
console.error("Error downloading Spotify track:", error.message);
|
| 114 |
+
throw new Error(error.message);
|
| 115 |
+
}
|
| 116 |
+
};
|
| 117 |
+
playlist = async function playlist(url) {
|
| 118 |
+
try {
|
| 119 |
+
const id = url.split("/playlist/")[0].trim().split("?si=")[0].trim();
|
| 120 |
+
const {
|
| 121 |
+
data
|
| 122 |
+
} = await axios.get(
|
| 123 |
+
"https://api.spotifydown.com/trackList/playlist/0F1VNeiZKjtX06yrVSqlbu", {
|
| 124 |
+
headers: {
|
| 125 |
+
Accept: "*/*",
|
| 126 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
| 127 |
+
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0.1; SM-J500G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Mobile Safari/537.36",
|
| 128 |
+
Origin: "https://spotifydown.com",
|
| 129 |
+
Referer: "https://spotifydown.com/",
|
| 130 |
+
"Referrer-Policy": "strict-origin-when-cross-origin",
|
| 131 |
+
},
|
| 132 |
+
},
|
| 133 |
+
).catch(e => e.response);
|
| 134 |
+
if (!data.trackList) return []
|
| 135 |
+
return data.trackList.map((a) => ({
|
| 136 |
+
...a,
|
| 137 |
+
url: "https://open.spotify.com/track/" + a.id
|
| 138 |
+
}))
|
| 139 |
+
} catch (err) {
|
| 140 |
+
return []
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
module.exports = new Spotify();
|