/// <summary> /// 获取在线数据具体实现方法 /// </summary> /// <param name="catalog"></param> /// <param name="retryCount"></param> /// <returns></returns> private async Task <string> GetCatalogContentFromWeb(BookCatalog catalog, int retryCount = 3) { var i = 0; IsCancleRequest = false; string html = null; while (i <= retryCount) { if (IsCancleRequest) { Debug.WriteLine($"用户取消请求操作"); break; } html = await GetHtmlData(catalog.CatalogUrl, false, false); html = AnalisysSourceHelper.AnalisysCatalogContent(catalog.CatalogUrl, html); if (!string.IsNullOrEmpty(html)) { break; } Debug.WriteLine($"加载正文内容失败,第{i + 1}次尝试"); i++; } if (string.IsNullOrEmpty(html)) { Debug.WriteLine("加载正文内容失败,不再次尝试"); } return(html); }
/// <summary> /// 获取目录页数据入口方法 /// </summary> /// <param name="catalog"></param> public void InitCatalogsData(BookCatalog catalog) { Task.Run(() => { if (CurrentBook == null || catalog?.CatalogUrl == null) { return; } if (CurrentBook.IsLocal || CurrentBook.IsTxt) { SetLocalBookCatalogsData(catalog); } else { SetOnlineBookCatalogsData(AnalisysSourceHelper.GetCatalogPageUrl(catalog.CatalogUrl)); } }); }
/// <summary> /// 获取在线所有目录数据具体实现方法 /// </summary> /// <param name="url"></param> /// <param name="retryCount"></param> private async void SetOnlineBookCatalogsData(string url, int retryCount = 3) { try { if (string.IsNullOrEmpty(url)) { return; } IsLoadingCatalogData = true; var i = 0; Tuple <List <BookCatalog>, string, string, string> value = null; while (i <= retryCount) { value = await AnalisysSourceHelper.GetCatalogPageData(url, CurrentBook.BookId); if (value != null) { break; } Debug.WriteLine($"加载目录失败,第{i + 1}次尝试"); i++; } if (value == null) { Debug.WriteLine("加载目录失败,不再次尝试"); return; } DispatcherHelper.CheckBeginInvokeOnUI(() => { if (value.Item1 != null) { if (CurrentBook == null) { return; } CurrentBook.CatalogList = new List <BookCatalog>(); foreach (var bookCatalog in value.Item1) { CurrentBook.CatalogList.Add(bookCatalog); } CatalogCount = CurrentBook.CatalogList.Count; var temp = CurrentBook.CatalogList.FirstOrDefault(p => p.CatalogUrl == CurrentCatalog.CatalogUrl); if (temp != null) { CurrentCatalog = temp; // PreLoadPreAndNextCatalog(); } } CurrentBook.Description = value.Item2; CurrentBook.Cover = value.Item3; CurrentBook.AuthorName = value.Item4; }); } catch (Exception ex) { Debug.WriteLine(ex.Message + "\n" + ex.StackTrace); } finally { IsLoadingCatalogData = false; } }
public async void StartDownload() { try { var taskCount = App.IsPro ? 15 : 1; var groups = Book.CatalogList.Split <BookCatalog>(taskCount); var enumerable = groups as IEnumerable <BookCatalog>[] ?? groups.ToArray(); if (groups == null || !enumerable.Any()) { return; } var tasks = new Task[enumerable.Length]; _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) }; _timer.Tick += Timer_Tick; _timer.Start(); for (var i = 0; i < enumerable.Length; i++) { if (IsDelete) { break; } tasks[i] = await Task.Factory.StartNew(async() => { var catalogs = enumerable[i]; foreach (var bookCatalog in catalogs) { try { if (IsDelete) { break; } bookCatalog.CatalogContent = await AnalisysSourceHelper.GetCatalogContent(bookCatalog.CatalogUrl); } catch (Exception e) { Console.WriteLine(e); } finally { lock (_obj) { _completedCount += 1; _progress = double.Parse(CompletedCount.ToString()) / double.Parse(TotalCount.ToString()) * 100; } } } }); } await Task.Factory.ContinueWhenAll(tasks, (obj) => { if (CompletedCount < TotalCount - 10) { if (!IsDelete) { ToastHelper.ShowMessage(Book.BookName + "下载失败", false); } ViewModelInstance.Instance.DownloadCenter.RemoveDownItem(this); return; } IsCompleted = true; InsertOrUpdateBookCatalogs(Book); DispatcherHelper.CheckBeginInvokeOnUI(() => { _timer?.Stop(); }); }); } catch (Exception e) { Console.WriteLine(e); } }
public void CheckUpdate() { if (CurrentBook.IsTxt) { return; } if (CurrentBook.IsOnline) { Task.Run(async() => { try { var catalogPageUrl = AnalisysSourceHelper.GetCatalogPageUrl(CurrentBook.NewestChapterUrl); var catalogData = await AnalisysSourceHelper.GetCatalogPageData(catalogPageUrl, CurrentBook.BookId); var lastWebCatalog = catalogData?.Item1?.LastOrDefault(); if (lastWebCatalog == null) { return; } if (lastWebCatalog.CatalogUrl == CurrentBook.NewestChapterUrl) { return; } DispatcherHelper.CheckBeginInvokeOnUI(() => { CurrentBook.IsNew = true; CurrentBook.NewestChapterName = lastWebCatalog.CatalogName; CurrentBook.NewestChapterUrl = lastWebCatalog.CatalogUrl; ViewModelInstance.Instance.LocalBookPage.InserOrUpdateBook(CurrentBook); }); } catch (Exception e) { Console.WriteLine(e); } }).ContinueWith((result) => { if (IsDeleted) { return; } }); } else if (CurrentBook.IsLocal) { Task.Run(async() => { try { var localCatalogs = DbLocalBook.SelectBookCatalogsByBookId(AppDataPath.GetLocalBookDbPath(), CurrentBook.BookId); var localLastCatalog = localCatalogs.LastOrDefault(); if (localLastCatalog != null) { DispatcherHelper.CheckBeginInvokeOnUI(() => { CurrentBook.NewestChapterName = localLastCatalog.CatalogName; CurrentBook.NewestChapterUrl = localLastCatalog.CatalogUrl; }); } var catalogPageUrl = AnalisysSourceHelper.GetCatalogPageUrl(CurrentBook.NewestChapterUrl); var catalogData = await AnalisysSourceHelper.GetCatalogPageData(catalogPageUrl, CurrentBook.BookId); var lastWebCatalog = catalogData?.Item1?.LastOrDefault(); if (lastWebCatalog == null) { return; } if (localCatalogs == null || localCatalogs.Count == 0) { NeedUpdateCatalogs = catalogData.Item1; } if (localLastCatalog == null) { return; } if (lastWebCatalog.CatalogUrl == localLastCatalog.CatalogUrl) { return; } var tempCatalog = catalogData.Item1.LastOrDefault(p => p.CatalogUrl == localLastCatalog.CatalogUrl); var tempList = catalogData.Item1.Skip(tempCatalog.Index).ToList(); NeedUpdateCatalogs = tempList; } catch (Exception e) { Console.WriteLine(e); } finally { DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = NeedUpdateCatalogs == null ? "" : NeedUpdateCatalogs?.Count.ToString(); }); } }).ContinueWith((result) => { if (IsDeleted) { return; } if (NeedUpdateCatalogs == null || NeedUpdateCatalogs.Count == 0) { return; } DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = NeedUpdateCatalogs.Count.ToString(); CurrentBook.IsNew = true; CurrentBook.NewestChapterName = NeedUpdateCatalogs.LastOrDefault()?.CatalogName; CurrentBook.NewestChapterUrl = NeedUpdateCatalogs.LastOrDefault()?.CatalogUrl; DbLocalBook.InsertOrUpdatBook(AppDataPath.GetLocalBookDbPath(), CurrentBook); }); StartUpdate(); }); } }
public async void StartUpdate() { try { var taskCount = 3; var groups = NeedUpdateCatalogs.Split <BookCatalog>(taskCount); var enumerable = groups as IEnumerable <BookCatalog>[] ?? groups.ToArray(); if (groups == null || !enumerable.Any()) { return; } IsUpdating = true; var tasks = new Task[enumerable.Length]; for (var i = 0; i < enumerable.Length; i++) { if (IsDeleted) { break; } tasks[i] = await Task.Factory.StartNew(async() => { var catalogs = enumerable[i]; foreach (var bookCatalog in catalogs) { try { if (IsDeleted) { break; } bookCatalog.CatalogContent = await AnalisysSourceHelper.GetCatalogContent(bookCatalog.CatalogUrl); } catch (Exception e) { Console.WriteLine(e); } finally { lock (_obj) { DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = (int.Parse(NeedUpdateCount) - 1).ToString(); }); } } } }); } await Task.Factory.ContinueWhenAll(tasks, (obj) => { DbHelper.AddDbOperator(new Action(() => { var reslut = DbLocalBook.InsertOrUpdateBookCatalogs(AppDataPath.GetLocalBookDbPath(), NeedUpdateCatalogs); })); DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = ""; }); }); } catch (Exception e) { Console.WriteLine(e); } finally { IsUpdating = false; } }