Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
1.显示书源连接速度
Browse files Browse the repository at this point in the history
2.下载失败自动重试
  • Loading branch information
Quanwei1992 committed Dec 20, 2016
1 parent 5c5e8ad commit cc0dc6e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 10 deletions.
35 changes: 25 additions & 10 deletions KindleHelper/FormBookDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,34 @@ private void backgroundworker_download_DoWork(object sender, DoWorkEventArgs e)
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;

while (true)
{
bool downloadSucess = false;
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 exc){}
}
if (!downloadSucess) {
var result = MessageBox.Show("章节 " + chapter.title + " 下载失败,是否重试?","下载失败",MessageBoxButtons.YesNo);
if (result != DialogResult.Yes) {
return;
}
}
} catch (Exception exc) {
MessageBox.Show("下载失败,请切换书源后重试:" + exc);
return;
}



}
backgroundworker_download.ReportProgress(chapters.Length, "正在生成电子书请稍后....");
string ext = Path.GetExtension(savePath);
Expand Down
59 changes: 59 additions & 0 deletions KindleHelper/FormTocList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public void ShowTocList(MixTocInfo mixToc,TocSummmaryInfo[] tocs,Action<int> cal
}

listview_toc.EndUpdate();
PingToc();
this.Show();

}

private void listview_toc_DoubleClick(object sender, EventArgs e)
Expand All @@ -62,5 +64,62 @@ private void listview_toc_DoubleClick(object sender, EventArgs e)
this.Close();
}
}


void PingToc()
{
foreach (var toc in mTocs)
{
RunAsync(() => {
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
st.Start();
try
{

HttpHelper.GET(toc.link);

//toc.link;
}
catch (Exception ex)
{

}
st.Stop();
System.Diagnostics.Debug.WriteLine(toc.name + " " + st.ElapsedMilliseconds + " ms");
RunInMainthread(()=> {
var item = listview_toc.FindItemWithText(toc.name);
item.Text = toc.name + "(" + st.ElapsedMilliseconds + " ms)";
});
});


}
}

void RunAsync(Action action)
{
try
{
((Action)(delegate ()
{
action?.Invoke();
})).BeginInvoke(null, null);
}
catch (Exception e) { }

}

void RunInMainthread(Action action)
{
try
{
this.BeginInvoke((Action)(delegate ()
{
action?.Invoke();
}));
}
catch (Exception e) { }

}
}
}
1 change: 1 addition & 0 deletions KindleHelper/KindleHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<DependentUpon>FormTocList.cs</DependentUpon>
</Compile>
<Compile Include="lib\Book.cs" />
<Compile Include="lib\BookDownloader.cs" />
<Compile Include="lib\Kindlegen.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
135 changes: 135 additions & 0 deletions KindleHelper/lib/BookDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using libZhuishu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace KindleHelper
{
public class BookDownloader
{


public class ChapterDownloadContext
{
public string title;
public string content;
public List<string> links = new List<string>();
}


private string mBookID;

public BookDownloader(string id)
{
mBookID = id;
}


List<ChapterDownloadContext> mChapters = new List<ChapterDownloadContext>();
Queue<ChapterDownloadContext> mChaptersDownloadQueue = new Queue<ChapterDownloadContext>();
private object _lock_obj = new object();
int mChaptersDownloadComplteCount = 0;
List<Thread> mWorkThreads = new List<Thread>();
public void StartDownload()
{
// 获得章节列表和所有书源
var mixTocInfo = LibZhuiShu.getMixToc(mBookID);
// 获得所有书源
var tocs = LibZhuiShu.getTocSummary(mBookID);
List<TocChaperListInfo> tocChaperListInfoList = new List<TocChaperListInfo>();
foreach (var toc in tocs)
{
if (toc.name == "优质书源") continue;
var tocChaperList = LibZhuiShu.getChaperList(toc._id);
tocChaperListInfoList.Add(tocChaperList);
}
foreach (var chapter in mixTocInfo.chapters) {
ChapterDownloadContext context = new ChapterDownloadContext();
context.title = chapter.title;
context.links.Add(chapter.link);
foreach (var tocChaterListInfo in tocChaperListInfoList) {
foreach (var tocChapter in tocChaterListInfo.chapters){
if (tocChapter.title.Replace(" ","").ToLower() == context.title.Replace(" ", "").ToLower()) {
if (!context.links.Contains(tocChapter.link)) {
context.links.Add(tocChapter.link);
}
break;
}
}
}
mChapters.Add(context);
mChaptersDownloadQueue.Enqueue(context);
}

for (int i = 0; i < 10; i++) {
Thread workThread = new Thread(DownLoadThread);
mWorkThreads.Add(workThread);
workThread.Start();
}

}

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)
{

}
}

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");
}

void OnFail()
{
foreach (var workThread in mWorkThreads)
{
workThread.Abort();
}

System.Diagnostics.Debug.WriteLine("download failed!");
}

}
}

0 comments on commit cc0dc6e

Please sign in to comment.