Exemple #1
0
        public async Task <List <Album> > GetAlbumsByName(string artistName)
        {
            var newArtistName    = artistName.TrimStartAndEnd().Replace(" ", "+").ToLowerInvariant();
            var cryptoArtistName = CryptoStringHelper.GetEncodeString(newArtistName);

            try
            {
                var artistId = await GetArtistIdByName(newArtistName);

                var albumResults = await _manager.GetAlbumsByArtistIdAsync(artistId, countryCode : Constants.CODE_RU);

                var artistCacheData = new ArtistCacheData(albumResults.Albums.Select(x => x.CollectionName).ToArray());
                CacheHelper.Set(cryptoArtistName, artistCacheData.Albums);

                return(albumResults.Albums);
            }
            catch (HttpErrorStatusCodeException ex)
            {
                var cacheData = CacheHelper.Get <string[]>(cryptoArtistName);
                return(cacheData.Select(x => new Album
                {
                    CollectionName = x
                }).ToList());
            }
        }
        public void GetAlbumsByArtistId_ValidArtist_ReturnsAlbums()
        {
            // Arrange
            iTunesSearchManager search = new iTunesSearchManager();
            long artistId = 311145;

            // Act
            var items = search.GetAlbumsByArtistIdAsync(artistId, 200).Result;

            // Assert
            Assert.IsTrue(items.Albums.Any(al => al.CollectionName == "Life's Rich Pageant"));
        }
        public async Task <byte[]> DownloadArtwork(string country, string artist, string albumName)
        {
            var searchManager = new iTunesSearchManager();

            var artists = await searchManager.GetSongArtistsAsync(artist);

            var foundArtist = artists.Artists.FirstOrDefault();

            if (foundArtist is null)
            {
                return(null);
            }

            var albums = await searchManager.GetAlbumsByArtistIdAsync(foundArtist.ArtistId);

            var album = albums.Albums.FirstOrDefault(x =>
                                                     x.CollectionName != null && x.CollectionName.Contains(albumName) &&
                                                     !string.IsNullOrWhiteSpace(x.ArtworkUrl100));

            if (album != null)
            {
                var artworkUri = album.ArtworkUrl100.Replace("100x100bb", "1000x1000bb");

                var clientHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = delegate { return(true); }
                };

                using var client = new HttpClient(clientHandler);

                var data = await client.GetByteArrayAsync(artworkUri);

                return(data);
            }

            return(null);
        }
        public async Task <List <ArtworkData> > GetPossibleAlbumArt(string country, string artist, string albumName)
        {
            var searchManager = new iTunesSearchManager();

            var artists = await searchManager.GetSongArtistsAsync(artist);

            var foundArtist = artists.Artists.FirstOrDefault();

            if (foundArtist is null)
            {
                return(null);
            }

            var albums = await searchManager.GetAlbumsByArtistIdAsync(foundArtist.ArtistId);

            var options = new List <FuzzyStringComparisonOptions>
            {
                FuzzyStringComparisonOptions.UseOverlapCoefficient,
                FuzzyStringComparisonOptions.UseLongestCommonSubsequence,
                FuzzyStringComparisonOptions.UseLongestCommonSubstring
            };

            var bestMatches = albums.Albums.Where(x =>
                                                  x.CollectionName != null && albumName.ApproximatelyEquals(x.CollectionName,
                                                                                                            FuzzyStringComparisonTolerance.Strong, options.ToArray())).ToList();

            return(bestMatches.Concat(albums.Albums.Where(x => x.CollectionName != null && !bestMatches.Contains(x)))
                   .Concat(albums.Albums.Where(x => x.CollectionName is null))
                   .Where(x => !string.IsNullOrWhiteSpace(x.ArtworkUrl100))
                   .Select(x => new ArtworkData
            {
                Url = x.ArtworkUrl100.Replace("100x100bb", "600x600bb"),
                Album = x.CollectionName,
                Artist = x.ArtistName
            })
                   .Take(20).ToList());
        }