Esempio n. 1
0
        //eg /v1/users/davemateer/playlists
        public ActionResult Create()
        {
            var returnURL = "/Playlists/Create";

            var ah     = new AuthHelper();
            var result = ah.DoAuth(returnURL, this);

            if (result != null)
            {
                return(Redirect(result));
            }

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

            // Get the current users id eg davemateer
            var url7    = "https://api.spotify.com/v1/me";
            var result7 = sh.CallSpotifyAPIPassingToken(access_token, url7);

            var    meReponse7 = JsonConvert.DeserializeObject <MeResponse>(result7);
            string userId     = meReponse7.id;

            // Does the playlist exist already for this user?
            var url4              = String.Format("https://api.spotify.com/v1/users/{0}/playlists", userId);
            var result4           = sh.CallSpotifyAPIPassingToken(access_token, url4);
            var meReponse         = JsonConvert.DeserializeObject <PlaylistSummaryViewModel>(result4);
            var currentPlaylistID = "";

            foreach (var thing in meReponse.items)
            {
                if (thing.name == "DTM - Playlist")
                {
                    currentPlaylistID = thing.id;
                }
            }

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

            // Get trackID's from database to add to Spotify (the app saves the trackID's to the db before we send to spotify)
            var listOfTrackIDs = new List <String>();

            using (var connection = new SqlConnection(connectionString))
                using (var command = new SqlCommand(null, connection)) {
                    connection.Open();
                    command.CommandText = String.Format("SELECT TrackID FROM UserPlaylists WHERE UserID = @UserID");
                    command.Parameters.AddWithValue("@UserID", userId);
                    command.CommandType = System.Data.CommandType.Text;
                    using (var reader = command.ExecuteReader()) {
                        while (reader.Read())
                        {
                            var trackID = reader.GetString(reader.GetOrdinal("TrackID"));
                            listOfTrackIDs.Add(trackID);
                        }
                    }
                }

            if (listOfTrackIDs.Count > 100)
            {
                string csvOfUris = "";

                // Get first 100 tracks and put into a csv string
                var first100 = listOfTrackIDs.Take(100);
                foreach (var trackID in first100)
                {
                    csvOfUris += "spotify:track:" + trackID + ",";
                }
                csvOfUris = csvOfUris.TrimEnd(',');

                var url3 = String.Format("https://api.spotify.com/v1/users/{0}/playlists/{1}/tracks?uris={2}", userId, currentPlaylistID, csvOfUris);

                // this will replace
                var result3 = sh.CallSpotifyPutAPIPassingToken(access_token, url3);

                var recordsPerPage      = 100;
                var records             = listOfTrackIDs.Count;
                int numberOfTimesToLoop = (records + recordsPerPage - 1) / recordsPerPage;

                // 1 as we've already done the first loop (0 based)
                for (int i = 1; i < numberOfTimesToLoop; i++)
                {
                    var stuff = listOfTrackIDs.Skip(100 * i).Take(100);
                    csvOfUris = "";
                    foreach (var trackID in stuff)
                    {
                        csvOfUris += "spotify:track:" + trackID + ",";
                    }
                    csvOfUris = csvOfUris.TrimEnd(',');

                    // this will add
                    url3 = String.Format("https://api.spotify.com/v1/users/{0}/playlists/{1}/tracks?uris={2}", userId, currentPlaylistID, csvOfUris);
                    var result5 = sh.CallSpotifyPostAPIPassingToken(access_token, url3);
                }
            }
            else
            {
                string csvOfUris = "";

                var first100 = listOfTrackIDs;
                foreach (var trackID in first100)
                {
                    csvOfUris += "spotify:track:" + trackID + ",";
                }
                csvOfUris = csvOfUris.TrimEnd(',');

                var url3 = String.Format("https://api.spotify.com/v1/users/{0}/playlists/{1}/tracks?uris={2}", userId,
                                         currentPlaylistID, csvOfUris);

                // this will replace
                var result3 = sh.CallSpotifyPutAPIPassingToken(access_token, url3);
            }

            return(Redirect("/"));
        }
Esempio n. 2
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));
        }