Skip to content

Commit 69bec00

Browse files
committed
Fixed data fetch issue with artist names + jquery
1 parent ae3dd06 commit 69bec00

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

client/page.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
// Use this on a website that is lucky enough to not use a javascript powered framework
44
// for a personal-website frontend
55

6+
// Make sure you import jquery before using this!
7+
8+
// Use this on a website that is lucky enough to not use a javascript powered framework
9+
// for a personal-website frontend
10+
611
$.ajax({
712
url: 'https://yourweb.site/api/currentplaying',
813
type: 'get',
914
dataType: 'JSON',
1015
success: function(resp){
11-
//console.log("DONE!");
12-
//console.log(resp['trackname']);
13-
//console.log(resp['author']);
14-
//console.log(resp);
16+
1517
if(resp['playing'] == true){
1618
//console.log(resp['url']);
1719
//console.log(resp['playing']);
@@ -20,16 +22,28 @@ $.ajax({
2022

2123
a.className += " show";
2224
var songName = resp['trackname'];
23-
var artists = resp['author'];
2425
var albumTitle = resp['albumtitle'];
2526
var albumArtURL = resp['albumart'];
27+
var authors = "";
28+
29+
//Getting Author(s)
30+
if (resp.urls.artists.length > 1) {
31+
for (i = 0; i < resp.urls.artists.length; i++) {
32+
//authors[i] = {"name": data.body.item.artists[i].name, "url": data.body.item.artists[i].external_urls.spotify};
33+
authors += resp.urls.artists[i].name + ", ";
34+
}
35+
} else {
36+
authors += resp.urls.artists[0].name
37+
}
2638

2739
$(songname).text(songName);
28-
$(songartists).text("by "+artists);
40+
$(songartists).text("by " + authors);
2941
$(album).text("on "+albumTitle)
30-
$(spotifyURL).attr("href", resp['url']);
42+
$(albumURL).attr("href", resp.urls.albumURL)
43+
//$(albumURL).attr("href", "#") // Needs to be replaced with new api responses in future.
44+
$(songURL).attr("href", resp.urls.trackURL);
3145
$(albumart).attr("src", albumArtURL)
3246

3347
}
3448
}
35-
})
49+
})

server/app.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
// GitHub Repo: https://github.com/dev-sda1/spotify-web-presence
55

66
// Changelog
7+
// 1.0.1 [8/7/2021]
8+
// - Made data fetching for the artist dataset easier by adding names
9+
// into their own seperate value.
10+
//
11+
// - Fixed the client jQuery example not replicating the new output
12+
// formats.
13+
//
714
// 1.0 [6/7/2021]
815
// - First Major Version!!11
916
// - Added album URLs to JSON output
@@ -75,16 +82,16 @@ function getPlaying(req, res) {
7582
}
7683

7784
var artists = "";
78-
var authors = {};
85+
var authors = [];
7986
var i;
8087

8188
try {
8289
if (data.body.item.artists.length > 1) {
8390
for (i = 0; i < data.body.item.artists.length; i++) {
84-
authors[data.body.item.artists[i].name] = data.body.item.artists[i].external_urls.spotify;
91+
authors[i] = {"name": data.body.item.artists[i].name, "url": data.body.item.artists[i].external_urls.spotify};
8592
}
8693
} else {
87-
authors[data.body.item.artists[0]['name']] = data.body.item.artists[0].external_urls.spotify;
94+
authors[0] = {"name": data.body.item.artists[0].name, "url": data.body.item.artists[0].external_urls.spotify};
8895
}
8996

9097
res.json({

0 commit comments

Comments
 (0)