/// <summary> /// 保存本子信息 /// </summary> /// <param name="Book"></param> private static void saveBook(AsmBook Book) { string savePath = BookRootPath + Book.bookID + ".txt"; if (!Directory.Exists(BookRootPath)) { Directory.CreateDirectory(BookRootPath); } if (!File.Exists(savePath)) { using (FileStream fs = File.Create(savePath)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, Book); } } else { using (FileStream fs = new FileStream(savePath, FileMode.Open)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, Book); } } }
static Queue <string> downloadWaitQ = new Queue <string>(); // 等待下载队列 public AsmDownloadControl() { // 添加BookID线程 Task.Run(() => { while (!parsingQueue.IsCompleted) { string bookID = ""; bookID = parsingQueue.Take(); string html = askBookURL(bookID); AsmBook book = Parsing(html, bookID); if (book == null) { return; } saveBook(book); downloadWaitQ.Enqueue(bookID); } } ); }
/// <summary> /// 解析本子网站获取信息并保存入book /// </summary> /// <param name="html"></param> /// <param name="bookID"></param> /// <returns></returns> private static AsmBook Parsing(string html, string bookID) { if (html.Equals("Error")) { return(null); // 如果html为空则直接退出 } AsmBook book = new AsmBook(); book.bookID = bookID; Regex findTitle1 = new Regex(@"<h1>(?<Title1>[\s\S]*?)</h1>"); Match match1 = findTitle1.Match(html); if (match1.Success) { book.title1 = match1.Groups["Title1"].Value; //book.downloadPath += ("/" + book.title1); } Regex findTitle2 = new Regex(@"<h2>(?<Title2>[\s\S]*?)</h2>"); Match match2 = findTitle2.Match(html); if (match2.Success) { book.title2 = match2.Groups["Title2"].Value; } // new Regex里表示要匹配的字符串 // (?<saveID>[\s\S]*?)表示要匹配的字符串中可变的格式 Regex findSaveID = new Regex(@"images.asmhentai.com/(?<saveID>[\s\S]*?)/" + book.bookID + "/cover.jpg"); Match match3 = findSaveID.Match(html); if (match3.Success) { book.saveID = match3.Groups["saveID"].Value; } Regex findPage = new Regex(@"<h3>Pages: (?<Page>[\s\S]*?)</h3>"); Match match4 = findPage.Match(html); if (match4.Success) { book.page = match4.Groups["Page"].Value; } // 用@时""表示" 不用@时\"表示" Regex findArtist = new Regex(@"href=""/artists/name/(?<Artist>[\s\S]*?)/"">"); MatchCollection match5 = findArtist.Matches(html); book.artists = new string[match5.Count]; for (int i = 0; i < match5.Count; i++) { book.artists[i] = match5[i].Groups["Artist"].Value; } Regex findTags = new Regex(@"href=""/tags/name/(?<Tag>[\s\S]*?)/"">"); MatchCollection match6 = findTags.Matches(html); book.tags = new string[match6.Count]; for (int i = 0; i < match6.Count; i++) { book.tags[i] = match6[i].Groups["Tag"].Value; } int pageCount = int.Parse(book.page) + 1; book.downloadPageURL = new string[pageCount]; book.downloadPageURL[0] = "https://images.asmhentai.com/" + book.saveID + "/" + book.bookID + "/cover.jpg"; for (int i = 1; i < pageCount; i++) { book.downloadPageURL[i] = "https://images.asmhentai.com/" + book.saveID + "/" + book.bookID + "/" + i.ToString() + ".jpg"; } return(book); }