private void backgroundworker_download_DoWork(object sender, DoWorkEventArgs e) { var chapters = mChapers; var pb = progressbar_download; var label = label_downloadinfo; string savePath = e.Argument.ToString(); List <ChapterInfo> chaperInfoList = new List <ChapterInfo>(); for (int i = 0; i < chapters.Length; i++) { if (backgroundworker_download.CancellationPending) { return; } var chapter = chapters[i]; float progress = (float)(i + 1) / (float)chapters.Length; string info = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length, progress * 100); backgroundworker_download.ReportProgress(i, info); try { var chapterInfo = LibZhuiShu.getChapter(chapter.link); if (chapterInfo != null) { chaperInfoList.Add(chapterInfo); } else { MessageBox.Show("下载失败:" + chapter.title); return; } } catch (Exception exc) { MessageBox.Show("下载失败,请切换书源后重试:" + exc); return; } } backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后...."); string ext = Path.GetExtension(savePath); Book book = new Book(); book.name = mBook.title; book.author = mBook.author; book.id = mBook._id; book.chapters = chaperInfoList.ToArray(); if (ext.ToLower() == ".txt") { Kindlegen.book2Txt(book, savePath); } else if (ext.ToLower() == ".mobi") { Kindlegen.book2Mobi(book, savePath); } MessageBox.Show("下载完成,文件保存在:" + savePath); }
void DownLoadThread() { System.Diagnostics.Debug.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " Start"); while (mChaptersDownloadQueue.Count > 0) { ChapterDownloadContext context = null; lock (mChaptersDownloadQueue) { context = mChaptersDownloadQueue.Dequeue(); } // 每个源尝试下载3次 foreach (var link in context.links) { for (int i = 0; i < 3; i++) { System.Diagnostics.Debug.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " " + context.title + " " + link); try { context.content = LibZhuiShu.getChapter(link).body; break; } catch (Exception ex) { loglibrary.LogHelper.Error(ex); loglibrary.LogHelper.Flush(); } } if (!string.IsNullOrEmpty(context.content)) { break; } } if (!string.IsNullOrEmpty(context.content)) { System.Diagnostics.Debug.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " complate " + context.title); lock (_lock_obj) { mChaptersDownloadComplteCount++; } } else { // 下载失败 OnFail(); return; } } System.Diagnostics.Debug.WriteLine("Thread:" + Thread.CurrentThread.ManagedThreadId + " Quit"); }
static void Main(string[] args) { var result = LibZhuiShu.autoComplate("完美"); var books = LibZhuiShu.fuzzySearch("完美世界", 0, 1); foreach (var book in books) { Console.WriteLine(string.Format("{0} {1} {2}", book._id, book.title, book.author)); var tocs = LibZhuiShu.getTocSummary(book._id); var chapertList = LibZhuiShu.getChaperList(tocs[0]._id); Console.WriteLine(LibZhuiShu.getChapter(chapertList.chapters[0].link).body); } foreach (var item in result) { Console.WriteLine("章节:" + item); Console.ReadKey(); } }
private void Backgroundworker_download_DoWork(object sender, DoWorkEventArgs e) { var chapters = mChapers; var pb = progressbar_download; var label = label_downloadinfo; string savePath = e.Argument.ToString(); List <ChapterInfo> chaperInfoList = new List <ChapterInfo>(); for (int i = 0; i < chapters.Length; i++) { if (backgroundworker_download.CancellationPending) { return; } var chapter = chapters[i]; float progress = (i + 1) / (float)chapters.Length; string info = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length + 1, progress * 100); backgroundworker_download.ReportProgress(i, info); while (true) { bool downloadSucess = false; string errMsg = ""; for (int j = 0; j < 3; j++) { try { var chapterInfo = LibZhuiShu.getChapter(chapter.link); if (chapterInfo != null) { chaperInfoList.Add(chapterInfo); downloadSucess = true; break; } } catch (Exception ex) { errMsg = ex.Message; loglibrary.LogHelper.Error(ex); loglibrary.LogHelper.Flush(); } } if (!downloadSucess) { var result = MessageBox.Show(errMsg, "章节 " + chapter.title + " 下载失败", MessageBoxButtons.AbortRetryIgnore); if (result == DialogResult.Abort) { return; } else if (result == DialogResult.Ignore) { var emptyChaper = new ChapterInfo() { title = chapter.title, body = "本章下载失败了,失败原因:\n " + errMsg }; chaperInfoList.Add(emptyChaper); downloadSucess = true; break; } } else { break; } } } backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后...."); string ext = Path.GetExtension(savePath); Book book = new Book() { name = mBook.title, author = mBook.author, id = mBook._id, chapters = chaperInfoList.ToArray() }; if (ext.ToLower() == ".txt") { Kindlegen.Book2Txt(book, savePath); } else if (ext.ToLower() == ".mobi") { Kindlegen.Book2Mobi(book, savePath); } MessageBox.Show("下载完成,文件保存在:" + savePath); if (checkBox1.Checked) { try { KindleSender.Service.Program.Main(); Configuration config = new Configuration() { SmtpServer = textBox4.Text, SmtpPort = 25, SmtpPassword = textBox3.Text, SmtpUserName = textBox5.Text, KindleMail = textBox1.Text, FolderPath = savePath.Replace(Path.GetFileName(savePath), string.Empty) }; FileSender sen = new FileSender(config); sen.Send(savePath); MessageBox.Show("已完成!"); } catch (Exception ee) { loglibrary.LogHelper.Error(ee); loglibrary.LogHelper.Flush(); MessageBox.Show("错误!\n 错误信息为:" + ee.Message); } } }
private void backgroundworker_download_DoWork(object sender, DoWorkEventArgs e) { var chapters = mChapers; var pb = progressbar_download; var label = label_downloadinfo; string savePath = e.Argument.ToString(); List <ChapterInfo> chaperInfoList = new List <ChapterInfo>(); for (int i = 0; i < chapters.Length; i++) { if (backgroundworker_download.CancellationPending) { return; } var chapter = chapters[i]; float progress = (float)(i + 1) / (float)chapters.Length; string info = string.Format("正在下载:{0} {1}/{2} {3:F2}%", chapter.title, i + 1, chapters.Length, progress * 100); backgroundworker_download.ReportProgress(i, info); while (true) { bool downloadSucess = false; string errMsg = ""; for (int j = 0; j < 3; j++) { try { var chapterInfo = LibZhuiShu.getChapter(chapter.link); if (chapterInfo != null) { chaperInfoList.Add(chapterInfo); downloadSucess = true; break; } } catch (Exception ex) { errMsg = ex.Message; } } if (!downloadSucess) { var result = MessageBox.Show(errMsg, "章节 " + chapter.title + " 下载失败", MessageBoxButtons.AbortRetryIgnore); if (result == DialogResult.Abort) { return; } else if (result == DialogResult.Ignore) { var emptyChaper = new ChapterInfo(); emptyChaper.title = chapter.title; emptyChaper.body = "本章下载失败了,失败原因:\n " + errMsg; chaperInfoList.Add(emptyChaper); downloadSucess = true; break; } } else { break; } } } backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后...."); string ext = Path.GetExtension(savePath); Book book = new Book(); book.name = mBook.title; book.author = mBook.author; book.id = mBook._id; book.chapters = chaperInfoList.ToArray(); if (ext.ToLower() == ".txt") { Kindlegen.book2Txt(book, savePath); } else if (ext.ToLower() == ".mobi") { Kindlegen.book2Mobi(book, savePath); } MessageBox.Show("下载完成,文件保存在:" + savePath); }