Beispiel #1
0
        public override void Run()
        {
            if (!OnlineMetadataServiceJob.TrackConditionsMet(Track) ||
                GetAlbumUrl(Track) == null)
            {
                return;
            }

            string artwork_id = Track.ArtworkId;

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

            Uri data_uri = new Uri(base_uri, String.Format("/{0}/data.xml", GetAlbumUrl(Track)));

            XmlDocument     doc      = new XmlDocument();
            HttpWebResponse response = GetHttpStream(data_uri);

            if (response == null)
            {
                return;
            }

            string [] content_types = response.Headers.GetValues("Content-Type");
            if (content_types.Length == 0 || content_types[0] != "text/xml")
            {
                response.Close();
                return;
            }

            using (Stream stream = response.GetResponseStream()) {
                doc.Load(stream);
            }

            XmlNode art_node = doc.DocumentElement.SelectSingleNode("/album/art/album-art[@size='large']/img");

            if (art_node != null && art_node.Attributes["src"] != null)
            {
                // awesome hack to get high resolution cover art from Rhapsody
                string second_attempt = art_node.Attributes["src"].Value;
                string first_attempt  = second_attempt.Replace("170x170", "500x500");

                if (SaveHttpStreamCover(new Uri(first_attempt), artwork_id, null) ||
                    SaveHttpStreamCover(new Uri(second_attempt), artwork_id, null))
                {
                    Log.Debug("Downloaded cover art from Rhapsody", artwork_id);
                    StreamTag tag = new StreamTag();
                    tag.Name  = CommonTags.AlbumCoverId;
                    tag.Value = artwork_id;

                    AddTag(tag);
                }
            }

            response.Close();
        }
Beispiel #2
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);
        }
Beispiel #3
0
        public override void Run()
        {
            if (!OnlineMetadataServiceJob.TrackConditionsMet(Track))
            {
                return;
            }

            string artwork_id = Track.ArtworkId;

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

            // Lastfm uses double url-encoding in their current 1.2 api for albuminfo.
            string lastfmArtist = HttpUtility.UrlEncode(HttpUtility.UrlEncode(Track.AlbumArtist));
            string lastfmAlbum  = HttpUtility.UrlEncode(HttpUtility.UrlEncode(Track.AlbumTitle));

            LastfmAlbumData album = null;

            try {
                album = new LastfmAlbumData(lastfmArtist, lastfmAlbum);
            } catch {
                return;
            }

            // AllUrls is an array with [small,medium,large] coverart url
            string [] album_cover_urls = album.AlbumCoverUrls.AllUrls();

            // Select the URL for the coverart with highest resolution
            string best_url = String.Empty;

            foreach (string url in album_cover_urls)
            {
                if (!String.IsNullOrEmpty(url))
                {
                    best_url = url;
                }
            }

            // No URL's found
            if (String.IsNullOrEmpty(best_url) || best_url.Contains("noimage"))
            {
                //string upload_url = String.Format ("http://www.last.fm/music/{0}/{1}/+images", lastfmArtist, lastfmAlbum);
                //Log.DebugFormat ("No coverart provided by lastfm. (you can upload it here: {0}) - {1} ", upload_url, Track.ArtworkId);
                return;
            }

            // Hack: You can get higher resolution artwork by replacing 130X130 with 300x300 in lastfm hosted albumart
            string high_res_url = null;

            if (best_url.Contains("130x130"))
            {
                high_res_url = best_url.Replace("130x130", "300x300");
            }

            // Hack: You can get higher resolution artwork from Amazon too (lastfm sometimes uses amazon links)
            if (best_url.Contains("MZZZZZZZ"))
            {
                high_res_url = best_url.Replace("MZZZZZZZ", "LZZZZZZZ");
            }

            // Download the cover
            try {
                if ((high_res_url != null && SaveHttpStreamCover(new Uri(high_res_url), artwork_id, null)) ||
                    SaveHttpStreamCover(new Uri(best_url), artwork_id, null))
                {
                    if (best_url.Contains("amazon"))
                    {
                        Log.Debug("Downloaded cover art from Amazon", artwork_id);
                    }
                    else
                    {
                        Log.Debug("Downloaded cover art from Lastfm", artwork_id);
                    }

                    StreamTag tag = new StreamTag();
                    tag.Name  = CommonTags.AlbumCoverId;
                    tag.Value = artwork_id;
                    AddTag(tag);
                }
            } catch (Exception e) {
                Log.Error("Cover art found on Lastfm, but downloading it failed. Probably server under high load or dead link", e);
            }
        }