public void AddChapter(string chapterName, string address) { NovelChapter nc = new NovelChapter(chapterName); nc.ChapterAddress = address; this.novelChapters.Add(nc); }
/// <summary> /// 从网站中获取指定章节的内容,返回章节内容的字节 /// </summary> /// <param name="needDownloadChapter"></param> /// <param name="request"></param> /// <param name="response"></param> /// <returns></returns> private string GetResponseStringFromServer(NovelChapter needDownloadChapter, HttpWebRequest request, HttpWebResponse response) { Stream dataStream = null; StreamReader reader = null; string responseFromServer = ""; for (int loop = 0; loop < this.repeatCount; loop++) { try { request = (HttpWebRequest)HttpWebRequest.Create(needDownloadChapter.ChapterAddress); request.Credentials = CredentialCache.DefaultCredentials; request.Timeout = 60000; response = (HttpWebResponse)request.GetResponse(); request.KeepAlive = false; dataStream = response.GetResponseStream(); reader = new StreamReader(dataStream, Encoding.Default); responseFromServer = reader.ReadToEnd(); if (responseFromServer == "") { continue; } if (regHtmlEnd.Match(responseFromServer).Success) { break; } else { continue; } } catch { responseFromServer = ""; Thread.Sleep(1000); } finally { if (reader != null) { reader.Close(); } if (dataStream != null) { dataStream.Close(); } if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } } return(responseFromServer); }
/// <summary> /// 改变章节的获取状态 /// </summary> /// <param name="nc"></param> /// <param name="cs"></param> private void ChangeGetState(NovelChapter nc, ChapterState cs) { lock (this.thisLock) { nc.ChapterState = cs; } if (this.NovelDirectoryListUpdated != null) { this.NovelDirectoryListUpdated(this); } }
private void SaveToSql() { novelID = Globals.MysqlHelper.GetNovelID(book.NovelName, book.Author); if (novelID < 0) { if (Globals.MysqlHelper.InsertNewNovel(book.NovelName, book.Author)) { novelID = Globals.MysqlHelper.GetNovelID(book.NovelName, book.Author); } } if (novelID >= 0) { for (int i = 0; i < this.book.NovelChapters.Count; i++) { UpdateCaption(book.NovelName + ":正在保存到数据库" + i.ToString() + "/" + book.NovelChapters.Count.ToString()); NovelChapter nc = this.book.NovelChapters[i]; if (nc != null) { if (nc.FileName != null && nc.FileName != string.Empty && nc.FileName != "") { if (nc.FileName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { if (File.Exists(this.book.LocalFolder + "\\" + nc.FileName)) { int chapterID = Convert.ToInt32(nc.FileName.Substring(0, nc.FileName.IndexOf('.'))); StreamReader sr = new StreamReader(this.book.LocalFolder + "\\" + nc.FileName); string s = sr.ReadToEnd(); sr.Close(); if (Globals.MysqlHelper.IsExistChapterID(novelID, chapterID)) { Globals.MysqlHelper.UpdateChapter(novelID, chapterID, this.book.NovelChapters[i].ChapterName, s); } else { Globals.MysqlHelper.InsertNewChapter(novelID, chapterID, this.book.NovelChapters[i].ChapterName, s); } } } else if (nc.FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) || nc.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)) { } } } } } UpdateCaption(book.NovelName); }
private void LastHandleResponseStringAndSave(string preHandleString, NovelChapter nc, int fileIndex) { string[] temp = preHandleString.Split(new string[] { "</div>" }, StringSplitOptions.RemoveEmptyEntries); string strForSave = temp[0].Replace("<div>", string.Empty); temp = null; strForSave = strForSave.Replace("纵横书海", string.Empty); strForSave = strForSave.Replace("www.dodbook.com", string.Empty); FileStream fs = System.IO.File.Create(this.novel.LocalFolder + "\\" + GenerateSaveFileName(fileIndex)); StreamWriter sw = new StreamWriter(fs); sw.Write(strForSave); temp = null; sw.Flush(); sw.Close(); fs.Close(); nc.FileName = GenerateSaveFileName(fileIndex); this.ChangeGetState(nc, ChapterState.完成); }
/// <summary> /// 获取要下载的章节对象 /// </summary> /// <returns></returns> private NovelChapter GetNeedDownloadChapter(ref int fileIndex) { NovelChapter needDownloadChapter = null; lock (this.thisLock) { for (int i = 0; i < this.novel.NovelChapters.Count; i++) { if (this.novel.NovelChapters[i].ChapterState == ChapterState.等待中) { fileIndex = i; needDownloadChapter = this.novel.NovelChapters[i]; needDownloadChapter.ChapterState = ChapterState.获取中; break; } } } if (this.NovelDirectoryListUpdated != null) { this.NovelDirectoryListUpdated(this); } return(needDownloadChapter); }
private void treeListDirectory_MouseClick(object sender, MouseEventArgs e) { DevExpress.XtraTreeList.TreeListHitInfo hi = this.treeListDirectory.CalcHitInfo(new Point(e.X, e.Y)); if (hi.Node == null) { return; } this.barTool.Visible = false; this.isChangeByLoad = true; //MessageBox.Show("test"); NovelChapter nc = this.treeListDirectory.GetDataRecordByNode(hi.Node) as NovelChapter; if (nc != null) { if (nc.FileName != null && nc.FileName != string.Empty && nc.FileName != "") { if (nc.FileName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) { if (File.Exists(this.book.LocalFolder + "\\" + nc.FileName)) { //FileStream fs = new FileStream(System.Windows.Forms.Application.StartupPath + "\\" + nc.FileName, FileMode.Open); StreamReader sr = new StreamReader(this.book.LocalFolder + "\\" + nc.FileName); string s = sr.ReadToEnd(); sr.Close(); //fs.Close(); this.memoEditTxt.Text = s; this.memoEditTxt.Tag = nc.FileName; } this.xtraScrollableControlImg.Visible = false; this.memoEditTxt.Visible = true; } else if (nc.FileName.EndsWith(".gif", StringComparison.OrdinalIgnoreCase) || nc.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)) { this.xtraScrollableControlImg.Controls.Clear(); string[] filelist = nc.FileName.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); int imglocationheight = 10; for (int i = 0; i < filelist.Length; i++) { //MessageBox.Show(Application.ExecutablePath); //MessageBox.Show(this.book.LocalFolder + "\\" + filelist[i]);//http://www.biquge.com/0_46/ if (File.Exists(this.book.LocalFolder + "\\" + filelist[i])) { PictureEdit pe = new PictureEdit(); pe.Image = Image.FromFile(this.book.LocalFolder + "\\" + filelist[i]); pe.Size = new Size(pe.Image.Width, pe.Image.Height); pe.Location = new Point(0, imglocationheight); imglocationheight = imglocationheight + pe.Image.Height + 10; this.xtraScrollableControlImg.Controls.Add(pe); } else { } } this.xtraScrollableControlImg.Visible = true; this.memoEditTxt.Visible = false; } } } }
private void GetContentDelegate() { HttpWebRequest request = null; HttpWebResponse response = null; int fileIndex = 0; //获取要下载的章节对象 NovelChapter needDownloadChapter = this.GetNeedDownloadChapter(ref fileIndex); while (needDownloadChapter != null) { //开始下载html文件任务 string responseFromServer = null; //获取指定章节页的网页源码 responseFromServer = this.GetResponseStringFromServer(needDownloadChapter, request, response); if (responseFromServer == null || responseFromServer == "") { this.ChangeGetState(needDownloadChapter, ChapterState.获取失败); } else { //对获取的网页源码作预处理 string preHandleString = this.PreHandleResponseString(responseFromServer); if (preHandleString == "") { this.ChangeGetState(needDownloadChapter, ChapterState.获取失败); } else { int index = preHandleString.IndexOf("<img"); if (index > 0) { this.DownloadAllChapterImg(needDownloadChapter, preHandleString, fileIndex); } else { this.LastHandleResponseStringAndSave(preHandleString, needDownloadChapter, fileIndex); } } } Thread.Sleep(500); //判断是否要退出当前线程 string threadName = Thread.CurrentThread.Name; Trace.Write(threadName); Trace.Write(threadName.Substring(16, threadName.Length - 16)); if (threadName.StartsWith("GetContentThread")) { if (int.Parse(threadName.Substring(16, threadName.Length - 16)) > this.threadCount - 1) { break; } } if (this.isStopDownload) { break; } //继续下一个章节下载 needDownloadChapter = this.GetNeedDownloadChapter(ref fileIndex); } lock (this.thisLock2) { this.activatedThreadCount--; if (this.activatedThreadCount == 0) { if (this.HasAllThreadStopped != null) { this.HasAllThreadStopped(this); } } } }
/// <summary> /// 从网页内容中取出章节图片地址并下载 /// </summary> /// <param name="nc"></param> /// <param name="contentString"></param> private void DownloadAllChapterImg(NovelChapter nc, string contentString, int fileIndex) { Uri uri = new Uri(nc.ChapterAddress); WebClient webclient = new WebClient(); webclient.Credentials = CredentialCache.DefaultCredentials; string[] temp = contentString.Split(new string[] { "<img src=\"" }, StringSplitOptions.RemoveEmptyEntries); string imgFilesName = ""; //当前文件的名称(带后缀) string filenamewithExt = ""; string imgPath; bool downloadSuccess = false; for (int j = 0; j < temp.Length; j++) { imgPath = ""; filenamewithExt = ""; int index3 = temp[j].IndexOf('"'); if (index3 < 5) { continue; } string str = temp[j].Substring(0, index3); if (str.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || str.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)) { if (str.StartsWith("/")) { imgPath = "http://" + uri.Host + str; } else { imgPath = nc.ChapterAddress.Substring(0, nc.ChapterAddress.LastIndexOf("/")) + "/" + str; } filenamewithExt = fileIndex.ToString() + "_" + j.ToString() + imgPath.Substring(imgPath.Length - 4, 4); for (int loop = 0; loop < this.repeatCount; loop++) { try { webclient.DownloadFile(imgPath, this.novel.LocalFolder + "\\" + filenamewithExt); downloadSuccess = true; break; } catch { Thread.Sleep(1000); } finally { } if (loop == 4) { downloadSuccess = false; } } if (imgFilesName == "") { imgFilesName = filenamewithExt; } else { imgFilesName = imgFilesName + "|" + filenamewithExt; } //如果其中一张图片下载失败,停止下载该章的其它图片 if (downloadSuccess == false) { break; } } } webclient.Dispose(); if (downloadSuccess) { nc.FileName = imgFilesName; this.ChangeGetState(nc, ChapterState.完成); } else { this.ChangeGetState(nc, ChapterState.获取失败); } }