Commit 12ca9b02 authored by Deac Karns's avatar Deac Karns
Browse files

add ability to save track; save track to library when added to playlist

parent fd80c43f
{
"name": "spotiko",
"version": "0.1.1",
"version": "0.1.2",
"description": "a node CLI app for working with the spotify API",
"main": "spotiko",
"bin": {
......
......@@ -19,11 +19,12 @@ table = new Table({
});
program
.version('0.1.1')
.version('0.1.2')
.option('-a, --add', 'Add current track to default playlist', 0)
.option('-s, --settings', 'Display the current configuration', 0)
.option('-s, --save', 'Save the currently playing track to the users library', 0)
.option('-c, --current', 'Display the current playing track', 0)
.option('-p, --playlist', 'Display playlist tracks', 0)
.option('-i, --info', 'Display confiuration values', 0)
.parse(process.argv);
console.reset = function () {
......@@ -101,7 +102,7 @@ const refreshAccessToken = function() {
const displayAuthorizeUrl = function() {
// your application requests authorization
let scope = 'user-read-private user-read-currently-playing playlist-read-private playlist-modify-private playlist-modify-public playlist-read-collaborative';
let scope = 'user-read-private user-read-currently-playing playlist-read-private playlist-modify-private playlist-modify-public playlist-read-collaborative user-library-modify';
let authorizeUrl = 'https://accounts.spotify.com/authorize?' +
querystring.stringify({
response_type: 'code',
......@@ -150,6 +151,29 @@ const exchangeAuthorizationCodeForTokens = function(code) {
});
}
const addTrackToLibrary = function(track) {
trackString = querystring.stringify({
ids: track.track_id,
});
let options = {
url: ' https://api.spotify.com/v1/me/tracks?'+trackString,
auth: {
'bearer': config.access_token
},
json: true
}
return new Promise(function(resolve, reject) {
request.put(options, function(error, response, body) {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
resolve(track);
}else{
reject(error);
}
})
})
}
const addTrackToPlaylist = function(track, playlist) {
trackString = querystring.stringify({
uris: track.track_uri,
......@@ -359,8 +383,7 @@ const runProgram = function(){
if(program.add){
return refreshAccessToken()
.then( () => {
getCurrentTrack()
.then( getCurrentTrack )
.then( track => {
console.log(colors.cyan('\n[Track]'))
console.log(colors.magenta(' Artist: ')+track.album_artist);
......@@ -372,6 +395,10 @@ const runProgram = function(){
return track
})
.then( track => {
console.log("\n Track Added to library")
return addTrackToLibrary(track)
})
.then( track => {
return selectPlaylist()
.then( playlist => {
......@@ -381,7 +408,7 @@ const runProgram = function(){
.then( playlist => {
return ProgramPlaylist(playlist)
})
})
}
else if(program.current){
......@@ -401,11 +428,30 @@ const runProgram = function(){
})
}
else if(program.save){
return refreshAccessToken()
.then( getCurrentTrack )
.then( track => {
console.log(colors.cyan('\n[Track]'))
console.log(colors.magenta(' Artist: ')+track.album_artist);
console.log(colors.magenta(' Album: ')+track.album_name);
console.log(colors.magenta(' Name: ')+track.track_name);
console.log(colors.magenta(' ID: ')+track.track_id);
console.log(colors.magenta(' URI: ')+track.track_uri);
console.log(colors.magenta(' URL: ')+track.track_url);
return track
})
.then( track => {
console.log("Added to library")
return addTrackToLibrary(track)
})
}
else if(program.playlist){
return ProgramPlaylist()
}
else if(program.settings){
else if(program.info){
console.log(config)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment