This repository was archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathplugin.ts
More file actions
289 lines (242 loc) · 9.33 KB
/
plugin.ts
File metadata and controls
289 lines (242 loc) · 9.33 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import {
Manager,
Plugin,
TrackUtils,
UnresolvedTrack,
UnresolvedQuery,
LoadType,
SearchQuery
} from "erela.js";
import Axios from "axios";
const BASE_URL = "https://api.spotify.com/v1";
const REGEX = /(?:https:\/\/open\.spotify\.com\/|spotify:)(?:.+)?(track|playlist|album)[\/:]([A-Za-z0-9]+)/;
const buildSearch = (loadType: LoadType, tracks: UnresolvedTrack[], error: string, name: string): SearchResult => ({
loadType: loadType,
tracks: tracks ?? [],
playlist: name ? {
name,
duration: tracks
.reduce(
(acc: number, cur: UnresolvedTrack) => acc + (cur.duration || 0),
0
),
} : null,
exception: error ? {
message: error,
severity: "COMMON"
} : null,
});
const check = (options: SpotifyOptions) => {
if (!options) throw new TypeError("SpotifyOptions must not be empty.");
if (typeof options.clientID !== "string" || !/^.+$/.test(options.clientID))
throw new TypeError(
'Spotify option "clientID" must be present and be a non-empty string.'
);
if (typeof options.clientSecret !== "string" || !/^.+$/.test(options.clientSecret))
throw new TypeError(
'Spotify option "clientSecret" must be a non-empty string.'
);
if (
typeof options.convertUnresolved !== "undefined" &&
typeof options.convertUnresolved !== "boolean"
)
throw new TypeError(
'Spotify option "convertUnresolved" must be a boolean.'
);
if (
typeof options.playlistLimit !== "undefined" &&
typeof options.playlistLimit !== "number"
)
throw new TypeError('Spotify option "playlistLimit" must be a number.');
if (
typeof options.albumLimit !== "undefined" &&
typeof options.albumLimit !== "number"
)
throw new TypeError('Spotify option "albumLimit" must be a number.');
}
export class Spotify extends Plugin {
private readonly authorization: string;
private token: string;
private readonly axiosOptions: { headers: { Authorization: string; "Content-Type": string } };
private _search: (query: string | SearchQuery, requester?: unknown) => Promise<SearchResult>;
private manager: Manager;
private readonly functions: Record<string, Function>;
private readonly options: SpotifyOptions;
public constructor(options: SpotifyOptions) {
super();
check(options);
this.options = {
...options
}
this.token = "";
this.authorization = Buffer.from(
`${this.options.clientID}:${this.options.clientSecret}`
).toString("base64");
this.axiosOptions = {
headers: {
"Content-Type": "application/json",
Authorization: this.token
}
};
this.functions = {
track: this.getTrack.bind(this),
album: this.getAlbumTracks.bind(this),
playlist: this.getPlaylistTracks.bind(this),
};
this.renew();
}
public load(manager: Manager) {
this.manager = manager;
this._search = manager.search.bind(manager);
manager.search = this.search.bind(this);
}
private async search(query: string | SearchQuery, requester?: unknown): Promise<SearchResult> {
const finalQuery = (query as SearchQuery).query || query as string;
const [, type, id] = finalQuery.match(REGEX) ?? [];
if (type in this.functions) {
try {
const func = this.functions[type];
if (func) {
const data: Result = await func(id);
const loadType = type === "track" ? "TRACK_LOADED" : "PLAYLIST_LOADED";
const name = ["playlist", "album"].includes(type) ? data.name : null;
const tracks = data.tracks.map(query => {
const track = TrackUtils.buildUnresolved(query, requester);
if (this.options.convertUnresolved) {
try {
track.resolve();
} catch {
return null;
}
}
return track;
}).filter(track => !!track);
return buildSearch(loadType, tracks, null, name);
}
const msg = 'Incorrect type for Spotify URL, must be one of "track", "album" or "playlist".';
return buildSearch("LOAD_FAILED", null, msg, null);
} catch (e) {
return buildSearch(e.loadType ?? "LOAD_FAILED", null, e.message ?? null, null);
}
}
return this._search(query, requester);
}
private async getAlbumTracks(id: string): Promise<Result> {
const { data: album } = await Axios.get<Album>(`${BASE_URL}/albums/${id}`, this.axiosOptions);
const tracks = album.tracks.items.map(item => Spotify.convertToUnresolved(item));
let next = album.tracks.next, page = 1;
while (next && !this.options.playlistLimit ? true : page < this.options.albumLimit) {
const { data: nextPage } = await Axios.get<AlbumTracks>(next, this.axiosOptions);
tracks.push(...nextPage.items.map(item => Spotify.convertToUnresolved(item)));
next = nextPage.next;
page++;
}
return { tracks, name: album.name };
}
private async getPlaylistTracks(id: string): Promise<Result> {
let { data: playlist } = await Axios.get<Playlist>(`${BASE_URL}/playlists/${id}`, this.axiosOptions);
const tracks = playlist.tracks.items.map(item => Spotify.convertToUnresolved(item.track));
let next = playlist.tracks.next, page = 1;
while (next && !this.options.playlistLimit ? true : page < this.options.playlistLimit) {
const { data: nextPage } = await Axios.get<PlaylistTracks>(next, this.axiosOptions);
tracks.push(...nextPage.items.map(item => Spotify.convertToUnresolved(item.track)));
next = nextPage.next;
page++;
}
return { tracks, name: playlist.name };
}
private async getTrack(id: string): Promise<Result> {
const { data } = await Axios.get<SpotifyTrack>(`${BASE_URL}/tracks/${id}`, this.axiosOptions);
const track = Spotify.convertToUnresolved(data);
return { tracks: [track] };
}
private static convertToUnresolved(track: SpotifyTrack): UnresolvedQuery {
if (!track) throw new ReferenceError("The Spotify track object was not provided");
if (!track.artists) throw new ReferenceError("The track artists array was not provided");
if (!track.name) throw new ReferenceError("The track name was not provided");
if (!Array.isArray(track.artists)) throw new TypeError(`The track artists must be an array, received type ${typeof track.artists}`);
if (typeof track.name !== "string") throw new TypeError(`The track name must be a string, received type ${typeof track.name}`);
return {
title: track.name,
author: track.artists[0].name,
duration: track.duration_ms,
spotifyUri: track.external_urls.spotify,
}
}
private async renewToken(): Promise<number> {
const { data: { access_token, expires_in } } = await Axios.post(
"https://accounts.spotify.com/api/token",
"grant_type=client_credentials",
{
headers: {
Authorization: `Basic ${this.authorization}`,
"Content-Type": "application/x-www-form-urlencoded"
}
}
);
if (!access_token) throw new Error("Invalid Spotify client.");
this.token = `Bearer ${access_token}`;
this.axiosOptions.headers.Authorization = this.token;
return expires_in * 1000;
}
private async renew(): Promise<void> {
setTimeout(this.renew.bind(this), await this.renewToken());
}
}
export interface SpotifyOptions {
clientID: string;
clientSecret: string;
/** Amount of pages to load, each page having 100 tracks. */
playlistLimit?: number
/** Amount of pages to load, each page having 50 tracks. */
albumLimit?: number
/**
* Whether to convert UnresolvedTracks to Track. Defaults to false.
* **Note: This is** ***not*** **recommended as it spams YouTube and takes a while if a large playlist is loaded.**
*/
convertUnresolved?: boolean
}
export interface Result {
tracks: UnresolvedQuery[];
name?: string;
}
export interface Album {
name: string;
tracks: AlbumTracks;
}
export interface AlbumTracks {
items: SpotifyTrack[];
next: string | null;
}
export interface Artist {
name: string;
}
export interface Playlist {
tracks: PlaylistTracks;
name: string;
}
export interface PlaylistTracks {
items: [
{
track: SpotifyTrack;
}
];
next: string | null;
}
export interface SpotifyTrack {
artists: Artist[];
name: string;
duration_ms: number;
}
export interface SearchResult {
exception?: {
severity: string;
message: string
};
loadType: string;
playlist?: {
duration: number;
name: string
};
tracks: UnresolvedTrack[]
}