Ejemplo n.º 1
0
        ///v1/users/{owner_id}/playlists/{playlist_id}/followers
        public ActionResult Follow(string ownerId, string playlistId)
        {
            var returnURL = "/Playlists/Follow/" + ownerId + "/" + playlistId;
            var ah        = new AuthHelper();
            var result    = ah.DoAuth(returnURL, this);

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

            var access_token = Session["AccessToken"].ToString();
            var url2         = String.Format("https://api.spotify.com/v1/users/{0}/playlists/{1}/followers", ownerId, playlistId);
            var sh           = new SpotifyHelper();
            var result2      = sh.CallSpotifyPutAPIPassingToken(access_token, url2);

            return(Redirect("/Profiles/Me"));
        }
Ejemplo n.º 2
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("/"));
        }