Commit 6d3a5033 authored by Deac Karns's avatar Deac Karns
Browse files

trying to display track list after adding

parent 95e9fa6b
......@@ -149,47 +149,49 @@ const exchangeAuthorizationCodeForTokens = function(code) {
});
}
const addTrackToPlaylist = function(track,playlist) {
trackString = querystring.stringify({
uris: track.track_uri,
});
const addTrackToPlaylist = function(track, playlist) {
trackString = querystring.stringify({
uris: track.track_uri,
});
let options = {
url: ' https://api.spotify.com/v1/playlists/'+playlist.id+'/tracks?'+trackString,
auth: {
'bearer': config.access_token
},
json: true
}
let options = {
url: ' https://api.spotify.com/v1/playlists/'+playlist.id+'/tracks?'+trackString,
auth: {
'bearer': config.access_token
},
json: true
}
return new Promise(function(resolve, reject) {
request.post(options, function(error, response, body) {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
resolve(true);
}
});
});
return new Promise(function(resolve, reject) {
request.post(options, function(error, response, body) {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
resolve(playlist);
}else{
reject(error);
}
})
})
}
const selectPlaylist = function(){
console.log('\n');
return getAllPlaylists()
.then( (pl) => {
playlists = pl.items.map(p => p.name)
return inquirer
.prompt([
{
type: 'list',
name: 'playlist',
message: 'Select a playlist?',
choices: playlists
}
])
.then(answers => {
let list = pl.items.filter( p => p.name == answers.playlist )
return list.shift()
});
})
.then( (pl) => {
let playlists = pl.items.map(p => p.name)
return inquirer
.prompt([
{
type: 'list',
name: 'playlist',
message: 'Select a playlist?',
choices: playlists
}
])
.then(answers => {
let list = pl.items.filter( p => p.name == answers.playlist )
return list.shift()
});
})
}
const getCurrentTrack = function() {
......@@ -326,40 +328,65 @@ const ensureTokens = function() {
}
}
const ProgramPlaylist = function(playlist=null) {
if(!playlist){
console.log('no playlist');
dynpl = selectPlaylist()
}else{
console.log('yes playlist');
dynpl = new Promise(resolve => reslove())
}
return dynpl
.then( pl => {
console.log('here');
return getPlaylistTracks(pl)
})
.then( t => {
t.items.forEach( function(i) {
table.push(
[i.track.name, i.track.artists.map( (artist) => artist.name).toString(), i.track.album.name]
);
})
return table;
})
.then( t => {
console.log(table.toString())
return t
})
}
const runProgram = function(){
if(program.add){
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) => {
return selectPlaylist()
.then( (playlist) => {
console.log(colors.cyan('\n[Playlist]'))
console.log(colors.magenta(' Name: ')+playlist.name)
console.log(colors.magenta(' ID: ')+playlist.id)
return playlist
}).then( (playlist) => {
return addTrackToPlaylist(track, playlist)
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
})
.finally( () => {
console.log('\nTrack added to playlist\n');
process.exit()
.then( track => {
return selectPlaylist()
.then( playlist => {
return addTrackToPlaylist(track, playlist)
})
.then(tst => {
return tst
})
})
.then( playlist => {
ProgramPlaylist(playlist)
process.exit()
})
})
})
}
else if(program.current){
......@@ -380,19 +407,7 @@ const runProgram = function(){
}
else if(program.playlist){
selectPlaylist()
.then( pl => {
return getPlaylistTracks(pl)
})
.then( t => {
t.items.forEach( function(i) {
table.push(
[i.track.name, i.track.artists.map( (artist) => artist.name).toString(), i.track.album.name]
);
})
return table;
})
.then( t => console.log(table.toString()) )
ProgramPlaylist()
}
else if(program.settings){
......
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