-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotifyAPI.js
More file actions
207 lines (179 loc) · 7.31 KB
/
spotifyAPI.js
File metadata and controls
207 lines (179 loc) · 7.31 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import fs from "fs";
export class SpotifyAPI {
_accessTokenTimestamp = 0;
loadedDataJSON = false;
spEndpoint = "https://api.spotify.com/v1/";
data = {
author: "loading",
name: "Please wait...",
song_link: "",
duration: 0,
playing: false,
album_name: "loading",
album_image: "https://upload.wikimedia.org/wikipedia/commons/5/59/Empty.png",
explicit: false,
progress: 0
};
get accessToken() {
if (this._accessToken == "") this.handleRefreshToken();
if (Date.now() > (this._accessTokenTimestamp + 3600000) && this._refreshtoken) this.handleRefreshToken();
return this._accessToken;
}
set accessToken(access_token) {
this._accessTokenTimestamp = Date.now();
this._accessToken = access_token;
}
get refreshToken() {
if (this._refreshtoken == "") return console.log("Non c'è un refresh token A") && false;
return this._refreshtoken;
}
set refreshToken(refresh_token) {
this._refreshtoken = refresh_token;
if (!this.loadedDataJSON) {
this.loadedDataJSON = true;
fs.writeFileSync('./data.json', `{ "refresh_token": "${refresh_token}" }`);
}
}
constructor() {
if (fs.existsSync("./data.json")) {
this.loadedDataJSON = true;
const datas = JSON.parse(fs.readFileSync("./data.json"));
this.refreshToken = datas.refresh_token;
}
}
async makeRequest(endpoint) {
if (!this.accessToken) await this.handleRefreshToken();
return await fetch(this.spEndpoint + endpoint, {
headers: {Authorization: `Bearer ${this.accessToken}`}
})
}
async handleRefreshToken() {
const refresh_token = this.refreshToken;
const res = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
body: new URLSearchParams({
grant_type: "refresh_token",
refresh_token
}),
headers: {Authorization: `Basic ${(Buffer.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`)).toString("base64")}`}
});
if (res.status === 200) {
const body = await res.json();
this.accessToken = body.access_token;
await this.getData();
return;
}
const text = await res.text();
console.log(`1. Error in refresh token: ${res.status}`, text);
const json = JSON.parse(text);
if (json.error == "invalid_grant") {
console.log("The grant has expired, deleting the refresh token!");
fs.unlink("./data.json", () => {
console.log("Deleted!");
});
}
}
async getData() {
if (!this.accessToken) await this.handleRefreshToken();
if (this.accessToken) {
let response = await this.makeRequest("me/player/currently-playing");
if (response.status == 200) { // Current playing
try {
const json = await response.json();
return {
author: json.item?.artists?.[0]?.name,
name: json.item.name,
song_link: json.item?.external_urls?.spotify,
duration: json.item.duration_ms,
explicit: json.item.explicit,
playing: json.is_playing,
album_name: json.item.album?.name,
album_image: json.item.album?.images?.[1]?.url || "https://upload.wikimedia.org/wikipedia/commons/5/59/Empty.png",
progress: json.progress_ms,
}
} catch (err) {
console.error(err);
console.log(response);
}
} else if (response.status == 204) { // Last played
response = await this.makeRequest("me/player/recently-played?limit=1");
let json;
try {
json = await response.json()
} catch (err) {
console.error(err);
console.log(response);
await this.handleRefreshToken();
}
try {
return {
author: json.items[0].track.artists[0].name,
name: json.items[0].track.name,
song_link: json.items[0].track.external_urls.spotify,
duration: json.items[0].track.duration_ms,
explicit: json.items[0].track.explicit,
playing: false, // Obviously it is false because it was previously playing
album_name: json.items[0].track.album.name,
album_image: json.items[0].track.album.images[1].url,
progress: 0,
};
} catch (err) {
console.error(err);
console.log(response);
}
} else if (response.status == 401) {
await this.handleRefreshToken();
return {
status: 401,
response
}
}
} else {
console.error("Non c'è un token");
}
}
async getLastSong() {
if (!this.accessToken) await this.handleRefreshToken();
if (this.accessToken) {
if (this.data.playing == false && this.data.progress == 0) return null;
let response = await this.makeRequest("me/player/recently-played?limit=2");
if (response.status == 200) {
try {
const json = await response.json();
let index = 0;
// If the last song is the same as the current one, get the previous one
if (json.items[index].track.external_urls.spotify == this.data.song_link) index = 1;
return {
author: json.items[index].track.artists[0].name,
name: json.items[index].track.name,
song_link: json.items[index].track.external_urls.spotify,
explicit: json.items[index].track.explicit,
album_name: json.items[index].track.album.name,
album_image: json.items[index].track.album.images[1].url,
};
} catch (err) {
console.error(err);
console.log(response);
}
}
}
}
async getSongData(songId) {
if (!this.accessToken) await this.handleRefreshToken();
if (this.accessToken) {
const response = await this.makeRequest(`tracks/${songId}?market=IT`);
if (response.status !== 200) {
console.log(response);
return {status: response.status};
}
const json = await response.json();
return {
name: json.name,
author: json.artists.map(a => a.name).join(", "),
explicit: json.explicit,
album_name: json.album.name,
album_image: json.album.images[1].url
}
}
}
}