/// <summary> /// 解析预览 /// </summary> /// <param name="url"></param> /// <param name="rule"></param> /// <returns>返回书对象</returns> public static Book ParsePreviewBook(string url, BookRule rule) { try { Uri uri = new Uri(url); Book book = new Book(); book.rule = rule.name; book.baseurl = url; ParseCatalog(uri, book, rule); ParseContent(uri, book, rule, 5);//预览5章 return book; } catch (Exception err) { throw err; } }
private static void ParseContent(Uri uri, Book book, BookRule rule,int pagenum) { if (rule.content_rule != "" && book.catalogs!=null) { MultiThreadingWorker thWork = new MultiThreadingWorker(); thWork.threadCount = 20; thWork.workContent = new WorkContent(ParsePage); for (int i = 0; i < book.catalogs.Count; i++) { if (pagenum > 0 && pagenum < i + 1) break;//预览 thWork.AddWork(book.catalogs[i]); } thWork.Start(); } }
private static void ParseContent(Uri uri, Book book, BookRule rule) { ParseContent(uri, book, rule, 0); }
private static void ParseCatalog(Uri uri,string html, Book book, BookRule rule) { string buffer; NSoup.Nodes.Document doc = html == null ? NSoupHelper.GetNSoupDoc(uri.ToString(), rule.charset, out buffer) : NSoupHelper.GetNSoupDoc(html); if (doc == null) return; if (rule.title_rule != "") { book.title = doc.Select(rule.title_rule).Text; } if (rule.auther_rule != "") { book.auther = doc.Select(rule.auther_rule).Text; } if (rule.cover_rule != "") { book.cover = doc.Select(rule.cover_rule).Text; } if (rule.introduction_rule != "") { book.introduction = doc.Select(rule.introduction_rule).Text; } if (rule.catalog_rule != "") { var catalog = doc.Select(rule.catalog_rule); if (catalog.Count > 0) { book.catalogs = new List<BookCatalog>(); for (int i = 0; i < catalog.Count; i++) { BookCatalog bc = new BookCatalog(); bc.index = i; bc.url = catalog[i].Attr("href"); bc.text = catalog[i].Text(); bc.baseurl = uri.ToString(); bc.bookrule = rule; book.catalogs.Add(bc); } } } }
private static void ParseCatalog(Uri uri, Book book, BookRule rule) { ParseCatalog(uri, null, book, rule); }
public static void LoadXml() { XmlHandle xmlhandle = new XmlHandle(bookrulexml); int num = xmlhandle.GetCount("BookSite"); if (num > 0) _rulelist = new List<BookRule>(); for (int i = 1; i <= num; i++) { BookRule rule = new BookRule(); rule.name = xmlhandle.GetValue("BookSite[" + i + "]", "Name"); rule.url = xmlhandle.GetValue("BookSite[" + i + "]", "Url"); rule.charset = xmlhandle.GetValue("BookSite[" + i + "]", "charset"); rule.title_rule = xmlhandle.GetValue("BookSite[" + i + "]/title"); rule.auther_rule = xmlhandle.GetValue("BookSite[" + i + "]/auther"); rule.cover_rule = xmlhandle.GetValue("BookSite[" + i + "]/cover"); rule.introduction_rule = xmlhandle.GetValue("BookSite[" + i + "]/introduction"); rule.catalog_rule = xmlhandle.GetValue("BookSite[" + i + "]/catalog"); rule.content_rule = xmlhandle.GetValue("BookSite[" + i + "]/content"); _rulelist.Add(rule); } num = xmlhandle.GetCount("SearchSite"); if (num > 0) _searchrulelist = new List<SearchRule>(); for (int i = 1; i <= num; i++) { SearchRule rule = new SearchRule(); rule.name = xmlhandle.GetValue("SearchSite[" + i + "]", "Name"); rule.searchurl = xmlhandle.GetValue("SearchSite[" + i + "]", "SearchUrl"); rule.charset = xmlhandle.GetValue("SearchSite[" + i + "]", "charset"); rule.list_rule = xmlhandle.GetValue("SearchSite[" + i + "]", "list"); rule.conditions =new List<RuleConditions>(); rule.title_rule = xmlhandle.GetValue("SearchSite[" + i + "]/title"); XmlAttributeCollection attcoll = xmlhandle.GetAttributes("SearchSite[" + i + "]/title"); foreach (XmlAttribute v in attcoll) { RuleConditions cond = new RuleConditions(); cond.name = "title"; cond.conditionName = v.Name; cond.conditionValue = v.Value; rule.conditions.Add(cond); } rule.auther_rule = xmlhandle.GetValue("SearchSite[" + i + "]/auther"); attcoll = xmlhandle.GetAttributes("SearchSite[" + i + "]/auther"); foreach (XmlAttribute v in attcoll) { RuleConditions cond = new RuleConditions(); cond.name = "auther"; cond.conditionName = v.Name; cond.conditionValue = v.Value; rule.conditions.Add(cond); } rule.cover_rule = xmlhandle.GetValue("SearchSite[" + i + "]/cover"); attcoll = xmlhandle.GetAttributes("SearchSite[" + i + "]/cover"); foreach (XmlAttribute v in attcoll) { RuleConditions cond = new RuleConditions(); cond.name = "cover"; cond.conditionName = v.Name; cond.conditionValue = v.Value; rule.conditions.Add(cond); } rule.introduction_rule = xmlhandle.GetValue("SearchSite[" + i + "]/introduction"); attcoll = xmlhandle.GetAttributes("SearchSite[" + i + "]/introduction"); foreach (XmlAttribute v in attcoll) { RuleConditions cond = new RuleConditions(); cond.name = "introduction"; cond.conditionName = v.Name; cond.conditionValue = v.Value; rule.conditions.Add(cond); } rule.bookurl_rule = xmlhandle.GetValue("SearchSite[" + i + "]/bookurl"); attcoll = xmlhandle.GetAttributes("SearchSite[" + i + "]/bookurl"); foreach (XmlAttribute v in attcoll) { RuleConditions cond = new RuleConditions(); cond.name = "bookurl"; cond.conditionName = v.Name; cond.conditionValue = v.Value; rule.conditions.Add(cond); } _searchrulelist.Add(rule); } }
public static void SaveXml(BookRule rule) { XmlHandle xmlhandle = new XmlHandle(bookrulexml); if (xmlhandle.GetCount("BookSite[Name=" + rule.name + "]") > 0)//更新 { xmlhandle.SetValue("BookSite[Name=" + rule.name + "]", "Url", rule.url); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]", "charset", rule.charset); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/title", rule.title_rule); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/auther", rule.auther_rule); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/cover", rule.cover_rule); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/introduction", rule.introduction_rule); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/catalog", rule.catalog_rule); xmlhandle.SetValue("BookSite[Name=" + rule.name + "]/content", rule.content_rule); } else//新增 { XmlElement node = xmlhandle.AddNode("BookSite"); node.SetAttribute("Name", rule.name); node.SetAttribute("Url", rule.url); node.SetAttribute("charset", rule.charset); XmlElement node1 = xmlhandle.AddNode(node, "title"); node1.InnerText = rule.title_rule; XmlElement node2 = xmlhandle.AddNode(node, "auther"); node2.InnerText = rule.auther_rule; XmlElement node3 = xmlhandle.AddNode(node, "cover"); node3.InnerText = rule.cover_rule; XmlElement node4 = xmlhandle.AddNode(node, "introduction"); node4.InnerText = rule.introduction_rule; XmlElement node5 = xmlhandle.AddNode(node, "catalog"); node5.InnerText = rule.catalog_rule; XmlElement node6 = xmlhandle.AddNode(node, "content"); node6.InnerText = rule.content_rule; } xmlhandle.SaveConfig(); }