-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.js
More file actions
193 lines (183 loc) · 5.94 KB
/
Copy pathitems.js
File metadata and controls
193 lines (183 loc) · 5.94 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
import { library } from './library.js';
export class User {
}
export class CurrentUser extends User {
}
export class Item {
constructor(data) {
this.data = data;
this.id = data.id;
this.name = data.name;
this.type = data.type;
this.display_name = ''; // The artist name or user name associated with the item
}
static concatenateArtists(artists) {
const num_artists = artists.length;
let artists_str = artists[0].name;
for (let i = 1; i < num_artists; i++) {
artists_str.concat(', ' + artists[i].name);
}
return artists_str;
}
static async calculateDurationString(duration_ms, magnitude = 'minutes') {
const hours = Math.floor(duration_ms / (60 * 60 * 1000))
const minutes = Math.floor(duration_ms / 60 * 1000);
const seconds = Math.ceil((duration_ms % (60 * 1000) / 1000));
let duration = '';
if (magnitude === 'hours') {
duration.concat(hours.toString() + ':');
duration.concat(minutes.toString().padStart(2, '0') + ':');
} else {
duration.concat(minutes.toString() + ':');
}
duration.concat(seconds.toString().padStart(2, '0'));
return duration;
}
}
// NOTE: could possibly add the ability to get extra information about each track such as bpm through analysis
export class Track extends Item {
constructor(data, album = '') {
// Tracks in a playlist will have a track and added_at property which this will parse
let date_added = '';
const object_properties = Object.getOwnPropertyNames(data);
const num_properties = object_properties.length;
for (let i = 0; i < num_properties; i++) {
if (object_properties[i] === 'added_at') {
date_added = data.added_at;
data = data.track;
}
}
super(data);
this.artists = data.artists;
this.display_name = Item.concatenateArtists(this.artists);
// Tracks in an album do not have an album in thier data so we pass it manually
this.album = album;
if (this.album === '') {
this.album = data.album.name;
}
this.date_added = date_added;
this.duration_ms = data.duration_ms;
this.duration = Item.calculateDurationString(this.duration_ms);
}
}
export class Collection extends Item {
constructor(data) {
super(data);
this.num_songs = data.tracks.total;
// Initialise the following so they can be loaded when required
this.tracks_href = data.tracks.href;
this.duration_ms = 0;
this.duration = '';
this.tracks = [];
}
async getTracks() {
const data = await library.getAllFromHref(this.tracks_href);
const num_tracks = data.length;
const tracks = [];
if (this.type === 'playlist') {
for (let i = 0; i < num_tracks; i++) {
tracks.push(new Track(data[i]));
}
}
else if (this.type === 'album') {
for (let i = 0; i < this.num_songs; i++) {
tracks.push(new Track(data[i], this.name));
}
}
return tracks;
}
calculateTotalDuration() {
const length = this.tracks.length;
for (let i = 0; i < length; i++) {
this.duration_ms += this.tracks[i].duration_ms;
}
const duration = Item.calculateDurationString(this.duration_ms, 'hours');
return duration;
}
async loadTracks(item) {
this.tracks = await this.getTracks(item);
this.duration = this.calculateTotalDuration(item);
return 0;
}
}
export class Playlist extends Collection {
constructor(data) {
super(data);
this.description = data.description;
this.owner = data.owner.display_name;
this.display_name = this.owner;
}
}
export class Album extends Collection {
constructor(data) {
// Albums saved to library will have an album and an added_at property which this will parse
let date_added = '';
if (Object.getOwnPropertyNames(data).length === 2) {
date_added = data.added_at;
data = data.album;
}
super(data);
this.artists = data.artists;
this.display_name = Item.concatenateArtists(this.artists);
this.album_type = data.album_type;
this.release_date = data.release_date;
this.release_year = this.release_date.substr(0, 4);
this.label = data.label;
this.date_added = date_added;
}
}
// NOTE: the api says that you can get related artists but it is Deprecated so won't include it for now
export class Artist extends Item {
constructor(data) {
super(data);
//console.log(data);
this.followers = data.followers.total;
// Initialise fields to be filled later
this.top_tracks = [];
this.discography = [];
this.appears_on = [];
}
async getTopTracks() {
const data = await library.getFromHref('https://api.spotify.com/v1/artists/' + this.id + '/top-tracks');
const num_tracks = data.tracks.length;
const top_tracks = [];
for (let i = 0; i < num_tracks; i++) {
top_tracks.push(new Track(data.tracks[i]));
}
return top_tracks;
}
async loadTopTracks() {
this.top_tracks = await this.getTopTracks();
return 0;
}
async getAlbums(include_groups = 'album,single,appears_on,compilation', market = 'ES', offset = 0, limit = 50) {
const data = await library.getAllFromHref('https://api.spotify.com/v1/artists/' + this.id + '/albums' + '?' + new URLSearchParams({
include_groups: include_groups,
limit: limit,
offset: offset,
market: market
}).toString()
);
const num_albums = data.length;
const discography = [];
for (let i = 0; i < num_albums; i++) {
let album_data = await library.getFromHref(data[i].href);
discography.push(new Album(album_data));
}
return discography;
}
async loadDiscography() {
this.discography = await this.getAlbums('album,single,compilation');
return 0;
}
async loadAppearsOn() {
this.appears_on = await this.getAlbums('appears_on');
return 0;
}
async loadArtistData() {
this.top_tracks = await this.getTopTracks();
this.discography = await this.getAlbums('album,single,compilation');
this.appears_on = await this.getAlbums('appears_on');
return 0;
}
}