UNPKG

nativescript-spotify

Version:
151 lines 6.57 kB
var auth_1 = require('./auth'); var search_1 = require('./search'); var TNSSpotifyPlaylist = (function () { function TNSSpotifyPlaylist() { } TNSSpotifyPlaylist.MINE = function (offset) { if (offset === void 0) { offset = 0; } return new Promise(function (resolve, reject) { var processResults = function (error, results) { if (error != null) { console.log("*** Spotify playlists error:"); console.log(error); reject(error); return; } TNSSpotifyPlaylist.CURRENT_LIST = results; TNSSpotifyPlaylist.PLAYLISTS_FROM_RESULTS(results).then(function (playlists) { if (playlists && playlists.length) { var result = { page: offset, hasNextPage: results.hasNextPage, totalListLength: results.totalListLength, playlists: playlists }; resolve(result); } else { reject(); } }, reject); }; if (offset > 0 && TNSSpotifyPlaylist.CURRENT_LIST) { if (TNSSpotifyPlaylist.CURRENT_LIST.hasNextPage) { TNSSpotifyPlaylist.CURRENT_LIST.requestNextPageWithAccessTokenCallback(auth_1.TNSSpotifyAuth.SESSION.accessToken, processResults); } } else { SPTPlaylistList.playlistsForUserWithSessionCallback(auth_1.TNSSpotifyAuth.SESSION, processResults); } }); }; TNSSpotifyPlaylist.FOR_USER = function (user) { return new Promise(function (resolve, reject) { SPTPlaylistList.playlistsForUserWithSessionCallback(user, auth_1.TNSSpotifyAuth.SESSION, function (error, playlist) { if (error != null) { console.log("*** playlistsForUserWithSessionCallback got error:"); console.log(error); reject(); return; } resolve(playlist.items); }); }); }; TNSSpotifyPlaylist.PLAYLISTS_FROM_RESULTS = function (results) { return new Promise(function (resolve, reject) { var items = []; var cnt = 0; try { if (results && results.items) { var itemNSArray = results.items; cnt = itemNSArray.count; if (cnt > 0) { for (var i = 0; i < cnt; i++) { var playlistObj = itemNSArray.objectAtIndex(i); var playlist = { uri: playlistObj.uri.absoluteString, name: playlistObj.name, tracks: [], playing: false }; console.log("playlist name: " + playlist.name); items.push(playlist); } } } } catch (err) { console.log("TNSSpotifyPlaylist.PLAYLISTS_FROM_RESULTS error:"); console.log(err); } cnt = 0; var advance = function () { cnt++; if (cnt < items.length) { getTracks(); } else { resolve(items); } }; var getTracks = function () { var playlistItem = items[cnt]; console.log("Getting playlist snapshot with uri: " + playlistItem.uri); SPTPlaylistSnapshot.playlistWithURIAccessTokenCallback(NSURL.URLWithString(playlistItem.uri), auth_1.TNSSpotifyAuth.SESSION.accessToken, function (error, playlistSnapshot) { if (error != null) { console.log("*** SPTPlaylistSnapshot.playlistWithURIAccessTokenCallback got error:"); console.log(error); advance(); return; } var trackList = playlistSnapshot.firstTrackPage; TNSSpotifyPlaylist.GET_TRACKS(trackList, playlistItem).then(advance, advance); }); }; if (items.length) { getTracks(); } else { resolve([]); } }); }; TNSSpotifyPlaylist.GET_TRACKS = function (trackList, playlist) { return new Promise(function (resolve, reject) { var currentPage = trackList; console.log("GET_TRACKS ----"); playlist.tracks = search_1.TNSSpotifySearch.TRACKS_FROM_RESULTS(currentPage); console.log("playlist: " + playlist.name); console.log("playlist.tracks.length: " + playlist.tracks.length); var fetchNextPage = function () { if (currentPage.hasNextPage) { currentPage.requestNextPageWithAccessTokenCallback(auth_1.TNSSpotifyAuth.SESSION.accessToken, function (error, nextTrackList) { if (error != null) { console.log("*** playlist requestNextPageWithAccessTokenCallback got error:"); console.log(error); reject(); return; } console.log("got next track page ----"); currentPage = nextTrackList; var tracks = search_1.TNSSpotifySearch.TRACKS_FROM_RESULTS(currentPage); for (var _i = 0, tracks_1 = tracks; _i < tracks_1.length; _i++) { var t = tracks_1[_i]; console.log(t); } playlist.tracks = playlist.tracks.concat(tracks); fetchNextPage(); }); } else { resolve(); } }; fetchNextPage(); }); }; return TNSSpotifyPlaylist; }()); exports.TNSSpotifyPlaylist = TNSSpotifyPlaylist; //# sourceMappingURL=playlist.js.map