public ActionResult Me()
        {
            var returnURL = "/Profiles/Me";
            var ah        = new AuthHelper();
            var result    = ah.DoAuth(returnURL, this);

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

            var access_token = Session["AccessToken"].ToString();
            var url2         = "https://api.spotify.com/v1/me";
            var sh           = new SpotifyHelper();
            var result2      = sh.CallSpotifyAPIPassingToken(access_token, url2);

            var meReponse = JsonConvert.DeserializeObject <MeResponse>(result2);

            // Holder UserId in session so can turn menu on and off for login
            //Session["UserId"] = meReponse.id;

            meReponse.access_token = access_token;
            //return View(meReponse);
            // Redirect to PlaylistShuffler page
            return(Redirect("/Users/Playlists/" + meReponse.id));
        }
Beispiel #2
0
        public ActionResult LoginRedirect()
        {
            var returnURL = "/Home/TopTracks";
            var ah        = new AuthHelper();
            var result    = ah.DoAuth(returnURL, this);

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

            return(Redirect(returnURL));
        }
Beispiel #3
0
        public async Task <ActionResult> Playlists(string id, bool doAsync = true)
        {
            var returnURL = "/Users/Playlists";
            var ah        = new AuthHelper();
            var result    = ah.DoAuth(returnURL, this);

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

            var vm = await GetPlaylistDetailsViewModel(id, doAsync);

            return(View(vm));
        }
Beispiel #4
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"));
        }
Beispiel #5
0
        public ActionResult Details(string id, string userId)
        {
            var returnURL = "/Playlists/Details/" + id + "/" + userId;
            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}", userId, id);
            var sh           = new SpotifyHelper();
            var result2      = sh.CallSpotifyAPIPassingToken(access_token, url2);

            var meReponse = JsonConvert.DeserializeObject <PlaylistDetails>(result2);

            meReponse.access_token = access_token;
            return(View(meReponse));
        }
        ///v1/users/{user_id}
        public ActionResult User(string id)
        {
            var returnURL = "/Profiles/User";
            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}", id);
            var sh           = new SpotifyHelper();
            var result2      = sh.CallSpotifyAPIPassingToken(access_token, url2);

            var response = JsonConvert.DeserializeObject <UserDetails>(result2);

            response.access_token = access_token;
            return(View(response));
        }
        // Browse/NewReleases
        public ActionResult NewReleases()
        {
            var returnURL = "/Browse/NewReleases";
            var ah        = new AuthHelper();
            var result    = ah.DoAuth(returnURL, this);

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

            var access_token = Session["AccessToken"].ToString();
            var url2         = "https://api.spotify.com/v1/browse/new-releases?country=GB";
            var sh           = new SpotifyHelper();
            var result2      = sh.CallSpotifyAPIPassingToken(access_token, url2);

            var meReponse = JsonConvert.DeserializeObject <NewReleaseDetails>(result2);

            meReponse.access_token = access_token;
            return(View(meReponse));
        }
        public ActionResult FeaturedPlaylists()
        {
            var returnURL    = "/Browse/FeaturedPlaylists";
            var helper       = new AuthHelper();
            var helperResult = helper.DoAuth(returnURL, this);

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

            var access_token = Session["AccessToken"].ToString();
            var url          = "https://api.spotify.com/v1/browse/featured-playlists?country=GB";
            var sh           = new SpotifyHelper();
            var result       = sh.CallSpotifyAPIPassingToken(access_token, url);

            var meReponse = JsonConvert.DeserializeObject <FeaturedPlaylists>(result);

            meReponse.access_token = access_token;
            return(View(meReponse));
        }
Beispiel #9
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("/"));
        }