-
Notifications
You must be signed in to change notification settings - Fork 1
/
lastfm.js
106 lines (103 loc) · 4.04 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
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
/**
* Class representing the contained Last.fm API calls. Stores the API key.
*/
class LastFmClient { // eslint-disable-line no-unused-vars
/**
* Create an instance of the LastFmClient with provided API key.
* @param {string} apikey Last.fm API key that will be used to make API calls. Can be obtained here: https://www.last.fm/api/account/create
*/
constructor (apikey) {
this._apikey = apikey;
}
/**
* Get list of top albums with playcount, name, id and image. Asynchronous function resolves with result.
* Error alert is displayed if an error has occurred.
* @param {string} username username of user.
* @param {integer} limit Maximum number of albums to fetch.
* @param {string} period Timescale to fetch top albums.
* @returns {Array} List of top albums for this user
*/
async fetchAlbumList (username, limit, period) {
const albums = [];
try {
const response = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.gettopalbums&user=${username}&period=${period}&limit=${limit}&api_key=${this._apikey}&format=json`
)
.then(response => {
if (!response.ok) throw new Error(response.status);
return response.json();
});
response.topalbums.album.forEach(element => {
const {
playcount,
name,
mbid
} = element;
const image = element.image.find(image => image.size === 'extralarge')['#text'];
albums.push({
type: 'release',
value: playcount,
name,
image,
id: mbid || ('!' + Math.random().toString(36).substr(2, 12))
});
});
} catch (error) {
alert('Failed to fetch: ' + error);
}
return albums;
}
/**
* Get list of top artists with playcount, name, id. Asynchronous function resolves with result.
* Error alert is displayed if an error has occurred.
* @param {string} username username of user.
* @param {integer} limit Maximum number of artists to fetch.
* @param {string} period Timescale to fetch top artists.
* @returns {Array} List of top artists for this user
*/
async fetchArtistList (username, limit, period) {
const artists = [];
try {
const response = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=${username}&period=${period}&limit=${limit}&api_key=${this._apikey}&format=json`
)
.then(response => {
if (!response.ok) throw new Error(response.status);
return response.json();
});
response.topartists.artist.forEach(element => {
const {
playcount,
name,
mbid
} = element;
// let image = element.image.find(image => image.size == "extralarge")['#text']; Disabled by Last.fm API
const image = '';
artists.push({
type: 'artist',
value: playcount,
name,
image,
id: mbid || ('!' + Math.random().toString(36).substr(2, 12))
});
});
} catch (error) {
alert('Failed to fetch: ' + error);
}
return artists;
}
/**
* Create a click handler that can be used with this dataset.
* @returns {function} Click handler
*/
createClickHandler () {
return function (data) {
if (data.id && !data.id.startsWith('!')) {
window.open(
`https://musicbrainz.org/${data.type}/${data.id}`, '_blank');
} else {
alert(`No associated MusicBrainz ID for ${data.name}.`);
}
};
}
}