예제 #1
0
        public async Task <ActionResult> Playlists(PlaylistSummaryViewModel vm, string id)
        {
            ServicePointManager.DefaultConnectionLimit = 5;

            var userId       = id;
            var access_token = Session["AccessToken"].ToString();
            var sh           = new SpotifyHelper();

            // Create/Update playlist in Spotify
            // like /Playlists/Follow

            // Does the playlist exist already for this user?
            var    url4 = String.Format("https://api.spotify.com/v1/users/{0}/playlists", userId);
            string result4;

            using (mp.Step("POST - Does the DTM Shuffler playlist exist already for this user")) {
                result4 = sh.CallSpotifyAPIPassingToken(access_token, url4);
            }
            var meReponse         = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(result4);
            var currentPlaylistID = "";
            var shuffler          = meReponse.items.FirstOrDefault(x => x.name == "DTM - Shuffler");

            if (shuffler != null)
            {
                currentPlaylistID = shuffler.id;
            }

            // If not playlist create one
            if (currentPlaylistID == "")
            {
                var    url2 = String.Format("https://api.spotify.com/v1/users/{0}/playlists", userId);
                string result2;
                using (mp.Step("POST - Creating DTM - Shuffler playlist")) {
                    result2 = sh.CallSpotifyCreatePlaylistPostAPIPassingToken(access_token, url2, "DTM - Shuffler");
                }
                var playlistReturn = JsonConvert.DeserializeObject <CreatePlaylistReturn>(result2);
                currentPlaylistID = playlistReturn.id;
            }

            // Go through each Checked playlist and add to Shuffler list
            var listOfTrackIDs = new List <String>();

            foreach (var playlist in vm.items)
            {
                var ownerId    = playlist.owner.id;
                var playlistId = playlist.id;
                if (playlist.Checked)
                {
                    // Get the details of the playlist ie the tracks
                    PlaylistTracks result22;
                    using (mp.Step("POST - Async.. Get details of the playlist ie the tracks.. 50 at a time")) {
                        result22 = await sh.CallSpotifyAPIPassingTokenPlaylistsAsync(access_token, ownerId, playlistId);
                    }
                    // add tracks to list
                    foreach (var item in result22.items)
                    {
                        // catching a track in a playlist with no id
                        if (item.track != null)
                        {
                            listOfTrackIDs.Add(item.track.id);
                        }
                    }
                }
            }

            var result3 = await sh.CallSpotifyPutAPIPassingTokenSendTracksAsync(access_token, userId, currentPlaylistID, listOfTrackIDs);

            // Get data again as not saved, including Checked status
            var vm2 = await GetPlaylistDetailsViewModel(id);

            return(View(vm2));
        }