private void DoWork(object sender, DoWorkEventArgs e) { List <AnimeEntry> m_aList = new List <AnimeEntry>(); double totalProcessedFiles = 0; double totalFiles = int.Parse(new XPathDocument(xmlPath).CreateNavigator().Evaluate("count(//file)").ToString()) * 2; using (XmlReader reader = XmlReader.Create(xmlPath)) { reader.ReadToFollowing("mylist"); if (reader["template"] != "mini") { Dispatcher.Invoke(new Action(delegate { MessageBox.Show("Please ensure you selected a mylist export file that used the xml-mini template.", "Invalid xml template!", MessageBoxButton.OK, MessageBoxImage.Error); })); xmlWorker.CancelAsync(); return; } // <anime> while (reader.ReadToFollowing("anime")) { while (closePending) { Thread.Sleep(500); } AnimeEntry entry = new AnimeEntry(); entry.aid = int.Parse(reader["aid"]); entry.type = reader["type"]; entry.year = reader["year"]; // <titles> reader.ReadToFollowing("default"); entry.romaji = reader.ReadElementContentAsString(); Dispatcher.BeginInvoke(new Action(delegate { importFilePath.Text = String.Format("Reading: {0}", entry.title); })); reader.ReadToFollowing("nihongo"); entry.nihongo = reader.ReadElementContentAsString().FormatNullable(); reader.ReadToFollowing("english"); entry.english = reader.ReadElementContentAsString().FormatNullable(); // </titles> // <episodes> if (!reader.ReadToFollowing("episodes")) { goto Finish; } entry.eps_total = int.Parse(reader["total"]); XmlReader episodesReader = reader.ReadSubtree(); // <episode> while (episodesReader.ReadToFollowing("episode")) { while (closePending) { Thread.Sleep(500); } EpisodeEntry episode = new EpisodeEntry(); episode.eid = int.Parse(episodesReader["eid"]); episode.airdate = episodesReader["aired"] == "-" ? null : UnixTimestamp.FromDateTime(DateTime.Parse(episodesReader["aired"], System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"))); episode.watched = Convert.ToBoolean(int.Parse(episodesReader["watched"])); if (Regex.IsMatch(episodesReader["epno"].Substring(0, 1), @"\D")) { episode.spl_epno = episodesReader["epno"]; } else { episode.epno = int.Parse(episodesReader["epno"]); } // <titles> episodesReader.ReadToDescendant("english"); episode.english = episodesReader.ReadElementContentAsString(); episodesReader.ReadToFollowing("nihongo"); episode.nihongo = episodesReader.ReadElementContentAsString().FormatNullable(); episodesReader.ReadToFollowing("romaji"); episode.romaji = episodesReader.ReadElementContentAsString().FormatNullable(); // </titles> // <files> if (!episodesReader.ReadToFollowing("files")) { goto Finish; } XmlReader filesReader = episodesReader.ReadSubtree(); // <file> while (filesReader.ReadToFollowing("file")) { while (closePending) { Thread.Sleep(500); } FileEntry file = new FileEntry(); file.fid = int.Parse(filesReader["fid"]); file.lid = int.Parse(filesReader["lid"]); file.watcheddate = filesReader["watched"] == "-" ? null : UnixTimestamp.FromDateTime(DateTime.Parse(episodesReader["watched"], System.Globalization.CultureInfo.CreateSpecificCulture("en-GB"))); file.watched = file.watcheddate != null; file.generic = episodesReader["generic"] != null; if (!file.generic) // generic entries do not have this information { int gid = 0; if (filesReader["gid"] != null) { gid = int.Parse(filesReader["gid"]); } file.ed2k = filesReader["ed2k"]; file.length = double.Parse(filesReader["length"]); file.size = double.Parse(filesReader["size"]); file.source = filesReader["source"].FormatNullable(); file.acodec = ExtensionMethods.FormatAudioCodec(filesReader["acodec"].FormatNullable()); file.vcodec = ExtensionMethods.FormatVideoCodec(filesReader["vcodec"].FormatNullable()); file.vres = filesReader["vres"].FormatNullable(); if (gid != 0) { // <group_name> filesReader.ReadToFollowing("group_name"); string group_name = filesReader.ReadElementContentAsString(); // </group_name> // <group_abbr> filesReader.ReadToFollowing("group_abbr"); string group_abbr = filesReader.ReadElementContentAsString(); // </group_abbr> file.Group = new GroupEntry { gid = gid, group_name = group_name, group_abbr = group_abbr }; } } episode.Files.Add(file); importProgressBar.Dispatcher.BeginInvoke(new Action <double, double>((total, processed) => { importProgressBar.Value = Math.Ceiling(processed / total * 100); }), totalFiles, ++totalProcessedFiles); // </file> } // </files> filesReader.Close(); entry.Episodes.Add(episode); // </episode> } // </episodes> episodesReader.Close(); Finish: m_aList.Add(entry); // </anime> } // </mylist> } m_myList.OnEntryInserted += (aTitle) => { importProgressBar.Dispatcher.BeginInvoke(new Action(delegate { importFilePath.Text = String.Format("Writing: {0}", aTitle); importProgressBar.Value = Math.Ceiling(++totalProcessedFiles / totalFiles * 100); })); }; m_myList.InsertFromImport(m_aList); }
/// <summary> /// Parses the return message of a FILE command, /// and then triggers the OnFileInfoFetched event. /// </summary> private void ParseFileData(object item, string data) { string[] info = Regex.Split(data, "\n")[1].Split('|'); AnimeEntry anime = new AnimeEntry(); EpisodeEntry episode = new EpisodeEntry(); FileEntry file = (item is HashItem) ? new FileEntry((HashItem)item) : (FileEntry)item; file.fid = int.Parse(info[0]); anime.aid = episode.aid = int.Parse(info[1]); episode.eid = file.eid = int.Parse(info[2]); file.lid = int.Parse(info[4]); file.source = info[5].FormatNullable(); file.acodec = info[6].Contains("'") ? info[6].Split('\'')[0] : info[6].FormatNullable(); file.acodec = ExtensionMethods.FormatAudioCodec(file.acodec); file.vcodec = ExtensionMethods.FormatVideoCodec(info[7].FormatNullable()); file.vres = info[8].FormatNullable(); file.length = double.Parse(info[9]); if (!string.IsNullOrEmpty(info[10]) && int.Parse(info[10]) != 0) { episode.airdate = double.Parse(info[10]); } file.state = int.Parse(info[11]); episode.watched = file.watched = Convert.ToBoolean(int.Parse(info[12])); if (!string.IsNullOrEmpty(info[13]) && int.Parse(info[13]) != 0) { file.watcheddate = double.Parse(info[13]); } anime.eps_total = !string.IsNullOrEmpty(info[14]) ? (int.Parse(info[14]) > int.Parse(info[15]) ? int.Parse(info[14]) : int.Parse(info[15])) : int.Parse(info[15]); anime.year = info[16].Contains('-') ? (info[16].Split('-')[0] != info[16].Split('-')[1] ? info[16] : info[16].Split('-')[0]) : info[16]; anime.type = info[17]; anime.romaji = info[18]; anime.nihongo = info[19].FormatNullable(); anime.english = info[20].FormatNullable(); if (Regex.IsMatch(info[21].Substring(0, 1), @"\D")) { episode.spl_epno = info[21]; } else { episode.epno = info[21].Contains('-') ? int.Parse(info[21].Split('-')[0]) : int.Parse(info[21]); } episode.english = info[22]; episode.romaji = info[23].FormatNullable(); episode.nihongo = info[24].FormatNullable(); if (int.Parse(info[3]) != 0) { file.Group = new GroupEntry { gid = int.Parse(info[3]), group_name = info[25], group_abbr = info[26] } } ; OnFileInfoFetched(new FileInfoFetchedArgs(anime, episode, file)); }