/// <summary> /// Scan all tracks in the given playlist. /// </summary> /// <param name="list"></param> private void ScanTracks(PersistentIDCollection pids) { var tagger = new Tagger(); int total = pids.Count; int count = 0; foreach (PersistentID persistentID in pids) { if (!base.isActive) { Logger.WriteLine(base.name, "Information scanner cancelled while scanning"); break; } using (Track track = libraryPlaylist.GetTrack(persistentID)) { if ((track != null) && (track.Kind == TrackKind.File)) { Logger.WriteLine(base.name, String.Format( "Fetching tag information for '{0}' ({1})", track.MakeKey(), track.UniqueID)); try { // store into a temporary TrackFile so we can decide which // properties to update... var buffer = new TrackFile(track); tagger.RetrieveTags(buffer); Reconcile(track, buffer); } catch (Exception exc) { Logger.WriteLine(base.name, String.Format("Error fetching information {0}, {1}, {2}", track.Artist, track.Name, track.Album), exc); } } } count++; base.ProgressPercentage = (int)((double)count / (double)total * 100.0); } tagger = null; }
/// <summary> /// /// </summary> private void ExecuteInternal() { var playlist = controller.CreatePlaylist( Path.GetFileNameWithoutExtension(playlistPath)); if (playlist == null) { Logger.WriteLine(Logger.Level.Error, base.name, "Error creating playlist"); return; } int count = 0; // tracks successfully imported int failed = 0; // tracks failed import int numFound = 0; // tracks found in library int numRetrieved = 0; // tracks retrieved from GenPUID/MusicBrainz int numSimilar = 0; // tracks identified using similar artists int numFailed = 0; // tracks unidentified string path; string description = String.Empty; Tagger tagger = null; while ((path = reader.GetNext()) != null) { if (!base.isActive) { // scanner cancelled by user break; } if (!File.Exists(path)) { Logger.WriteLine(base.name, "Could not find " + path); continue; } base.UpdateProgress(Path.GetFileNameWithoutExtension(path)); var trackFile = new TrackFile(path); Tagger.ReadFileTags(trackFile); if (String.IsNullOrEmpty(trackFile.Title) || (String.IsNullOrEmpty(trackFile.Album) && String.IsNullOrEmpty(trackFile.Artist))) { Logger.WriteLine(Logger.Level.Debug, base.name, "retrieving tag for " + trackFile.Location); if (tagger == null) { tagger = new Tagger(); } tagger.RetrieveTags(trackFile); numRetrieved++; } PersistentID pid = catalog.FindTrack( trackFile.Album, trackFile.Artist, trackFile.Title); if (pid.IsEmpty) { numFailed++; Logger.WriteLine(base.name, String.Format("Could not find track '{0}', '{1}', '{2}'", trackFile.Artist, trackFile.Album, trackFile.Title)); continue; } Track track = controller.LibraryPlaylist.GetTrack(pid); numFound++; if (track.Artist.Equals(trackFile.Artist)) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format("Found track ({0} | {1} | {2})", track.Artist, track.Album, track.Title)); } else { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format("Found similar track ({0} | {1} | {2})", track.Artist, track.Album, track.Title)); numSimilar++; } try { track = playlist.AddTrack(track); if (track == null) { failed++; Logger.WriteLine(Logger.Level.Error, base.name, String.Format("Error importing {0}", path)); } else { description = String.Format("{0}, {1}", track.Name, track.Artist); count++; Logger.WriteLine(base.name, String.Format("Imported {0}", description)); } } catch (Exception) { // TODO: ? } finally { track.Dispose(); } } base.UpdateProgress(Resx.Completed); playlist.Dispose(); Logger.WriteLine(base.name, String.Format("- found:{0}, failed:{1}, similar:{2}, retrieved:{3}", numFound, numFailed, numSimilar, numRetrieved)); }
/// <summary> /// /// </summary> /// <param name="track"></param> /// <param name="buffer"></param> private void Reconcile(Track track, TrackFile buffer) { if (!String.IsNullOrEmpty(buffer.Album) && !buffer.Album.Equals(track.Album, StringComparison.InvariantCultureIgnoreCase)) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} Album '{1}' to '{2}'", track.UniqueID, track.Album, buffer.Album)); if (ScannerBase.isLive) { track.Album = buffer.Album; } } if (!String.IsNullOrEmpty(buffer.Artist) && !buffer.Artist.Equals(track.Artist, StringComparison.InvariantCultureIgnoreCase)) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} Artist '{1}' to '{2}'", track.UniqueID, track.Artist, buffer.Artist)); if (ScannerBase.isLive) { track.Artist = buffer.Artist; } } if (!String.IsNullOrEmpty(buffer.ArtistURL) && !buffer.ArtistURL.Equals(track.ArtistURL, StringComparison.InvariantCultureIgnoreCase)) { track.ArtistURL = buffer.ArtistURL; Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} ArtistURL to '{1}'", track.UniqueID, buffer.ArtistURL)); if (ScannerBase.isLive) { track.ArtistURL = buffer.ArtistURL; } } if (!String.IsNullOrEmpty(buffer.Genre) && !buffer.Genre.Equals(track.Genre, StringComparison.InvariantCultureIgnoreCase)) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} Genre '{1}' to '{2}'", track.UniqueID, track.Genre, buffer.Genre)); if (ScannerBase.isLive) { track.Genre = buffer.Genre; } } if (!String.IsNullOrEmpty(buffer.Title) && !buffer.Title.Equals(track.Title, StringComparison.InvariantCultureIgnoreCase)) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} Title '{1}' to '{2}'", track.UniqueID, track.Title, buffer.Title)); if (ScannerBase.isLive) { track.Title = buffer.Title; } } if (track.Year != buffer.Year) { Logger.WriteLine(Logger.Level.Debug, base.name, String.Format(" - updating {0} Year '{1}' to '{2}'", track.UniqueID, track.Year, buffer.Year)); if (ScannerBase.isLive) { track.Year = buffer.Year; } } }