protected void AddTag(StreamTag tag)
 {
     tags.Add(tag);
 }
示例#2
0
        private bool SaveCover (string uri) {

            string artwork_id = Track.ArtworkId;

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

                AddTag (tag);
                return true;
            }
            return false;
         }
示例#3
0
        public static void TrackInfoMerge(TrackInfo track, StreamTag tag)
        {
            try {
                switch (tag.Name)
                {
                case CommonTags.Artist:
                    track.ArtistName = Choose((string)tag.Value, track.ArtistName);
                    break;

                case CommonTags.ArtistSortName:
                case CommonTags.MusicBrainzSortName:
                    track.ArtistNameSort = Choose((string)tag.Value, track.ArtistNameSort);
                    break;

                case CommonTags.Title:
                    //track.TrackTitle = Choose ((string)tag.Value, track.TrackTitle);
                    string title = Choose((string)tag.Value, track.TrackTitle);

                    // Try our best to figure out common patterns in poor radio metadata.
                    // Often only one tag is sent on track changes inside the stream,
                    // which is title, and usually contains artist and track title, separated
                    // with a " - " string.
                    if (track.IsLive && title.Contains(" - "))
                    {
                        string [] parts = Regex.Split(title, " - ");
                        track.TrackTitle = parts[1].Trim();
                        track.ArtistName = parts[0].Trim();

                        // Often, the title portion contains a postfix with more information
                        // that will confuse lookups, such as "Talk [Studio Version]".
                        // Strip out the [] part.
                        Match match = Regex.Match(track.TrackTitle, "^(.+)[ ]+\\[.*\\]$");
                        if (match.Groups.Count == 2)
                        {
                            track.TrackTitle = match.Groups[1].Value;
                        }
                    }
                    else
                    {
                        track.TrackTitle = title;
                    }
                    break;

                case CommonTags.TitleSortName:
                    track.TrackTitleSort = Choose((string)tag.Value, track.TrackTitleSort);
                    break;

                case CommonTags.Album:
                    track.AlbumTitle = Choose((string)tag.Value, track.AlbumTitle);
                    break;

                case CommonTags.AlbumSortName:
                    track.AlbumTitleSort = Choose((string)tag.Value, track.AlbumTitleSort);
                    break;

                case CommonTags.Disc:
                case CommonTags.AlbumDiscNumber:
                    int disc = (int)tag.Value;
                    track.DiscNumber = disc == 0 ? track.DiscNumber : disc;
                    break;

                case CommonTags.AlbumDiscCount:
                    int count = (int)tag.Value;
                    track.DiscCount = count == 0 ? track.DiscCount : count;
                    break;

                case CommonTags.Genre:
                    track.Genre = Choose((string)tag.Value, track.Genre);
                    break;

                case CommonTags.Composer:
                    track.Composer = Choose((string)tag.Value, track.Composer);
                    break;

                case CommonTags.Copyright:
                    track.Copyright = Choose((string)tag.Value, track.Copyright);
                    break;

                case CommonTags.LicenseUri:
                    track.LicenseUri = Choose((string)tag.Value, track.LicenseUri);
                    break;

                case CommonTags.Comment:
                    track.Comment = Choose((string)tag.Value, track.Comment);
                    break;

                case CommonTags.TrackNumber:
                    int track_number = (int)tag.Value;
                    track.TrackNumber = track_number == 0 ? track.TrackNumber : track_number;
                    break;

                case CommonTags.TrackCount:
                    track.TrackCount = (int)tag.Value;
                    break;

                case CommonTags.BeatsPerMinute:
                    track.Bpm = (int)tag.Value;
                    break;

                case CommonTags.Duration:
                    if (tag.Value is TimeSpan)
                    {
                        track.Duration = (TimeSpan)tag.Value;
                    }
                    else
                    {
                        track.Duration = new TimeSpan((uint)tag.Value * TimeSpan.TicksPerMillisecond);
                    }
                    break;

                case CommonTags.MoreInfoUri:
                    track.MoreInfoUri = (SafeUri)tag.Value;
                    break;

                /* No year tag in GST it seems
                 * case CommonTags.Year:
                 *  track.Year = (uint)tag.Value;
                 *  break;*/
                case CommonTags.NominalBitrate:
                    track.BitRate = (int)tag.Value;
                    break;

                case CommonTags.StreamType:
                    track.MimeType = (string)tag.Value;
                    break;

                case CommonTags.VideoCodec:
                    if (tag.Value != null)
                    {
                        track.MediaAttributes |= TrackMediaAttributes.VideoStream;
                    }
                    break;

                    /*case CommonTags.AlbumCoverId:
                     *  foreach(string ext in TrackInfo.CoverExtensions) {
                     *      string path = Paths.GetCoverArtPath((string) tag.Value, "." + ext);
                     *      if(System.IO.File.Exists(path)) {
                     *          track.CoverArtFileName = path;
                     *          break;
                     *      }
                     *  }
                     *  break;*/
                }
            } catch {
            }
        }
