fast72 commited on
Commit
cbeae54
·
verified ·
1 Parent(s): aa87d09

Upload spotify.js

Browse files
Files changed (1) hide show
  1. spotify.js +142 -0
spotify.js ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require("axios");
2
+
3
+ const client_id = "acc6302297e040aeb6e4ac1fbdfd62c3";
4
+ const client_secret = "0e8439a1280a43aba9a5bc0a16f3f009";
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
+ }));
67
+ } catch (error) {
68
+ return {
69
+ status: false,
70
+ msg: "Error searching for music. " + error.message,
71
+ };
72
+ }
73
+ };
74
+
75
+ download = async (url) => {
76
+ const BASEURL = "https://api.fabdl.com";
77
+ const headers = {
78
+ Accept: "application/json, text/plain, */*",
79
+ "Content-Type": "application/json",
80
+ "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36",
81
+ };
82
+
83
+ try {
84
+ const {
85
+ data: info
86
+ } = await axios.get(`${BASEURL}/spotify/get?url=${url}`, {
87
+ headers
88
+ });
89
+ const {
90
+ gid,
91
+ id,
92
+ name,
93
+ image,
94
+ duration_ms
95
+ } = info.result;
96
+
97
+ const {
98
+ data: download
99
+ } = await axios.get(`${BASEURL}/spotify/mp3-convert-task/${gid}/${id}`, {
100
+ headers
101
+ });
102
+ if (download.result.download_url) {
103
+ return {
104
+ title: name,
105
+ duration: duration_ms,
106
+ cover: image,
107
+ download: `${BASEURL}${download.result.download_url}`
108
+ }
109
+ }
110
+ } catch (error) {
111
+ console.error("Error downloading Spotify track:", error.message);
112
+ throw new Error(error.message);
113
+ }
114
+ };
115
+ playlist = async function playlist(url) {
116
+ try {
117
+ const id = url.split("/playlist/")[0].trim().split("?si=")[0].trim();
118
+ const {
119
+ data
120
+ } = await axios.get(
121
+ "https://api.spotifydown.com/trackList/playlist/0F1VNeiZKjtX06yrVSqlbu", {
122
+ headers: {
123
+ Accept: "*/*",
124
+ "Content-Type": "application/x-www-form-urlencoded",
125
+ "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",
126
+ Origin: "https://spotifydown.com",
127
+ Referer: "https://spotifydown.com/",
128
+ "Referrer-Policy": "strict-origin-when-cross-origin",
129
+ },
130
+ },
131
+ ).catch(e => e.response);
132
+ if (!data.trackList) return []
133
+ return data.trackList.map((a) => ({
134
+ ...a,
135
+ url: "https://open.spotify.com/track/" + a.id
136
+ }))
137
+ } catch (err) {
138
+ return []
139
+ }
140
+ }
141
+ }
142
+ module.exports = new Spotify();