調整邏輯: 加入連結馬上加入資料庫,每個工作階段均 update state
This commit is contained in:
@@ -29,33 +29,40 @@ namespace EHDownloader
|
|||||||
DownloadImageFailed,
|
DownloadImageFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum BookStates
|
enum BookStates:int
|
||||||
{
|
{
|
||||||
[Description("等待")]
|
[Description("等待")]
|
||||||
Wait,
|
Wait = 0,
|
||||||
//[Description("擷取頁面失敗")]
|
//[Description("擷取頁面失敗")]
|
||||||
//FetchError,
|
//FetchError,
|
||||||
[Description("載入首頁失敗")]
|
[Description("載入首頁失敗")]
|
||||||
LoadFailed,
|
LoadFailed = -1,
|
||||||
[Description("擷取標題失敗")]
|
[Description("擷取標題失敗")]
|
||||||
FetchTitleFailed,
|
FetchTitleFailed = -2,
|
||||||
[Description("擷取頁集合失敗")]
|
[Description("擷取頁集合失敗")]
|
||||||
FetchPageSetFailed,
|
FetchPageSetFailed = -3,
|
||||||
[Description("擷取頁連結失敗")]
|
[Description("擷取頁連結失敗")]
|
||||||
FetchPageLinkFailed,
|
FetchPageLinkFailed = -4,
|
||||||
|
[Description("下載完成, 但是有些頁面出錯")]
|
||||||
|
DownloadFinishedButSomePageError = -5,
|
||||||
|
|
||||||
[Description("擷取頁面完成")]
|
[Description("擷取頁面完成")]
|
||||||
FetchCompleted,
|
FetchCompleted = 1,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[Description("下載完成")]
|
[Description("下載完成")]
|
||||||
DownloadCompleted,
|
DownloadCompleted = 2,
|
||||||
[Description("下載完成, 但是有些頁面出錯")]
|
[Description("下載並封裝完成")]
|
||||||
DownloadFinishedButSomePageError,
|
Compressed = 3,
|
||||||
Compressed,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class BookStateHelper
|
||||||
|
{
|
||||||
|
static public bool IsCompleted(this BookStates? state)
|
||||||
|
{
|
||||||
|
return state.HasValue && ((int)state.Value) >= (int)BookStates.Compressed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Book
|
class Book
|
||||||
{
|
{
|
||||||
private const string PageSetXPath = @"//table[@class='ptb']//a";
|
private const string PageSetXPath = @"//table[@class='ptb']//a";
|
||||||
@@ -74,7 +81,7 @@ namespace EHDownloader
|
|||||||
|
|
||||||
public List<Page> Pages { get; private set; }
|
public List<Page> Pages { get; private set; }
|
||||||
|
|
||||||
public int PageCount => Pages.Count;
|
public int PageCount => Pages?.Count ?? 0;
|
||||||
|
|
||||||
public BookStates State { get; set; } = BookStates.Wait;
|
public BookStates State { get; set; } = BookStates.Wait;
|
||||||
|
|
||||||
|
|||||||
@@ -99,6 +99,29 @@ namespace EHDownloader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool BookIsCompleted(string url)
|
||||||
|
{
|
||||||
|
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||||
|
{
|
||||||
|
var query =
|
||||||
|
"select State from Book where Url=(@url)";
|
||||||
|
int? state = cn.Query<int?>(query, new { url }).FirstOrDefault();
|
||||||
|
return ((BookStates?)state).IsCompleted();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// null: book not exists
|
||||||
|
public BookStates? QueryBookState(string url)
|
||||||
|
{
|
||||||
|
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||||
|
{
|
||||||
|
var query =
|
||||||
|
"select State from Book where Url=(@url)";
|
||||||
|
int? state = cn.Query<int?>(query, new { url }).FirstOrDefault();
|
||||||
|
return (BookStates)state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void InsertBook(Book book)
|
public void InsertBook(Book book)
|
||||||
{
|
{
|
||||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||||
@@ -109,12 +132,13 @@ namespace EHDownloader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateBookStatus(Book book)
|
public void UpdateBook(Book book)
|
||||||
{
|
{
|
||||||
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
using (var cn = new SQLiteConnection(Settings.GetConnectString()))
|
||||||
{
|
{
|
||||||
var updateScript =
|
var updateScript =
|
||||||
"UPDATE Book SET (State=@State) WHERE Url=@Url";
|
"UPDATE Book SET Title=@Title,PageCount=@PageCount,DownloadDateTime=@DownloadDateTime,State=@State,Path=@Path " +
|
||||||
|
"WHERE Url=@Url";
|
||||||
cn.Execute(updateScript, book);
|
cn.Execute(updateScript, book);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,18 +106,32 @@ namespace EHDownloader
|
|||||||
{
|
{
|
||||||
if (!isEhUrl(url)) return;
|
if (!isEhUrl(url)) return;
|
||||||
if (lv_Tasks.Items.ContainsKey(url)) return;
|
if (lv_Tasks.Items.ContainsKey(url)) return;
|
||||||
if (_dbContext.BookIsExists(url)) return;
|
// if (_dbContext.BookIsExists(url)) return;
|
||||||
|
await newTask(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task newTask(string url)
|
||||||
|
{
|
||||||
|
BookStates? newBookState = _dbContext.QueryBookState(url);
|
||||||
|
if (newBookState.IsCompleted())
|
||||||
|
{
|
||||||
|
// 已完成
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Book book = new Book(url, _folder);
|
||||||
|
if (!newBookState.HasValue)
|
||||||
|
_dbContext.InsertBook(book);
|
||||||
|
else
|
||||||
|
_dbContext.UpdateBook(book);
|
||||||
|
|
||||||
|
|
||||||
ListViewItem lvi = new ListViewItem(url) { Name = url };
|
ListViewItem lvi = new ListViewItem(url) { Name = url };
|
||||||
lvi.SubItems.Add("Wait");
|
lvi.SubItems.Add("Wait");
|
||||||
|
|
||||||
Book book = new Book(url, _folder);
|
|
||||||
lvi.Text = url;
|
lvi.Text = url;
|
||||||
lvi.Tag = book;
|
lvi.Tag = book;
|
||||||
|
|
||||||
lv_Tasks.Items.Add(lvi);
|
lv_Tasks.Items.Add(lvi);
|
||||||
await TryStartTask(lvi);
|
await TryStartTask(lvi);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task TryStartTask()
|
private async Task TryStartTask()
|
||||||
@@ -140,14 +154,16 @@ namespace EHDownloader
|
|||||||
{
|
{
|
||||||
case BookStates.Wait:
|
case BookStates.Wait:
|
||||||
await book.FetchAsync();
|
await book.FetchAsync();
|
||||||
|
_dbContext.UpdateBook(book);
|
||||||
break;
|
break;
|
||||||
case BookStates.FetchCompleted:
|
case BookStates.FetchCompleted:
|
||||||
await book.DownloadBookAsync(new Progress<DownloadProgressInfo>(
|
await book.DownloadBookAsync(new Progress<DownloadProgressInfo>(
|
||||||
(info) => lvi.SubItems[1].Text = $"下載中 {info.PageNumber}/{info.PageCount}"));
|
(info) => lvi.SubItems[1].Text = $"下載中 {info.PageNumber}/{info.PageCount}"));
|
||||||
|
_dbContext.UpdateBook(book);
|
||||||
break;
|
break;
|
||||||
case BookStates.DownloadCompleted:
|
case BookStates.DownloadCompleted:
|
||||||
await book.CompressAsync();
|
await book.CompressAsync();
|
||||||
_dbContext.InsertBook(book);
|
_dbContext.UpdateBook(book);
|
||||||
goto default;
|
goto default;
|
||||||
case BookStates.Compressed:
|
case BookStates.Compressed:
|
||||||
case BookStates.LoadFailed:
|
case BookStates.LoadFailed:
|
||||||
|
|||||||
Reference in New Issue
Block a user