Ejemplo n.º 1
0
        public async Task <List <SuggestedContribution> > GetSuggestedContributionsAsync(PartyGoer partier, List <string> excludedIds = null)
        {
            if (excludedIds == null)
            {
                excludedIds = new List <string>();
            }

            bool newSuggestionExists = false;
            Task <List <SpotibroModels.Track> >  tracksTask;
            Task <List <SpotibroModels.Artist> > artistsTask;

            List <SpotibroModels.Track>  uniqueTracks  = null;
            List <SpotibroModels.Artist> uniqueArtists = null;

            // How about some nice lil API calls in a while loop :o
            do
            {
                // get items from multiple sources and return that to the client
                //Task<List<SpotibroModels.PlaylistSummary>> playlistsTask = GetUsersPlaylistsAsync(partier, 10);
                tracksTask  = _spotifyHttpClient.GetUserTopTracksAsync(partier.GetSpotifyId(), 15);
                artistsTask = _spotifyApi.GetUsersTopArtistsAsync(partier, 15);

                await Task.WhenAll(artistsTask, tracksTask);

                // Lets make sure we got some results
                foreach (var excludedId in excludedIds)
                {
                    tracksTask.Result.RemoveAll(p => p.Id == excludedId);
                    artistsTask.Result.RemoveAll(p => p.Id == excludedId);
                }

                if (tracksTask.Result.Count + artistsTask.Result.Count > 0)
                {
                    uniqueArtists       = artistsTask.Result.Distinct(new ArtistComparer()).ToList();
                    uniqueTracks        = tracksTask.Result.Distinct(new TrackComparer()).ToList();
                    newSuggestionExists = true;
                }
            } while (!newSuggestionExists);

            List <SuggestedContribution> contributions = new List <SuggestedContribution>();

            lock (_randomLock)
            {
                for (int i = 0; i < 2; i++)
                {
                    SpotibroModels.Track track = uniqueTracks.ElementAt(_random.Next(0, uniqueTracks.Count - 1));

                    contributions.Add(new SuggestedContribution
                    {
                        Id   = track.Id,
                        Name = $"{track.Name} - {track.Artists.First().Name}",
                        Type = ContributionType.Track
                    });
                }

                /*
                 * for (int i = 0; i < 2; i++)
                 * {
                 *  SpotibroModels.PlaylistSummary playlist = playlistsTask.Result.ElementAt(_random.Next(0, playlistsTask.Result.Count - 1));
                 *
                 *  contributions.Add(new SuggestedContribution
                 *  {
                 *      Id = playlist.Id,
                 *      Name = playlist.Name,
                 *      Type = ContributionType.Playlist
                 *  });
                 * }*/

                for (int i = 0; i < 2; i++)
                {
                    SpotibroModels.Artist artist = uniqueArtists.ElementAt(_random.Next(0, uniqueArtists.Count - 1));

                    contributions.Add(new SuggestedContribution
                    {
                        Id   = artist.Id,
                        Name = artist.Name,
                        Type = ContributionType.Artist
                    });
                }
            }

            return(contributions);
        }