Ejemplo n.º 1
0
        private ArtistDetailsViewModel GetArtistDetailsViewModel(string id)
        {
            string artistID = id;
            var    sh       = new SpotifyHelper();

            // 1. Getting Artist details from Spotify
            string json;

            using (profiler.Step("1. Getting Artist details from Spotify")) {
                json = sh.CallSpotifyAPIArtist(artistID, null);
            }
            ViewBag.Id = artistID;
            var artistDetails = JsonConvert.DeserializeObject <ArtistDetails>(json);

            // 2. All Artists albums - possibly more than 50!
            APIResult apiResult;

            using (profiler.Step("2. All Artists albums - possibly more than 50!")) {
                apiResult = sh.CallSpotifyAPIArtistAlbums(null, artistID);
            }
            var artistAlbums = JsonConvert.DeserializeObject <ArtistAlbums>(apiResult.Json);

            // Want studio albums only - get rid of live albums and put into another bucket to display too
            IEnumerable <ArtistAlbums.Item> studioAlbums = artistAlbums.items.Where(x => !x.name.ToLower().Contains("live from"));

            studioAlbums = studioAlbums.Where(x => !x.name.ToLower().Contains("live at"));
            studioAlbums = studioAlbums.Where(x => !x.name.ToLower().Contains("(live"));
            studioAlbums = studioAlbums.Where(x => !x.name.ToLower().Contains("live in"));
            studioAlbums = studioAlbums.Where(x => !x.name.ToLower().Contains("greatest hits"));

            // get rid of any dupes - eg Phil Collins has many albums called Tarzan....could use DistinctBy
            studioAlbums = studioAlbums.GroupBy(x => x.name).Select(yy => yy.First());

            //Queen
            //if contains   (Deluxe
            //  or Deluxe
            // take start of the string before that…
            // and check another album exists
            var deluxeAlbums = studioAlbums.Where(x => x.name.ToLower().Contains("(deluxe"));

            // check another album exists
            foreach (var deluxeAlbum in deluxeAlbums)
            {
                var name = deluxeAlbum.name;
                //var firstBit = name.Substring(0)
                string[] res      = name.Split(new string[] { "(Deluxe" }, StringSplitOptions.None);
                var      firstBit = res[0];

                // does another album exist with this name but not the deluxe?
                var anohterAlbum = studioAlbums.FirstOrDefault(x => x.name.ToLower().Trim() == firstBit.ToLower().Trim());
                if (anohterAlbum != null)
                {
                    studioAlbums = studioAlbums.Where(x => x.id != anohterAlbum.id);
                }

                // does another album exist with this name but stuff in brackets
                // eg Queen (Deluxe Edition..) and Queen (2011 Remaster)
                var anohterAlbum2 = studioAlbums.FirstOrDefault(x => x.name.ToLower().Trim().Contains(firstBit.ToLower().Trim() + " ("));
                if (anohterAlbum2 != null)
                {
                    studioAlbums = studioAlbums.Where(x => x.id != anohterAlbum2.id);
                }
            }

            List <MultipleAlbums.Album> multiAlbumDetails3 = null;

            // The exponents - have no albums (even before filtering)
            if (studioAlbums.Count() > 0)
            {
                multiAlbumDetails3 = sh.CallSpotifyAPIMultipleAlbumDetails2(null, studioAlbums);
            }

            // 3. Artists Top Tracks
            using (profiler.Step("3. Artists Top Tracks")) {
                apiResult = sh.CallSpotifyAPIArtistTopTracks(null, artistID);
            }
            var artistTopTracks = JsonConvert.DeserializeObject <ArtistTopTracks>(apiResult.Json);

            var tracks        = artistTopTracks.tracks;
            var distinctTop10 = new List <ArtistTopTracks.Track>();
            List <ArtistTopTracks.Track> top10 = tracks.OrderByDescending(x => x.popularity).Take(10).ToList();

            // problem is that some tracks are related to their "Greatest Hits compilation" / not original album
            // eg Black Crowes - Twice as Hard (#2) related to Greatest Hits 1990-1999
            // take the track name, and find the earliest album it is on..
            IList <string> top10trackNames         = top10.Select(x => x.name).ToList();
            var            top10trackNamesDistinct = top10trackNames.Distinct(StringComparer.CurrentCultureIgnoreCase);

            foreach (var trackName in top10trackNamesDistinct)
            {
                var track = top10.FirstOrDefault(x => x.name == trackName);
                distinctTop10.Add(track);
            }

            var dictionaryOfNameAndAlbumIDAndNewTrackID = new Dictionary <string, Thing2>();

            // find track name in the earliest album
            foreach (var trackName in top10trackNamesDistinct)
            {
                MultipleAlbums.Album originalAlbum = null;

                // if there are any remaining albums for this artist (eg Brian May)
                if (multiAlbumDetails3 != null)
                {
                    var xxx = multiAlbumDetails3.Select(x => x.tracks.items.Where(yy => yy.name == trackName));
                    // Descending so last one, is the earliest one
                    var listOfTrackName = new List <Thing2>();
                    foreach (var album in multiAlbumDetails3.OrderByDescending(al => al.release_date))
                    {
                        foreach (var track in album.tracks.items)
                        {
                            if (track.name.ToLower() == trackName.ToLower())
                            {
                                originalAlbum = album;
                            }
                            var t2 = new Thing2 {
                                AlbumID = album.id,
                                TrackID = track.id
                            };
                            listOfTrackName.Add(t2);
                        }
                    }
                }

                if (originalAlbum == null)
                {
                    // Eminem - guts over fear.. on album Shadyyxv (a compilation)
                    var asdf = distinctTop10.SingleOrDefault(x => x.name == trackName);
                    var t2   = new Thing2 {
                        AlbumID = asdf.album.id,
                        TrackID = asdf.id
                    };
                    dictionaryOfNameAndAlbumIDAndNewTrackID.Add(trackName, t2);
                }
                else
                {
                    // add the albums trackID rather than whatever it is at the moment (compilation perhaps)
                    // want the last one added to the list
                    var t2 = new Thing2 {
                        AlbumID = originalAlbum.id,
                        TrackID = originalAlbum.tracks.items.FirstOrDefault(x => x.name.ToLower() == trackName.ToLower()).id
                    };
                    dictionaryOfNameAndAlbumIDAndNewTrackID.Add(trackName, t2);
                }
            }

            // Wire up new album if needed
            foreach (var track in distinctTop10)
            {
                var trackName = track.name;
                track.album.id = dictionaryOfNameAndAlbumIDAndNewTrackID[trackName].AlbumID;
                track.id       = dictionaryOfNameAndAlbumIDAndNewTrackID[trackName].TrackID;
            }

            // Iterate through records in db, setting vm checked property for Admin - add to playlist
            var trackIDsTemp = new List <string>();

            using (IDbConnection db = DBHelper.GetOpenConnection()) {
                trackIDsTemp = db.Query <string>("SELECT TrackID FROM Tracks").ToList();
            }

            foreach (var trackID in trackIDsTemp)
            {
                // Is this track in the current search list?
                var track = distinctTop10.FirstOrDefault(x => x.id == trackID);
                if (track != null)
                {
                    track.Checked = true;
                }
            }

            // 4. Artists top tracks and get the date that this track/album was released - using GetMultipleAlbums
            var csvStringOfAlbumIDs = "";
            var listOfAlbumIDs      = new List <string>();

            foreach (var track in distinctTop10)
            {
                if (listOfAlbumIDs.Contains(track.album.id))
                {
                }
                else
                {
                    csvStringOfAlbumIDs += track.album.id + ",";
                    listOfAlbumIDs.Add(track.album.id);
                }
            }
            csvStringOfAlbumIDs = csvStringOfAlbumIDs.TrimEnd(',');
            APIResult apiResult2;

            if (csvStringOfAlbumIDs.Length > 0)
            {
                using (profiler.Step("4. Artists top tracks and get the date that this track/album was released - using GetMultipleAlbums")) {
                    apiResult2 = sh.CallSpotifyAPIMultipleAlbumDetails(null, csvStringOfAlbumIDs);
                }
                var multiAlbumDetails = JsonConvert.DeserializeObject <MultipleAlbums>(apiResult2.Json);
                foreach (var album in multiAlbumDetails.albums)
                {
                    // get all associated albums in top10
                    var albumInTop10s = distinctTop10.Where(x => x.album.id == album.id);

                    foreach (var album2 in albumInTop10s)
                    {
                        album2.album.DateOfAlbumRelease = album.release_date;
                        album2.album.name = album.name;
                        album2.album.uri  = album.uri;

                        //images
                        var images = new List <ArtistTopTracks.Image>();
                        foreach (var image in album.images)
                        {
                            var att = new ArtistTopTracks.Image();
                            att.height = image.height;
                            att.width  = image.width;
                            att.url    = image.url;
                            images.Add(att);
                        }
                        album2.album.images = images;
                    }
                }
            }

            artistTopTracks.tracks = distinctTop10.ToList();

            // 5. Artist's related Artists - top 15
            using (profiler.Step("5. Artist's related Artists - top 15")) {
                apiResult = sh.CallSpotifyAPIArtistRelated(null, artistID);
            }
            var artistRelated = JsonConvert.DeserializeObject <ArtistRelated>(apiResult.Json);
            var y             = artistRelated.artists.Take(15).ToList();

            artistRelated.artists = y;


            // 6. Biography from Wikipedia via Echonest - deprecated!
            //using (profiler.Step("6. Biography (Echonest)")) {
            //    apiResult = sh.CallEchonestAPIArtistBiography(null, artistID);
            //}
            //var artistBiography = JsonConvert.DeserializeObject<ArtistBiography>(apiResult.Json);
            //if (artistBiography != null) {
            //    // Just get last.fm and Wikipedia entries
            //    if (artistBiography.response.biographies != null) {
            //        var a = artistBiography.response.biographies.SingleOrDefault(x => x.url.Contains("wikipedia"));
            //        artistBiography.response.biographies.Clear();
            //        artistBiography.response.biographies.Add(a);
            //    }
            //}

            // 7. Artists singles
            using (profiler.Step("7. Artists singles")) {
                apiResult = sh.CallSpotifyAPIArtistsSingles(artistID);
            }
            var artistSingles = JsonConvert.DeserializeObject <ArtistAlbums>(apiResult.Json);

            // use the query I did above
            foreach (var trackID in trackIDsTemp)
            {
                // Is this track in the current artist Singles list?
                var track = artistSingles.items.FirstOrDefault(x => x.id == trackID);
                if (track != null)
                {
                    track.Checked = true;
                }
            }

            var vm = new ArtistDetailsViewModel {
                ArtistDetails   = artistDetails,
                ArtistTopTracks = artistTopTracks,
                ArtistAlbums    = multiAlbumDetails3,

                ArtistRelated = artistRelated,
                //ArtistBiography = artistBiography,
                ArtistSingles = artistSingles
            };

            return(vm);
        }