-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (55 loc) · 2.11 KB
/
index.js
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
import BaseModule from './structures/module/BaseModule.js'
import DeezerAPI from './structures/api/DeezerAPI.js'
import { HostNames } from './util/Constants.js'
export default class TrackDeezer extends BaseModule {
/**
* @param {Main} main
*/
constructor(main) {
super(main);
this.register(TrackDeezer, {
name: 'trackDeezer',
requires: [ 'trackResolver' ]
});
}
/**
* @private
* @param {URL} url
*/
async _resolve(url) {
let deezer = url.pathname;
let isAlbum = deezer.includes('/album/');
let isPlaylist = deezer.includes('/playlist/');
let isPlaylistOrAlbum = isAlbum || isPlaylist;
if (!isAlbum && !isPlaylist && !deezer.includes('/track/')) {
const trackLink = await this.deezer.fetchSharableLink(deezer);
deezer = new URL(trackLink).pathname;
isAlbum = deezer.includes('/album/');
isPlaylist = deezer.includes('/playlist/');
isPlaylistOrAlbum = isAlbum || isPlaylist;
if(!isPlaylistOrAlbum) {
const track = (await this.deezer.getTrack(deezer.split('/track/')[1]));
return { type: 'song', data: new this.resolvableTrack(this._m, track) };
}
}
const playlist = isPlaylist
? (await this.deezer.getPlaylist(deezer.split('/playlist/')[1]))
: (await this.deezer.getAlbum(deezer.split('/album/')[1]));
const trackList = [];
playlist.tracks.data.forEach(track => trackList.push(new this.resolvableTrack(this._m, track)));
this._m.emit(isPlaylist ? 'playlistPlayed' : 'albumPlayed');
return { type: isPlaylist ? 'playlist' : 'album', data: trackList };
}
async init() {
this.deezer = new DeezerAPI(this._m);
this.resolvableTrack = (await import('./structures/track/DeezerTrack.js')).default;
this.modules.trackResolver.registerResolver(this.name, HostNames);
return true;
}
/**
* @param {URL} url
*/
resolve(url) {
return this._resolve(url);
}
}