spotify control

This commit is contained in:
Chris Ham
2018-12-04 11:55:33 -08:00
parent 67c677f61c
commit 11655fac86
3 changed files with 100 additions and 19 deletions

View File

@@ -62,16 +62,61 @@ function Spotify(config) {
});
};
this.getPlaybackState = () => {
spotifyApi.getMyCurrentPlaybackState({
})
.then(function(data) {
// Output items
console.log("Now Playing: ",data.body);
}, function(err) {
console.log('Something went wrong!', err);
this.getCurrentSong = () => {
return new Promise((resolve, reject) => {
spotifyApi.getMyCurrentPlaybackState({}, (err, data) => {
if (err) {
reject(err);
return;
}
let state = data.body;
resolve({
artists: state.item.artists,
name: state.item.name,
album: state.item.album.name,
url: state.item.external_urls.spotify
});
});
});
}
};
this.getCurrentPlaylist = () => {
return new Promise((resolve, reject) => {
spotifyApi.getMyCurrentPlaybackState({}, (err, data) => {
if (err) {
reject(err);
return;
}
let state = data.body;
if (state.context) {
resolve(state.context.external_urls.spotify);
} else {
resolve(state.item.album.external_urls.spotify);
}
});
});
};
this.skip = () => {
return spotifyApi.skipToNext();
};
this.pause = () => {
return spotifyApi.pause();
};
this.resume = () => {
return spotifyApi.play();
};
this.setVolume = (volume) => {
volume = parseInt(volume);
if (volume < 0) volume = 0;
if (volume > 100) volume = 100;
return spotifyApi.setVolume(volume);
};
}
module.exports = Spotify;