コード例 #1
0
ファイル: ITunes.cs プロジェクト: Korijn/MusicLibraryFixer
        public static ITunesTrack GetTrack(string file, string basePath)
        {
            if (iTunesLibraryTracks == null || iTunesLibraryTracks.Count == 0)
                return null;

            string iTunesFriendlyName = file.Replace(basePath, "").Replace(@"\", @"/");

            string[] parts = iTunesFriendlyName.Split(iTunesPathSeperator, StringSplitOptions.RemoveEmptyEntries);

            iTunesFriendlyName = "";
            for (int i = 0; i < parts.Length; i++)
            {
                iTunesFriendlyName += "/" + System.Uri.EscapeUriString(parts[i]);
            }

            XElement song = iTunesLibraryTracks.Find(delegate(XElement node)
            {
                return node.Element("Location").Value.EndsWith(iTunesFriendlyName);
            });

            if (song != null)
            {
                ITunesTrack result = new ITunesTrack();

                PropertyInfo[] props = result.GetType().GetProperties();
                for (int i = 0; i < props.Length; i++)
                {
                    if(song.Element(props[i].Name) != null && !song.Element(props[i].Name).IsEmpty)
                    {
                        object o = null;
                        try
                        {
                            o = Convert.ChangeType(song.Element(props[i].Name).Value, props[i].PropertyType);
                        }
                        catch {
                        }
                        if (o != null)
                            props[i].SetValue(result, o, null);
                    }
                }

                return result;
            }

            return null;
        }
コード例 #2
0
ファイル: Fixer.cs プロジェクト: Korijn/MusicLibraryFixer
        private static void UpdateTag(Tag fileTag, Dictionary<WMP.Attributes, string> wmpTrack, ITunesTrack iTunesTrack)
        {
            // get as much extra info from Windows Media Player and iTunes
            if (String.IsNullOrEmpty(fileTag.Title))
            {
                if (iTunesTrack != null && !String.IsNullOrEmpty(iTunesTrack.Name))
                    fileTag.Title = iTunesTrack.Name;
                else if (wmpTrack.ContainsKey(WMP.Attributes.Name))
                    fileTag.Title = wmpTrack[WMP.Attributes.Name];
            }

            if (fileTag.Performers.Length == 0)
            {
                if (iTunesTrack != null && !String.IsNullOrEmpty(iTunesTrack.Artist))
                    fileTag.Performers = new string[] { iTunesTrack.Artist };
                else if (wmpTrack.ContainsKey(WMP.Attributes.Author))
                    fileTag.Performers = new string[] { wmpTrack[WMP.Attributes.Author] };
            }

            if (fileTag.AlbumArtists.Length == 0)
            {
                if (iTunesTrack != null && !String.IsNullOrEmpty(iTunesTrack.AlbumArtist))
                    fileTag.AlbumArtists = new string[] { iTunesTrack.AlbumArtist };
                else if (wmpTrack.ContainsKey(WMP.Attributes.AlbumArtist))
                    fileTag.AlbumArtists = new string[] { wmpTrack[WMP.Attributes.AlbumArtist] };
            }

            if (String.IsNullOrEmpty(fileTag.Album))
            {
                if (iTunesTrack != null && !String.IsNullOrEmpty(iTunesTrack.Album))
                    fileTag.Album = iTunesTrack.Album;
                else if (wmpTrack.ContainsKey(WMP.Attributes.Album))
                    fileTag.Album = wmpTrack[WMP.Attributes.Album];
            }

            if (fileTag.Year <= 0)
            {
                if (iTunesTrack != null && iTunesTrack.Year > 0)
                    fileTag.Year = (uint)iTunesTrack.Year;
                else if (wmpTrack.ContainsKey(WMP.Attributes.WMYear))
                {
                    uint wmpYear = (uint)Convert.ToInt32(wmpTrack[WMP.Attributes.WMYear]);
                    if (wmpYear > 0)
                        fileTag.Year = wmpYear;
                }
            }

            if (fileTag.Track <= 0)
            {
                if (iTunesTrack != null && iTunesTrack.TrackNumber > 0)
                    fileTag.Track = (uint)iTunesTrack.TrackNumber;
                else if (wmpTrack.ContainsKey(WMP.Attributes.OriginalIndex))
                {
                    uint wmpTrackNr = (uint)Convert.ToInt32(wmpTrack[WMP.Attributes.OriginalIndex]);
                    if (wmpTrackNr > 0)
                        fileTag.Track = wmpTrackNr;
                }
            }

            // now attempt to get additional data from musicbrainz
            if (String.IsNullOrEmpty(fileTag.Title)
                ||
                fileTag.Performers.Length == 0
                ||
                fileTag.AlbumArtists.Length == 0
                ||
                String.IsNullOrEmpty(fileTag.Album))
            {

                Track musicBrainzTrack = null;
                if (!String.IsNullOrEmpty(fileTag.FirstPerformer))
                    musicBrainzTrack = MusicBrainz.GetTrack(fileTag.FirstPerformer, fileTag.Album, fileTag.Title);
                else if (!String.IsNullOrEmpty(fileTag.FirstAlbumArtist))
                    musicBrainzTrack = MusicBrainz.GetTrack(fileTag.FirstAlbumArtist, fileTag.Album, fileTag.Title);

                if (musicBrainzTrack != null)
                {
                    try
                    {
                        if (String.IsNullOrEmpty(fileTag.Title))
                        {
                            string title = musicBrainzTrack.GetTitle();
                            if (!String.IsNullOrEmpty(title))
                                fileTag.Title = title;
                        }
                    }
                    catch { }

                    try
                    {
                        if (fileTag.Performers.Length == 0)
                        {
                            string artist = musicBrainzTrack.GetArtist().GetName();
                            if (!String.IsNullOrEmpty(artist))
                                fileTag.Performers = new string[] { artist };
                        }
                    }
                    catch { }

                    try
                    {
                        if (fileTag.AlbumArtists.Length == 0)
                        {
                            string artist = musicBrainzTrack.GetArtist().GetName();
                            if (!String.IsNullOrEmpty(artist))
                                fileTag.AlbumArtists = new string[] { artist };
                        }
                    }
                    catch { }

                    try
                    {
                        if (String.IsNullOrEmpty(fileTag.Album))
                        {
                            string firstReleaseTitle = musicBrainzTrack.GetReleases().First().GetTitle();
                            if (!String.IsNullOrEmpty(firstReleaseTitle))
                                fileTag.Album = firstReleaseTitle;
                        }
                    }
                    catch { }
                }
            }
        }