Commit bfee137b authored by Deac Karns's avatar Deac Karns
Browse files

Merge branch 'dev'

parents 313e014f 12ca9b02
{
"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',
......@@ -133,20 +134,44 @@ const exchangeAuthorizationCodeForTokens = function(code) {
},
json: true
}
return new Promise(function(resolve, reject) {
return request.post(options, function(error, response, body) {
if (!error && response.statusCode === 200) {
writeToConfig(
{
access_token:body.access_token,
refresh_token:body.refresh_token
}
);
config.access_token = body.access_token;
config.refresh_token = body.refresh_token
resolve(config);
}
});
});
}
return new Promise(function(resolve, reject) {
request.post(options, function(error, response, body) {
if (!error && response.statusCode === 200) {
writeToConfig(
{
access_token:body.access_token,
refresh_token:body.refresh_token
}
);
resolve(true);
}
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) {
......@@ -262,6 +287,7 @@ const ensureClientId = function() {
writeToConfig({
client_id: answers.answer
});
config.client_id = answers.answer;
return answers.answer;
});
}else{
......@@ -286,6 +312,7 @@ const ensureClientSecret = function() {
writeToConfig({
client_secret: answers.answer
});
config.client_secret = answers.answer;
return answers.answer;
});
}else{
......@@ -298,32 +325,31 @@ const ensureClientSecret = function() {
const ensureTokens = function() {
if(!config.authorization_code){
return new Promise(function(resolve, reject) {
return new Promise(function(resolve) {
console.reset()
displayAuthorizeUrl()
.then( (authorizeUrl) => {
return new Promise(function(resolve, reject) {
opn(authorizeUrl)
resolve(true)
});
})
.then( () => {
return promptForAuthorizationCode()
})
.then( (code) => {
writeToConfig({authorization_code:code});
return code;
})
.then( (code) => {
return exchangeAuthorizationCodeForTokens(code)
})
return displayAuthorizeUrl()
.then( (authorizeUrl) => {
return new Promise(function(resolve, reject) {
opn(authorizeUrl)
resolve(true)
});
})
.then( () => {
return promptForAuthorizationCode()
})
.then( (code) => {
writeToConfig({authorization_code:code});
return code;
})
.then( (code) => {
resolve(exchangeAuthorizationCodeForTokens(code))
})
});
}
else{
return new Promise(function(resolve, reject) {
resolve(config.authorization_code)
return new Promise(function(resolve) {
resolve(config)
});
}
}
......@@ -336,29 +362,28 @@ const ProgramPlaylist = function(playlist=null) {
}
return dynpl
.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())
return t
.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())
return t
})
}
const runProgram = function(){
if(program.add){
refreshAccessToken()
.then( () => {
getCurrentTrack()
return refreshAccessToken()
.then( getCurrentTrack )
.then( track => {
console.log(colors.cyan('\n[Track]'))
console.log(colors.magenta(' Artist: ')+track.album_artist);
......@@ -370,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 => {
......@@ -377,32 +406,52 @@ const runProgram = function(){
})
})
.then( playlist => {
ProgramPlaylist(playlist)
return ProgramPlaylist(playlist)
})
})
}
else if(program.current){
refreshAccessToken()
.then( () => {
return getCurrentTrack()
return refreshAccessToken()
.then( () => {
return 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);
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 true
})
}
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){
ProgramPlaylist()
return ProgramPlaylist()
}
else if(program.settings){
else if(program.info){
console.log(config)
}
......@@ -412,6 +461,7 @@ const runProgram = function(){
}
ensureClientId()
.then(ensureClientSecret)
.then(ensureTokens)
.finally(runProgram)
\ No newline at end of file
.then(ensureClientSecret)
.then(ensureTokens)
.then(runProgram)
.finally(process.exit)
\ No newline at end of file
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