//Method for returning artists from the users playlists public List <string> GetArtists(Playlists playlists, string token) { List <string> Artists = new List <string>(); foreach (var playlist in playlists.Items) { string url = string.Format("https://api.spotify.com/v1/users/" + playlist.Owner.UserId + "/playlists/" + playlist.Id + "/tracks"); Tracks tracks = SpotifyService <Tracks>(url, token); if (tracks == null) { continue; } foreach (var track in tracks.Items) { foreach (var artist in track.FullTrack.Artists) { Artists.Add(artist.Name); } } } //Return the list of artists return(Artists); }
//method for getting User playlist information from this the users favourite artists can be found public Playlists GetPlaylists(string UserId, string token) { string url = string.Format("https://api.spotify.com/v1/users/{0}/playlists", UserId); //URL to access the spotify api Playlists playlists = SpotifyService <Playlists>(url, token); return(playlists); }
//This is an alternative method which gets the latest artist release if it is within the pas month //This method returns much more results however it requires a lot of calls to the spotify API //due to spotifys rate limiting on api calls this is a very time consuming but it returns more detailed results //The method can be changed to get more parameters from the JSON data as per GetNewReleases() above public List <string> GetReleasesFromAlubms(Playlists playlists, string token) { //Make a list of new releases List <string> Releases = new List <string>(); foreach (var playlist in playlists.Items) { //Get all tracks from the playlists string url = string.Format("https://api.spotify.com/v1/users/" + playlist.Owner.UserId + "/playlists/" + playlist.Id + "/tracks"); Tracks tracks = SpotifyService <Tracks>(url, token); if (tracks == null) { continue; } foreach (var track in tracks.Items) { string music = track.FullTrack.Name; string artists = ""; //From the tracks get the artist, the artist will then be checked for album releases foreach (var artist in track.FullTrack.Artists) { //Current date to use as a comparator DateTime dt1 = DateTime.Now; string url1 = string.Format("https://api.spotify.com/v1/artists/" + artist.Id + "/albums?market=ES&include_groups=album,single,appears_on,compilation&limit=2"); Releases releases = SpotifyService <Releases>(url1, token); if (releases == null) { continue; } foreach (var album in releases.Albums) { string date = album.Date; //To deal with apotify returning only the year release if (date.Length < 9) { continue; } DateTime dt2 = DateTime.ParseExact(date, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); //Get number of days between 2 dates if it is less than 30 days it can be considered a new release var result = (dt1 - dt2).Days; if (result < 30) { Releases.Add(album.Name); } } } } } return(Releases); }
//Redirect from authorisation call public ActionResult Auth(string access_token, string token_type, string expires_in, string state) { if (string.IsNullOrEmpty(access_token)) { return(View()); } //Create a new service for making spotify api calls SpotifyAPI spotifyService = new SpotifyAPI(); //Get user authentication and return name to client display UserProfile spotifyUser = spotifyService.GetUserProfile(access_token); //pass in the authentication token provided by spotify ViewBag.Users = spotifyUser.DisplayName; //Get user playlists ids Playlists playlists = spotifyService.GetPlaylists(spotifyUser.UserId, access_token); ViewBag.Playlists = playlists; //Get all tracks from user List <string> Artists = spotifyService.GetArtists(playlists, access_token); ViewBag.Artists = Artists; //Make a call to a method which returns new releases in the form of 3 seperate variables var(l1, l2, l3) = spotifyService.GetNewReleases(Artists, access_token); ViewBag.Url = l1; ViewBag.Album = l2; ViewBag.Artwork = l3; //Ticketmaster service for checking if any of the users artists have any upcoming concert dates TicketmasterAPI ticketmasterService = new TicketmasterAPI(); //Return url link to event and event name var(url, name) = ticketmasterService.GetConcerts(Artists); ViewBag.Url1 = url; ViewBag.Name = name; //Return the app html file, Auth.cshtml return(View()); }