예제 #1
0
        /// <summary>
        /// Get the media type information from the iTunes track nodes, and add a new media type if it does not exist yet.
        /// </summary>
        /// <param name="trackInfo">Parsed iTunes track info.</param>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <returns>Returns the MediaTypeId.</returns>
        private static int AddMediaType(TrackInfo trackInfo, ChinookDataSet ds)
        {
            DataRow[] results = ds.MediaType.Select("Name = '" + trackInfo.MediaTypeName.Replace("'", "''") + "'");

            if (results == null || results.Length == 0)
            {
                var row = ds.MediaType.AddMediaTypeRow(trackInfo.MediaTypeName);
                return(row.MediaTypeId);
            }

            return((int)results[0][0]);
        }
예제 #2
0
        /// <summary>
        /// Get the artist information from the iTunes track nodes, and add a new artist if it does not exist yet.
        /// </summary>
        /// <param name="trackInfo">Parsed iTunes track info.</param>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <returns>Returns the ArtistId.</returns>
        private static int AddArtist(TrackInfo trackInfo, ChinookDataSet ds)
        {
            DataRow[] results = ds.Artist.Select("Name = '" + trackInfo.ArtistName.Replace("'", "''") + "'");

            if (results == null || results.Length == 0)
            {
                var row = ds.Artist.AddArtistRow(trackInfo.ArtistName);
                return(row.ArtistId);
            }

            return((int)results[0][0]);
        }
        /// <summary>
        /// Builds a dataset and populates it with iTunes library information.
        /// </summary>
        /// <returns>Populated dataset.</returns>
        public ChinookDataSet BuildDataSet()
        {
            // Create a dataset and prepopulate it.
            var ds = new ChinookDataSet();

            // If non media data is supplied, then prepopulate the dataset.
            if (_xmlNonMediaDataFilename != null && File.Exists(_xmlNonMediaDataFilename))
            {
                ds.ReadXml(_xmlNonMediaDataFilename);
            }

            // Create an XPath navigator for the iTunes library.
            if (File.Exists(_filename))
            {
                var document = new XPathDocument(_filename);
                var navigator = document.CreateNavigator();

                // Import tracks (including album, artist, media type, etc.) and playlists.
                ImportTracks(ds, navigator);
                ImportPlaylists(ds, navigator);
            }

            return ds;
        }
        /// <summary>
        /// Get the album information from the iTunes track nodes, and add a new album if it does not exist yet.
        /// </summary>
        /// <param name="trackInfo">Parsed iTunes track info.</param>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <returns>Returns the AlbumId.</returns>
        private static int AddAlbum(TrackInfo trackInfo, ChinookDataSet ds)
        {
            DataRow[] results = ds.Album.Select("Title = '" + trackInfo.AlbumName.Replace("'", "''") + "'");

            if (results == null || results.Length == 0)
            {
                var row = ds.Album.AddAlbumRow(trackInfo.AlbumName, trackInfo.ArtistId);
                return row.AlbumId;
            }

            return (int)results[0][0];
        }
        /// <summary>
        /// Import media track information from iTunes library file.
        /// </summary>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <param name="navigator">XPath navigator of iTunes library.</param>
        private void ImportTracks(ChinookDataSet ds, XPathNavigator navigator)
        {
            var trackInfo = new TrackInfo();
            // Creates an XPath to the track info nodes.
            var nodes = navigator.Select("/plist/dict/dict/dict");
            while (nodes.MoveNext())
            {
                trackInfo.Clear();
                var clone = nodes.Current.Clone();
                var iterator = clone.SelectChildren("", "");

                if (ParseTrackNode(trackInfo, iterator))
                {
                    trackInfo.ArtistId = AddArtist(trackInfo, ds);
                    trackInfo.AlbumId = AddAlbum(trackInfo, ds);
                    trackInfo.GenreId = AddGenre(trackInfo, ds);
                    trackInfo.MediaTypeId = AddMediaType(trackInfo, ds);

                    // Add a new track to the dataset.
                    ChinookDataSet.TrackRow row = ds.Track.AddTrackRow(trackInfo.Name, trackInfo.AlbumId,
                                                                       trackInfo.MediaTypeId, trackInfo.GenreId,
                                                                       trackInfo.Composer, trackInfo.Time,
                                                                       trackInfo.Size, trackInfo.UnitPrice);

                    // Maps the track ids used by iTunes in order to use it later when importing playlists.
                    _trackIds[trackInfo.OriginalTrackId] = row.TrackId;
                }
            }
        }
        /// <summary>
        /// Import playlist information from iTunes library.
        /// </summary>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <param name="navigator">XPath navigator of iTunes library.</param>
        private void ImportPlaylists(ChinookDataSet ds, XPathNavigator navigator)
        {
            // Navigate to the node that contains the playlist name.
            var playlistNodes = navigator.Select("/plist/dict/array/dict/string[position()=1]");

            while (playlistNodes.MoveNext())
            {
                var clone = playlistNodes.Current.Clone();
                var name = clone.Value;

                // If it is one of the excluded playlists, then skip it.
                if (_excludedPlaylists.Contains(name)) continue;

                // Add a new Playlist
                var playlistRow = ds.Playlist.AddPlaylistRow(name);

                clone.MoveToParent();
                var trackNodes = clone.Select("./array/dict/integer");

                while (trackNodes.MoveNext())
                {
                    int originalTrackId;
                    if (int.TryParse(trackNodes.Current.Value, out originalTrackId) && _trackIds.ContainsKey(originalTrackId))
                    {
                        // Add a new Playlist
                        ds.PlaylistTrack.AddPlaylistTrackRow(playlistRow.PlaylistId, _trackIds[originalTrackId]);
                    }
                }
            }
        }
        /// <summary>
        /// Get the media type information from the iTunes track nodes, and add a new media type if it does not exist yet.
        /// </summary>
        /// <param name="trackInfo">Parsed iTunes track info.</param>
        /// <param name="ds">ChinookDataSet to be populated.</param>
        /// <returns>Returns the MediaTypeId.</returns>
        private static int AddMediaType(TrackInfo trackInfo, ChinookDataSet ds)
        {
            DataRow[] results = ds.MediaType.Select("Name = '" + trackInfo.MediaTypeName.Replace("'", "''") + "'");

            if (results == null || results.Length == 0)
            {
                var row = ds.MediaType.AddMediaTypeRow(trackInfo.MediaTypeName);
                return row.MediaTypeId;
            }

            return (int)results[0][0];
        }