diff --git a/EHDownloader/Book.cs b/EHDownloader/Book.cs index 69c2c03..5d0cbb3 100644 --- a/EHDownloader/Book.cs +++ b/EHDownloader/Book.cs @@ -29,33 +29,40 @@ namespace EHDownloader DownloadImageFailed, } - enum BookStates + enum BookStates:int { [Description("等待")] - Wait, + Wait = 0, //[Description("擷取頁面失敗")] //FetchError, [Description("載入首頁失敗")] - LoadFailed, + LoadFailed = -1, [Description("擷取標題失敗")] - FetchTitleFailed, + FetchTitleFailed = -2, [Description("擷取頁集合失敗")] - FetchPageSetFailed, + FetchPageSetFailed = -3, [Description("擷取頁連結失敗")] - FetchPageLinkFailed, + FetchPageLinkFailed = -4, + [Description("下載完成, 但是有些頁面出錯")] + DownloadFinishedButSomePageError = -5, [Description("擷取頁面完成")] - FetchCompleted, - - - + FetchCompleted = 1, [Description("下載完成")] - DownloadCompleted, - [Description("下載完成, 但是有些頁面出錯")] - DownloadFinishedButSomePageError, - Compressed, + DownloadCompleted = 2, + [Description("下載並封裝完成")] + Compressed = 3, } + static class BookStateHelper + { + static public bool IsCompleted(this BookStates? state) + { + return state.HasValue && ((int)state.Value) >= (int)BookStates.Compressed; + } + } + + class Book { private const string PageSetXPath = @"//table[@class='ptb']//a"; @@ -74,7 +81,7 @@ namespace EHDownloader public List Pages { get; private set; } - public int PageCount => Pages.Count; + public int PageCount => Pages?.Count ?? 0; public BookStates State { get; set; } = BookStates.Wait; diff --git a/EHDownloader/DbContext.cs b/EHDownloader/DbContext.cs index 945ef00..7853645 100644 --- a/EHDownloader/DbContext.cs +++ b/EHDownloader/DbContext.cs @@ -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(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(query, new { url }).FirstOrDefault(); + return (BookStates)state; + } + } + public void InsertBook(Book book) { 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())) { 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); } } diff --git a/EHDownloader/Form1.cs b/EHDownloader/Form1.cs index 8ecb9ca..048988f 100644 --- a/EHDownloader/Form1.cs +++ b/EHDownloader/Form1.cs @@ -106,18 +106,32 @@ namespace EHDownloader { if (!isEhUrl(url)) return; if (lv_Tasks.Items.ContainsKey(url)) return; - if (_dbContext.BookIsExists(url)) return; - ListViewItem lvi = new ListViewItem(url) { Name = url }; - lvi.SubItems.Add("Wait"); + // 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 }; + lvi.SubItems.Add("Wait"); lvi.Text = url; lvi.Tag = book; lv_Tasks.Items.Add(lvi); await TryStartTask(lvi); - - } private async Task TryStartTask() @@ -140,14 +154,16 @@ namespace EHDownloader { case BookStates.Wait: await book.FetchAsync(); + _dbContext.UpdateBook(book); break; case BookStates.FetchCompleted: await book.DownloadBookAsync(new Progress( (info) => lvi.SubItems[1].Text = $"下載中 {info.PageNumber}/{info.PageCount}")); + _dbContext.UpdateBook(book); break; case BookStates.DownloadCompleted: await book.CompressAsync(); - _dbContext.InsertBook(book); + _dbContext.UpdateBook(book); goto default; case BookStates.Compressed: case BookStates.LoadFailed: