1
+ // spotify-web-presence
2
+ // By: dev-sda1
3
+ // Version 1.0
4
+ // GitHub Repo: https://github.com/dev-sda1/spotify-web-presence
5
+
6
+ // Changelog
7
+ // 1.0 [6/7/2021]
8
+ // - First Major Version!!11
9
+ // - Added album URLs to JSON output
10
+ // - Added seperate artist outputs to JSON output
11
+ // - Errors on the data fetching of /currentplaying will now output in console instead of
12
+ // relaying in the browser as "no track playing"
13
+ //
14
+
15
+
1
16
const express = require ( "express" ) ;
2
17
const SpotifyWebApi = require ( "spotify-web-api-node" ) ;
3
18
const config = require ( "./config.json" ) ;
@@ -52,43 +67,37 @@ function getPlaying(req, res) {
52
67
spotifyApi . getMyCurrentPlayingTrack ( )
53
68
. then ( function ( data ) {
54
69
55
- //console.log(data.body.item.album.images[2]["url"]);
56
- //console.log(data.body.item.external_urls['spotify']);
57
- //console.log(data.body.item.album.external_urls['spotify']);
58
- //console.log(data.body.item.album.name);
59
- //var artist = data.body.item.artists[0]['name'];
60
- //console.log(data.body.item.artists);
61
- //console.log(artist);
62
- //console.log("hi")
70
+ console . log ( data . body . item . artists [ 0 ] . name ) ;
71
+ console . log ( data . body . item . artists [ 0 ] . external_urls . spotify ) ;
63
72
64
73
if ( data . body === { } ) {
65
74
console . log ( "There is nothing." ) ;
66
75
}
67
76
68
77
var artists = "" ;
78
+ var authors = { } ;
69
79
var i ;
70
80
71
81
try {
72
82
if ( data . body . item . artists . length > 1 ) {
73
83
for ( i = 0 ; i < data . body . item . artists . length ; i ++ ) {
74
- artists += data . body . item . artists [ i ] [ ' name' ] + ", "
84
+ authors [ data . body . item . artists [ i ] . name ] = data . body . item . artists [ i ] . external_urls . spotify ;
75
85
}
76
86
} else {
77
- artists += data . body . item . artists [ 0 ] [ 'name' ]
87
+ authors [ data . body . item . artists [ 0 ] [ 'name' ] ] = data . body . item . artists [ 0 ] . external_urls . spotify ;
78
88
}
79
89
80
90
res . json ( {
81
91
"trackname" : data . body . item . name ,
82
- "author" : artists ,
83
- "url" : data . body . item . external_urls [ 'spotify' ] ,
84
92
"albumtitle" : data . body . item . album . name ,
85
93
"albumart" : data . body . item . album . images [ 1 ] [ "url" ] ,
86
94
"playing" : data . body . is_playing ,
87
- "urls" : { "authorURL " : "" , "albumURL " : "" , "songURL " : "" }
95
+ "urls" : { "albumURL " : data . body . item . album . external_urls [ 'spotify' ] , "trackURL " : data . body . item . external_urls [ 'spotify' ] , "artists " : authors }
88
96
} ) ;
89
97
90
98
} catch ( e ) {
91
99
res . json ( { "notice" : "No sound playing" } ) ;
100
+ console . log ( e ) ;
92
101
}
93
102
94
103
} , function ( err ) {
@@ -99,9 +108,9 @@ function getPlaying(req, res) {
99
108
100
109
101
110
app . get ( '/api/currentplaying' , function ( req , res ) {
102
- var owo = Number ( Math . floor ( Date . now ( ) / 1000 ) - tokenExpireEpoch ) ;
103
- console . log ( owo ) ;
104
- if ( owo >= 3600 ) {
111
+ var timesincetoken = Number ( Math . floor ( Date . now ( ) / 1000 ) - tokenExpireEpoch ) ;
112
+ // console.log(timesincetoken );
113
+ if ( timesincetoken >= 3600 ) {
105
114
pleaseRefreshlmao ( ) ;
106
115
getPlaying ( req , res ) ;
107
116
} else {
@@ -115,6 +124,7 @@ app.get('/api/currentplaying', function (req, res) {
115
124
} ) ;
116
125
117
126
127
+ // Callback - for when you're setting the API up for the first time
118
128
119
129
app . get ( '/callback' , function ( req , res ) {
120
130
var code = req . query . code ;
@@ -137,7 +147,7 @@ app.get('/callback', function (req, res) {
137
147
//res.status(err.code);
138
148
//res.send(err.message);
139
149
//res.sendStatus(500).json({"success": false});
140
- console . log ( "oh fuck it errored. error was : " + err . message ) ;
150
+ console . log ( "Fatal error! Couldn't get an auth token : " + err . message ) ;
141
151
} )
142
152
}
143
153
} ) ;
0 commit comments