File size: 4,895 Bytes
0a25a82
 
0d3782f
 
0a25a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522e078
0a25a82
 
 
 
c30a7c4
8ee0631
0a25a82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const axios = require("axios");

const client_id = "6dc6b46d39c24a078ff5946b1295bd81";
const client_secret = "f30d47e4b2ed46ec82ddc8e861e6cc99";
const basic = Buffer.from(`${client_id}:${client_secret}`).toString("base64");
const TOKEN_ENDPOINT = "https://accounts.spotify.com/api/token";

async function spotifyCreds() {
    try {
        const response = await axios.post(
            TOKEN_ENDPOINT,
            "grant_type=client_credentials", {
                headers: {
                    Authorization: "Basic " + basic,
                },
            },
        );
        return {
            status: true,
            data: response.data,
        };
    } catch (error) {
        return {
            status: false,
            msg: "Failed to retrieve Spotify credentials.",
        };
    }
}

const toTime = (ms) => {
    let h = Math.floor(ms / 3600000);
    let m = Math.floor(ms / 60000) % 60;
    let s = Math.floor(ms / 1000) % 60;
    return [h, m, s].map((v) => v.toString().padStart(2, "0")).join(":");
};

class Spotify {
    search = async (query, type = "track", limit = 20) => {
        try {
            const creds = await spotifyCreds();
            if (!creds.status) return creds;

            const response = await axios.get(
                `https://api.spotify.com/v1/search?query=${encodeURIComponent(query)}&type=${type}&offset=0&limit=${limit}`, {
                    headers: {
                        Authorization: "Bearer " + creds.data.access_token,
                    },
                },
            );

            if (
                !response.data[type + "s"] ||
                !response.data[type + "s"].items.length
            ) {
                return {
                    msg: "Music not found!",
                };
            }

            return response.data[type + "s"].items.map((item) => ({
                title: item.name,
                id: item.id,
                duration: toTime(item.duration_ms),
                artist: item.artists.map((artist) => artist.name).join(" & "),
                url: item.external_urls.spotify,
                thumbnail: item.album.images[0]?.url,
                //...item
            }));
        } catch (error) {
            return {
                status: false,
                msg: "Error searching for music. " + error.message,
            };
        }
    };

    download = async (url) => {
        const BASEURL = "https://api.fabdl.com";
        const headers = {
            Accept: "application/json, text/plain, */*",
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36",
        };

        try {
            const {
                data: info
            } = await axios.get(`${BASEURL}/spotify/get?url=${url}`, {
                headers
            });
            const {
                gid,
                id,
                name,
                image,
                duration_ms
            } = info.result;

            const {
                data: download
            } = await axios.get(`${BASEURL}/spotify/mp3-convert-task/${gid}/${id}`, {
                headers
            });
            if (download.result.download_url) {
                return {
                    title: name,
                    duration: duration_ms,
                    cover: image,
                    download: `${BASEURL}${download.result.download_url}`
                }
            }
        } catch (error) {
            console.error("Error downloading Spotify track:", error.message);
            throw new Error(error.message);
        }
    };
    playlist = async function playlist(url) {
        try {
            const id = url.split("/playlist/")[0].trim().split("?si=")[0].trim();
            const {
                data
            } = await axios.get(
                "https://api.spotifydown.com/trackList/playlist/0F1VNeiZKjtX06yrVSqlbu", {
                    headers: {
                        Accept: "*/*",
                        "Content-Type": "application/x-www-form-urlencoded",
                        "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",
                        Origin: "https://spotifydown.com",
                        Referer: "https://spotifydown.com/",
                        "Referrer-Policy": "strict-origin-when-cross-origin",
                    },
                },
            ).catch(e => e.response);
            if (!data.trackList) return []
            return data.trackList.map((a) => ({
                ...a,
                url: "https://open.spotify.com/track/" + a.id
            }))
        } catch (err) {
            return []
        }
    }
}
module.exports = new Spotify();