private static iTunesGenre BuildGenre(Dictionary <string, iTunesGenre> dictionaryGenre, iTunesGenre childGenreFolder, iTunesMusicLibrary folder, Dictionary <string, string> data) { if (!string.IsNullOrEmpty(data["Genre"])) { if (dictionaryGenre.ContainsKey(data["Genre"])) { childGenreFolder = dictionaryGenre[data["Genre"]]; } else { childGenreFolder = new iTunesGenre(); childGenreFolder.GenreName = data["Genre"]; childGenreFolder.Id = childGenreFolder.Name.GetMD5(); folder.Genres.Add(childGenreFolder); dictionaryGenre.Add(data["Genre"], childGenreFolder); } } return(childGenreFolder); }
public static iTunesMusicLibrary GetDetailsFromXml(iTunesMusicLibrary existingLibrary) { string path = Settings.Instance.iTunesLibraryXMLPath; if (!File.Exists(path)) { throw new FileNotFoundException("iTunesLibraryXMLPath " + path + " not found!"); } XPathDocument doc = new XPathDocument(path); XPathNavigator nav = doc.CreateNavigator(); // Move to plist, then master library and tracks nav.MoveToChild("plist", ""); nav.MoveToChild("dict", ""); nav.MoveToChild("dict", ""); Dictionary <string, iTunesArtist> dictionaryArtist = new Dictionary <string, iTunesArtist>(); Dictionary <string, iTunesAlbum> dictionaryAlbum = new Dictionary <string, iTunesAlbum>(); Dictionary <string, iTunesAlbum> dictionaryUniqueAlbum = new Dictionary <string, iTunesAlbum>(); Dictionary <string, iTunesGenre> dictionaryGenre = new Dictionary <string, iTunesGenre>(); iTunesArtist childArtistFolder; iTunesAlbum childAlbumFolder; iTunesGenre childGenreFolder = null; iTunesAlbum childUniqueAlbumFolder; // Move to first track info bool success = nav.MoveToChild("dict", ""); //int count = 0; // Read each song until we have enough or no more iTunesMusicLibrary folder; if (existingLibrary == null) { folder = new iTunesMusicLibrary(); } else { folder = existingLibrary; ClearCache(folder); } folder.LastUpdate = DateTime.Now; while (success) { success = nav.MoveToFirstChild(); // Read each piece of information about the song Dictionary <string, string> data = new Dictionary <string, string>(); while (success) { string key = nav.Value; nav.MoveToNext(); data[key] = nav.Value; success = nav.MoveToNext(); } if (!data.ContainsKey("Name")) { data.Add("Name", ""); } if (!data.ContainsKey("Artist")) { data.Add("Artist", ""); } if (!data.ContainsKey("Album")) { data.Add("Album", ""); } if (!data.ContainsKey("Genre")) { data.Add("Genre", ""); } string uncPath = GetUncFileName(data["Location"]); if (string.IsNullOrEmpty(data["Artist"]) || string.IsNullOrEmpty(data["Album"])) { success = NewElement(nav, success); continue; } #if !DEBUG else if (!File.Exists(uncPath)) { Logger.ReportError(string.Format("MusicPlugin, file {0} does not exist, not added to iTunes Library", uncPath)); success = NewElement(nav, success); continue; } #endif else if (!MusicHelper.IsMusic(uncPath)) { Logger.ReportError(string.Format("MusicPlugin, file {0} is not supported, not added to iTunes Library", uncPath)); success = NewElement(nav, success); continue; } childGenreFolder = BuildGenre(dictionaryGenre, childGenreFolder, folder, data); childArtistFolder = BuildArtist(dictionaryArtist, childGenreFolder, folder, data); childAlbumFolder = BuildAlbum(dictionaryAlbum, childArtistFolder, data); iTunesSong newSong = BuildSong(childArtistFolder, childAlbumFolder, data); ReadFileImage(childAlbumFolder, newSong); if (dictionaryUniqueAlbum.ContainsKey(data["Album"])) { childUniqueAlbumFolder = dictionaryUniqueAlbum[data["Album"]]; } else { childUniqueAlbumFolder = new iTunesAlbum(); childUniqueAlbumFolder.Name = data["Album"]; childUniqueAlbumFolder.AlbumName = data["Album"]; childUniqueAlbumFolder.Id = childUniqueAlbumFolder.AlbumName.GetMD5(); folder.Albums.Add(childUniqueAlbumFolder); dictionaryUniqueAlbum.Add(data["Album"], childUniqueAlbumFolder); } childUniqueAlbumFolder.Songs.Add(newSong); childUniqueAlbumFolder.PrimaryImagePath = childAlbumFolder.PrimaryImagePath; childUniqueAlbumFolder.BackdropImagePath = childAlbumFolder.BackdropImagePath; success = NewElement(nav, success); } if (Settings.Instance.ShowPlaylistAsFolder) { AddSpecialMusicFolder(folder); } return(folder); }
private static iTunesArtist BuildArtist(Dictionary <string, iTunesArtist> dictionaryArtist, iTunesGenre childGenreFolder, iTunesMusicLibrary folder, Dictionary <string, string> data) { iTunesArtist childArtistFolder; if (dictionaryArtist.ContainsKey(data["Artist"])) { childArtistFolder = dictionaryArtist[data["Artist"]]; } else { childArtistFolder = new iTunesArtist(); childArtistFolder.ArtistName = data["Artist"]; childArtistFolder.Id = childArtistFolder.Name.GetMD5(); if (childGenreFolder != null && !string.IsNullOrEmpty(data["Genre"])) { childGenreFolder.Artists.Add(childArtistFolder); } folder.Artists.Add(childArtistFolder); dictionaryArtist.Add(data["Artist"], childArtistFolder); } return(childArtistFolder); }