示例#4
0
        protected void Fetch()
        {
            if (Track.Uri == null || !Track.Uri.IsFile ||
                    Track.ArtworkId == null || !Banshee.IO.File.Exists (Track.Uri)) {
                return;
            }

            string directory = System.IO.Path.GetDirectoryName (Track.Uri.AbsolutePath);

            // Get the largest (in terms of file size) JPEG in the directory
            long max_size = 0;
            string best_file = null;
            int items_in_directory = 0;
            bool found_definite_best = false;
            int track_count = track.TrackCount;
            if (track.DiscCount > 0) {
                track_count = track.TrackCount * track.DiscCount;
            }
            int max_acceptable_items = Math.Max (30, track_count + 8);
            foreach (string file in Banshee.IO.Directory.GetFiles (directory)) {
                // Ignore directories with tons of songs in them; this lookup is only intended for when the
                // music file is in a directory specific to its album.
                if (++items_in_directory > max_acceptable_items) {
                    if (best_file != null) {
                        Log.Debug ("Ignoring image file in folder because too many files in folder", directory);
                    }
                    return;
                }

                if (found_definite_best) {
                    continue;
                }

                string extension = System.IO.Path.GetExtension (file).ToLower ();
                if (Array.IndexOf (extensions, extension) != -1) {
                    string filename = System.IO.Path.GetFileNameWithoutExtension (file).ToLower ();
                    if (Array.IndexOf (filenames, filename) != -1) {
                        best_file = file;
                        found_definite_best = true;
                    } else {
                        long size = Banshee.IO.File.GetSize (new SafeUri (file));
                        if (size > max_size) {
                            max_size = size;
                            best_file = file;
                        }
                    }
                }
            }

            if (best_file != null) {
                try {
                    // Copy the file to the cover art directory
                    SaveAtomically (CoverArtSpec.GetPathForNewFile (Track.ArtworkId, best_file), Banshee.IO.File.OpenRead (new SafeUri (best_file)));

                    // Send the new StreamTag
                    StreamTag tag = new StreamTag ();
                    tag.Name = CommonTags.AlbumCoverId;
                    tag.Value = Track.ArtworkId;
                    AddTag (tag);

                    Log.Debug ("Got cover art from track's folder", best_file);
                } catch (Exception e) {
                    Log.Error (e);
                }
            }
        }
        protected void OnTagFound (StreamTag tag)
        {
            if (tag.Equals (StreamTag.Zero) || current_track == null || current_track.Uri.IsFile) {
                return;
            }

            StreamTagger.TrackInfoMerge (current_track, tag);

            if (track_info_updated_timeout <= 0) {
                track_info_updated_timeout = Application.RunTimeout (250, OnTrackInfoUpdated);
            }
        }
示例#6
0
        protected void Fetch()
        {
            if (track.Uri == null || !track.Uri.IsFile || !Banshee.IO.File.Exists (track.Uri))
                return;

            string artist_album_id = track.ArtworkId;

            if(artist_album_id == null) {
                return;
            }

            IPicture [] pictures = GetEmbeddedPictures(track.Uri);

            if(pictures != null && pictures.Length > 0) {
                int preferred_index = GetPictureIndexToUse(pictures);
                IPicture picture = pictures[preferred_index];
                string path = CoverArtSpec.GetPath(artist_album_id);

                if(SavePicture(picture, path)) {
                    StreamTag tag = new StreamTag();
                    tag.Name = CommonTags.AlbumCoverId;
                    tag.Value = artist_album_id;

                    AddTag(tag);
                }
            }
        }
示例#7
0
        public override void Run ()
        {

            if (Track == null || (Track.MediaAttributes & TrackMediaAttributes.Podcast) != 0) {
                return;
            }

            string artwork_id = Track.ArtworkId;

            if (artwork_id == null || CoverArtSpec.CoverExists (artwork_id) || !ServiceManager.Get<Network> ().Connected) {
                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.Exception ("Cover art found on Lastfm, but downloading it failed. Probably server under high load or dead link", e);
            }
        }
示例#8
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 ();
        }
