Exemplo n.º 1
0
        public bool Lookup()
        {
            if (!OnlineMetadataServiceJob.TrackConditionsMet(Track))
            {
                return(false);
            }

            string artwork_id = Track.ArtworkId;

            if (artwork_id == null)
            {
                return(false);
            }
            else if (CoverArtSpec.CoverExists(artwork_id))
            {
                return(false);
            }
            else if (!InternetConnected)
            {
                return(false);
            }

            DatabaseTrackInfo dbtrack;

            dbtrack = Track as DatabaseTrackInfo;

            Release release;

            // If we have the MBID of the album, we can do a direct MusicBrainz lookup
            if (dbtrack != null && dbtrack.AlbumMusicBrainzId != null)
            {
                release = Release.Get(dbtrack.AlbumMusicBrainzId);
                if (!String.IsNullOrEmpty(release.GetAsin()) && SaveCover(String.Format(AmazonUriFormat, release.GetAsin())))
                {
                    return(true);
                }

                // Otherwise we do a MusicBrainz search
            }
            else
            {
                ReleaseQueryParameters parameters = new ReleaseQueryParameters();
                parameters.Title  = Track.AlbumTitle;
                parameters.Artist = Track.AlbumArtist;
                if (dbtrack != null)
                {
                    parameters.TrackCount = dbtrack.TrackCount;
                }

                Query <Release> query = Release.Query(parameters);
                release = query.PerfectMatch();

                foreach (Release r in query.Best())
                {
                    if (!String.IsNullOrEmpty(r.GetAsin()) && SaveCover(String.Format(AmazonUriFormat, r.GetAsin())))
                    {
                        return(true);
                    }
                }
            }

            if (release == null)
            {
                return(false);
            }

            // No success with ASIN, let's try with other linked URLs
            ReadOnlyCollection <UrlRelation> relations = release.GetUrlRelations();

            foreach (UrlRelation relation in relations)
            {
                foreach (CoverArtSite site in CoverArtSites)
                {
                    Match m = site.Regex.Match(relation.Target.AbsoluteUri);
                    if (m.Success)
                    {
                        string [] parameters = new string [m.Groups.Count];
                        for (int i = 1; i < m.Groups.Count; i++)
                        {
                            parameters[i - 1] = m.Groups[i].Value;
                        }

                        String uri = String.Format(site.ImgURI, parameters);
                        if (SaveCover(uri))
                        {
                            return(true);
                        }
                    }
                }

                if (relation.Type == "CoverArtLink" && SaveCover(relation.Target.AbsoluteUri))
                {
                    return(true);
                }
            }

            return(false);
        }