-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastfm.js
38 lines (36 loc) · 1.31 KB
/
lastfm.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
const lastFm = {
_baseUrl: 'https://ws.audioscrobbler.com/2.0/',
_apiKey: '59c115753cc99de97f1bf71a7eb965a4',
getUserInfo: function(userName, onSuccess, onError) {
const xhr = new XMLHttpRequest();
xhr.open("GET", `${this._baseUrl}?method=user.getinfo&user=${userName}&api_key=${this._apiKey}&format=json`, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
const result = JSON.parse(xhr.responseText);
if (result.error) {
onError(result);
} else {
onSuccess(result.user)
}
}
}
xhr.send();
},
getScrobbles: function(user, period, onScrobbles) {
const xhr = new XMLHttpRequest();
xhr.open("GET", `${this._baseUrl}?method=user.getrecenttracks&user=${user}&api_key=${this._apiKey}&from=${period.start.unix()}&to=${period.end.unix()}&limit=200&format=json`, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
const recentTracks = JSON.parse(xhr.responseText).recenttracks;
if (recentTracks.track.filter) {
onScrobbles(recentTracks.track
.filter(x => !(x['@attr'] && x['@attr']['nowplaying']))
.reverse());
} else {
onScrobbles([]);
}
}
}
xhr.send();
}
};