示例#9
0
        public static void TrackInfoMerge (TrackInfo track, StreamTag tag)
        {
            try {
                switch (tag.Name) {
                    case CommonTags.Artist:
                        track.ArtistName = Choose ((string)tag.Value, track.ArtistName);
                        break;
                    case CommonTags.ArtistSortName:
                    case CommonTags.MusicBrainzSortName:
                        track.ArtistNameSort = Choose ((string)tag.Value, track.ArtistNameSort);
                        break;
                    case CommonTags.Title:
                        //track.TrackTitle = Choose ((string)tag.Value, track.TrackTitle);
                        string title = Choose ((string)tag.Value, track.TrackTitle);

                        // Try our best to figure out common patterns in poor radio metadata.
                        // Often only one tag is sent on track changes inside the stream,
                        // which is title, and usually contains artist and track title, separated
                        // with a " - " string.
                        if (track.IsLive && title.Contains (" - ")) {
                            string [] parts = Regex.Split (title, " - ");
                            track.TrackTitle = parts[1].Trim ();
                            track.ArtistName = parts[0].Trim ();

                            // Often, the title portion contains a postfix with more information
                            // that will confuse lookups, such as "Talk [Studio Version]".
                            // Strip out the [] part.
                            Match match = Regex.Match (track.TrackTitle, "^(.+)[ ]+\\[.*\\]$");
                            if (match.Groups.Count == 2) {
                                track.TrackTitle = match.Groups[1].Value;
                            }
                        } else {
                            track.TrackTitle = title;
                        }
                        break;
                    case CommonTags.TitleSortName:
                        track.TrackTitleSort = Choose ((string)tag.Value, track.TrackTitleSort);
                        break;
                    case CommonTags.Album:
                        track.AlbumTitle = Choose ((string)tag.Value, track.AlbumTitle);
                        break;
                    case CommonTags.AlbumSortName:
                        track.AlbumTitleSort = Choose ((string)tag.Value, track.AlbumTitleSort);
                        break;
                    case CommonTags.Disc:
                    case CommonTags.AlbumDiscNumber:
                        int disc = (int)tag.Value;
                        track.DiscNumber = disc == 0 ? track.DiscNumber : disc;
                        break;
                    case CommonTags.AlbumDiscCount:
                        int count = (int)tag.Value;
                        track.DiscCount = count == 0 ? track.DiscCount : count;
                        break;
                    case CommonTags.Genre:
                        track.Genre = Choose ((string)tag.Value, track.Genre);
                        break;
                    case CommonTags.Composer:
                        track.Composer = Choose ((string)tag.Value, track.Composer);
                        break;
                    case CommonTags.Copyright:
                        track.Copyright = Choose ((string)tag.Value, track.Copyright);
                        break;
                    case CommonTags.LicenseUri:
                        track.LicenseUri = Choose ((string)tag.Value, track.LicenseUri);
                        break;
                    case CommonTags.Comment:
                        track.Comment = Choose ((string)tag.Value, track.Comment);
                        break;
                    case CommonTags.TrackNumber:
                        int track_number = (int)tag.Value;
                        track.TrackNumber = track_number == 0 ? track.TrackNumber : track_number;
                        break;
                    case CommonTags.TrackCount:
                        track.TrackCount = (int)tag.Value;
                        break;
                    case CommonTags.BeatsPerMinute:
                        track.Bpm = (int)tag.Value;
                        break;
                    case CommonTags.Duration:
                        if (tag.Value is TimeSpan) {
                            track.Duration = (TimeSpan)tag.Value;
                        } else {
                            track.Duration = new TimeSpan ((uint)tag.Value * TimeSpan.TicksPerMillisecond);
                        }
                        break;
                    case CommonTags.MoreInfoUri:
                        track.MoreInfoUri = (SafeUri)tag.Value;
                        break;
                    /* No year tag in GST it seems
                    case CommonTags.Year:
                        track.Year = (uint)tag.Value;
                        break;*/
                    case CommonTags.NominalBitrate:
                        track.BitRate = (int)tag.Value;
                        break;
                    case CommonTags.StreamType:
                        track.MimeType = (string)tag.Value;
                        break;
                    case CommonTags.VideoCodec:
                        if (tag.Value != null) {
                            track.MediaAttributes |= TrackMediaAttributes.VideoStream;
                        }
                        break;
                    /*case CommonTags.AlbumCoverId:
                        foreach(string ext in TrackInfo.CoverExtensions) {
                            string path = Paths.GetCoverArtPath((string) tag.Value, "." + ext);
                            if(System.IO.File.Exists(path)) {
                                track.CoverArtFileName = path;
                                break;
                            }
                        }
                        break;*/
                }
            } catch {
            }
        }
        public bool Lookup ()
        {
            if (Track == null || (Track.MediaAttributes & TrackMediaAttributes.Podcast) != 0) {
                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;
            }

            if (asin == null) {
                asin = FindAsin ();
                if (asin == null) {
                    return false;
                }
            }

            if (SaveHttpStreamCover (new Uri (String.Format (AmazonUriFormat, asin)), artwork_id,
                new string [] { "image/gif" })) {
                Log.Debug ("Downloaded cover art from Amazon", artwork_id);
                StreamTag tag = new StreamTag ();
                tag.Name = CommonTags.AlbumCoverId;
                tag.Value = artwork_id;

                AddTag (tag);

                return true;
            }

            return false;
        }