Created
May 24, 2017 21:16
-
-
Save etataurov/a53682be20d0a529fdbc90f867362f8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SpotifyWebApi = require('spotify-web-api-node'); | |
var YandexMusicApi = require('yandex-music-api'); | |
var yandexApi = new YandexMusicApi(); | |
var spotifyApi = new SpotifyWebApi({ | |
clientId: "CLIENTID", | |
clientSecret: "CLIENTSECRET" | |
}); | |
var getMatchingSong = function(track) { | |
var options = { type: "track" }; | |
var title = track.track.artists[0].name + ' ' + track.track.name; | |
return yandexApi.search(title, options).then(function(result) { | |
console.log(result); | |
console.log('\nSearch result for tracks: "' + title + '" (page: ' + result.page + ', per page: ' + result.perPage + '):'); | |
if (result.tracks !== undefined) { | |
return result.tracks.results[0]; | |
} | |
}); | |
}; | |
spotifyApi.clientCredentialsGrant() | |
.then(function(data) { | |
console.log('The access token expires in ' + data.body['expires_in']); | |
console.log('The access token is ' + data.body['access_token']); | |
// Save the access token so that it's used in future calls | |
spotifyApi.setAccessToken(data.body['access_token']); | |
}, function(err) { | |
console.log('Something went wrong when retrieving an access token', err); | |
}) | |
.then(function() { | |
return spotifyApi.getPlaylist('spotify', '6G2GPOJNY3ZmXHdbL2jAee') | |
.then(function(data) { | |
return data; | |
}, function(err) { | |
console.error(err); | |
}); | |
}) | |
.then(function(data) { | |
return yandexApi.init({username: 'EMAIL', password: 'PASSWORD'}).then(function() { | |
console.log("Initiated") | |
return yandexApi.createPlaylist(data["body"]["name"], {'visibility': 'public'}).then(function(playlist) { | |
console.log('New playlist has been created:') | |
console.log('Name: ' + playlist.title); | |
console.log('Kind: ' + playlist.kind); | |
console.log('Visibility: ' + playlist.visibility); | |
return [data, playlist]; | |
}) | |
}) | |
}).then(function(data){ | |
let spotifyData = data[0]; | |
let playlist = data[1]; | |
var actions = spotifyData.body.tracks.items.map(getMatchingSong); // run the function over all items. | |
var results = Promise.all(actions); // pass array of promises | |
return results.then(function(result) { | |
console.log(result); | |
var tracks = []; | |
result.forEach(function(el) { | |
if (el !== undefined) { | |
tracks.push({id: el.id, albumId: el.albums[0].id}) | |
} | |
}) | |
// { id:'20599729', albumId:'2347459' }, | |
// { id:'20069589', albumId:'2265364' }, | |
// { id:'15924630', albumId:'1795812' }, | |
// ] | |
// | |
return yandexApi.addTracksToPlaylist(playlist.kind, tracks, playlist.revision).then(function(playlist) { | |
console.log('Added ' + playlist.trackCount + ' tracks to the playlist:'); | |
return playlist; | |
}); | |
}); | |
}).catch(function(e) { | |
console.log(e); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment