Advertisement
Guest User

Untitled

a guest
May 24th, 2017
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. var SpotifyWebApi = require('spotify-web-api-node');
  2. var YandexMusicApi = require('yandex-music-api');
  3. var yandexApi = new YandexMusicApi();
  4. var spotifyApi = new SpotifyWebApi({
  5. clientId: "CLIENTID",
  6. clientSecret: "CLIENTSECRET"
  7. });
  8. var getMatchingSong = function(track) {
  9. var options = { type: "track" };
  10. var title = track.track.artists[0].name + ' ' + track.track.name;
  11. return yandexApi.search(title, options).then(function(result) {
  12. console.log(result);
  13. console.log('\nSearch result for tracks: "' + title + '" (page: ' + result.page + ', per page: ' + result.perPage + '):');
  14. if (result.tracks !== undefined) {
  15. return result.tracks.results[0];
  16. }
  17. });
  18. };
  19. spotifyApi.clientCredentialsGrant()
  20. .then(function(data) {
  21. console.log('The access token expires in ' + data.body['expires_in']);
  22. console.log('The access token is ' + data.body['access_token']);
  23.  
  24. // Save the access token so that it's used in future calls
  25. spotifyApi.setAccessToken(data.body['access_token']);
  26. }, function(err) {
  27. console.log('Something went wrong when retrieving an access token', err);
  28. })
  29. .then(function() {
  30. return spotifyApi.getPlaylist('spotify', '6G2GPOJNY3ZmXHdbL2jAee')
  31. .then(function(data) {
  32. return data;
  33. }, function(err) {
  34. console.error(err);
  35. });
  36. })
  37. .then(function(data) {
  38. return yandexApi.init({username: 'EMAIL', password: 'PASSWORD'}).then(function() {
  39. console.log("Initiated")
  40. return yandexApi.createPlaylist(data["body"]["name"], {'visibility': 'public'}).then(function(playlist) {
  41.  
  42. console.log('New playlist has been created:')
  43. console.log('Name: ' + playlist.title);
  44. console.log('Kind: ' + playlist.kind);
  45. console.log('Visibility: ' + playlist.visibility);
  46.  
  47. return [data, playlist];
  48. })
  49. })
  50. }).then(function(data){
  51. let spotifyData = data[0];
  52. let playlist = data[1];
  53. var actions = spotifyData.body.tracks.items.map(getMatchingSong); // run the function over all items.
  54.  
  55. var results = Promise.all(actions); // pass array of promises
  56.  
  57. return results.then(function(result) {
  58. console.log(result);
  59. var tracks = [];
  60. result.forEach(function(el) {
  61. if (el !== undefined) {
  62. tracks.push({id: el.id, albumId: el.albums[0].id})
  63. }
  64. })
  65. // { id:'20599729', albumId:'2347459' },
  66. // { id:'20069589', albumId:'2265364' },
  67. // { id:'15924630', albumId:'1795812' },
  68. // ]
  69. //
  70. return yandexApi.addTracksToPlaylist(playlist.kind, tracks, playlist.revision).then(function(playlist) {
  71.  
  72. console.log('Added ' + playlist.trackCount + ' tracks to the playlist:');
  73.  
  74. return playlist;
  75. });
  76. });
  77.  
  78. }).catch(function(e) {
  79. console.log(e);
  80. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement