예제 #1
0
        /// <summary>
        /// Return if artist exists in repository, if not fetch from MusicBrainz and populate then return
        /// </summary>
        /// <param name="name">Query name of Artist</param>
        /// <param name="resultsCount">Maximum Number of Results</param>
        public async Task <Artist> ArtistByName(string name, int?resultsCount = null)
        {
            Artist result = null;

            try
            {
                using (var db = new LiteDatabase(FileName))
                {
                    var col = db.GetCollection <RepositoryArtist>("artists");
                    col.EnsureIndex(x => x.ArtistName);
                    col.EnsureIndex(x => x.ArtistMbId);
                    var artist = col.Find(x => x.ArtistName.Equals(name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                    if (artist == null)
                    {
                        // Perform a query to get the MbId for the Name
                        var artistResult = await MusicBrainzRequestHelper.GetAsync <ArtistResult>(MusicBrainzRequestHelper.CreateSearchTemplate("artist", name, resultsCount ?? 1, 0));

                        if (artistResult == null || artistResult.artists == null || !artistResult.artists.Any() || artistResult.count < 1)
                        {
                            return(null);
                        }
                        var mbId = artistResult.artists.First().id;
                        // Now perform a detail request to get the details by the MbId
                        result = await MusicBrainzRequestHelper.GetAsync <Artist>(MusicBrainzRequestHelper.CreateLookupUrl("artist", mbId, "aliases+tags+genres+url-rels"));

                        if (result != null)
                        {
                            col.Insert(new RepositoryArtist
                            {
                                ArtistName = name,
                                ArtistMbId = result.id,
                                Artist     = result
                            });
                        }
                    }
                    else
                    {
                        result = artist.Artist;
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                Logger.LogTrace($"MusicBrainzArtist: ArtistName [{ name }], HttpRequestException: [{ ex.ToString() }] ");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
            return(result);
        }
예제 #2
0
        public async Task <OperationResult <IEnumerable <ArtistSearchResult> > > PerformArtistSearch(string query, int resultsCount)
        {
            ArtistSearchResult result = null;

            try
            {
                this.Logger.LogTrace("MusicBrainzProvider:PerformArtistSearch:{0}", query);
                // Find the Artist
                var artistCacheKey = string.Format("uri:musicbrainz:ArtistSearchResult:{0}", query);
                result = this.CacheManager.Get <ArtistSearchResult>(artistCacheKey);
                if (result == null)
                {
                    ArtistResult artistResult = null;
                    try
                    {
                        artistResult = await MusicBrainzRequestHelper.GetAsync <ArtistResult>(MusicBrainzRequestHelper.CreateSearchTemplate("artist", query, resultsCount, 0));
                    }
                    catch (Exception ex)
                    {
                        this.Logger.LogError(ex);
                    }
                    if (artistResult == null || artistResult.artists == null || artistResult.count < 1)
                    {
                        return(new OperationResult <IEnumerable <ArtistSearchResult> >());
                    }
                    var a        = artistResult.artists.First();
                    var mbArtist = await MusicBrainzRequestHelper.GetAsync <Artist>(MusicBrainzRequestHelper.CreateLookupUrl("artist", artistResult.artists.First().id, "releases"));

                    if (mbArtist == null)
                    {
                        return(new OperationResult <IEnumerable <ArtistSearchResult> >());
                    }
                    result = new ArtistSearchResult
                    {
                        ArtistName     = mbArtist.name,
                        ArtistSortName = mbArtist.sortname,
                        MusicBrainzId  = mbArtist.id,
                        ArtistType     = mbArtist.type,
                        IPIs           = mbArtist.ipis,
                        ISNIs          = mbArtist.isnis
                    };
                    if (mbArtist.lifespan != null)
                    {
                        result.BeginDate = SafeParser.ToDateTime(mbArtist.lifespan.begin);
                        result.EndDate   = SafeParser.ToDateTime(mbArtist.lifespan.end);
                    }
                    if (a.aliases != null)
                    {
                        result.AlternateNames = a.aliases.Select(x => x.name).Distinct().ToArray();
                    }
                    if (a.tags != null)
                    {
                        result.Tags = a.tags.Select(x => x.name).Distinct().ToArray();
                    }
                    var mbFilteredReleases = new List <Release>();
                    var filteredPlaces     = new List <string> {
                        "US", "WORLDWIDE", "XW", "GB"
                    };
                    foreach (var release in mbArtist.releases)
                    {
                        if (filteredPlaces.Contains((release.country ?? string.Empty).ToUpper()))
                        {
                            mbFilteredReleases.Add(release);
                        }
                    }
                    result.Releases = new List <ReleaseSearchResult>();
                    var bag = new ConcurrentBag <Release>();
                    var filteredReleaseDetails = mbFilteredReleases.Select(async release =>
                    {
                        bag.Add(await this.MusicBrainzReleaseById(release.id));
                    });
                    await Task.WhenAll(filteredReleaseDetails);

                    foreach (var mbRelease in bag.Where(x => x != null))
                    {
                        var release = new ReleaseSearchResult
                        {
                            MusicBrainzId       = mbRelease.id,
                            ReleaseTitle        = mbRelease.title,
                            ReleaseThumbnailUrl = mbRelease.coverThumbnailUrl
                        };
                        if (mbRelease.imageUrls != null)
                        {
                            release.ImageUrls = mbRelease.imageUrls;
                        }
                        if (mbRelease.releaseevents != null)
                        {
                            release.ReleaseDate = SafeParser.ToDateTime(mbRelease.releaseevents.First().date);
                        }
                        // Labels
                        if (mbRelease.media != null)
                        {
                            var releaseMedias = new List <ReleaseMediaSearchResult>();
                            foreach (var mbMedia in mbRelease.media)
                            {
                                var releaseMedia = new ReleaseMediaSearchResult
                                {
                                    ReleaseMediaNumber = SafeParser.ToNumber <short?>(mbMedia.position),
                                    TrackCount         = mbMedia.trackcount
                                };
                                if (mbMedia.tracks != null)
                                {
                                    var releaseTracks = new List <TrackSearchResult>();
                                    foreach (var mbTrack in mbMedia.tracks)
                                    {
                                        releaseTracks.Add(new TrackSearchResult
                                        {
                                            MusicBrainzId = mbTrack.id,
                                            TrackNumber   = SafeParser.ToNumber <short?>(mbTrack.number),
                                            Title         = mbTrack.title,
                                            Duration      = mbTrack.length
                                        });
                                    }
                                    releaseMedia.Tracks = releaseTracks;
                                }
                                releaseMedias.Add(releaseMedia);
                            }
                            release.ReleaseMedia = releaseMedias;
                        }

                        result.Releases.Add(release);
                    }
                    ;
                    this.CacheManager.Add(artistCacheKey, result);
                }
            }
            catch (HttpRequestException)
            {
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ex);
            }
            if (result == null)
            {
                this.Logger.LogWarning("MusicBrainzArtist: ArtistName [{0}], No MusicBrainz Artist Found", query);
            }
            else
            {
                this.Logger.LogTrace("MusicBrainzArtist: Result [{0}]", query, result.ToString());
            }
            return(new OperationResult <IEnumerable <ArtistSearchResult> >
            {
                IsSuccess = result != null,
                Data = new ArtistSearchResult[] { result }
            });
        }