public void Download(DownloadItem downloadItem) { lock (_lock4) { var source = _sourceManager.GetSourceFromLink(downloadItem.Hq.Link); if (downloadItem.Hq.Chapters == null || downloadItem.Hq.Chapters.Count() == 0) { var hq = downloadItem.Hq; if ((_downloadContext.Chapter.Find().Where(x => x.Hq == hq && x.ToDownload == true).Execute() is List <Chapter> chaps) && chaps.Count > 0) { downloadItem.Hq.Chapters = chaps; } else { hq = new Hq(); source.GetInfo(downloadItem.Hq.Link, out hq); if (hq != null) { downloadItem.Hq = hq; } } } downloadItem.DownloadStarted = DateTime.Now; DownloadEventHub.OnDownloadStart(this, new DownloadEventArgs(downloadItem)); var hqDirectory = _directoryHelper.CreateHqDirectory(downloadItem.DownloadLocation, downloadItem.Hq.Title); var numChapters = downloadItem.Hq.Chapters.Count(); var chapAtual = 1; var failedToDownload = new List <String>(); foreach (var chapter in downloadItem.Hq.Chapters) { if (_stop) { DownloadEventHub.OnDownloadStop(this, new DownloadEventArgs(downloadItem, (downloadItem.DownloadFinished - downloadItem.DownloadStarted), failedToDownload)); break; } try { downloadItem.ActualPage = null; SaveChapter(source, downloadItem, chapter, chapAtual, numChapters, hqDirectory); } catch (Exception e) { DownloadEventHub.OnDownloadError(this, new DownloadErrorEventArgs(downloadItem, e, DateTime.Now)); failedToDownload.Add(chapter.Link); } chapAtual++; } downloadItem.DownloadFinished = DateTime.Now; var downloadInfo = new HqDownloadInfo(downloadItem); downloadInfo.Path = hqDirectory; downloadItem.IsDownloaded = true; _downloadContext.DownloadList.SaveOrReplace(downloadItem); _downloadInfoHelper.SaveDownloadInfo(downloadInfo); DownloadEventHub.OnDownloadEnd(this, new DownloadEventArgs(downloadItem, (downloadItem.DownloadFinished - downloadItem.DownloadStarted), failedToDownload)); } }
public void Download(DownloadItem downloadItem) { lock (_lock4) { var hq = downloadItem.Hq.ToObject <Hq>(); var source = _sourceManager.GetSourceFromLink(hq.Link); if (hq.Chapters == null || hq.Chapters.Count() == 0) { source.GetInfo(hq.Link, out hq, 5); } downloadItem.DownloadStarted = DateTime.Now; DownloadEventHub.OnDownloadStart(this, new DownloadEventArgs(downloadItem)); var hqDirectory = _directoryHelper.CreateHqDirectory(downloadItem.DownloadLocation, hq.Title); var numChapters = hq.Chapters.Count(); downloadItem.NumChapters = numChapters; var chapAtual = 1; var failedToDownload = new List <String>(); var coverSource = hq.CoverSource; var hqTitle = StringHelper.RemoveSpecialCharacters(hq.Title); hq.Title = hqTitle; coverSource = $"{hqDirectory}\\{hqTitle}{FormatPage(coverSource)}"; if (!File.Exists(coverSource)) { using (var webClient = new HttpClient()) { using (var response = webClient.GetAsync(hq.CoverSource).Result) { var imageByte = response.Content.ReadAsByteArrayAsync().Result; using (var binaryWriter = new BinaryWriter(new FileStream(coverSource, FileMode.Append, FileAccess.Write))) { binaryWriter.Write(imageByte); } } } } hq.CoverSource = coverSource; var chapters = hq.Chapters; var hqDownloaded = new DownloadedHq() { Hq = hq, Location = hqDirectory, Date = DateTime.Now }; _downloadHelper.SaveDownloadedHq(hqDownloaded); foreach (var chapter in chapters) { downloadItem.ActualChapterIndex = chapAtual; if (_stop) { DownloadEventHub.OnDownloadStop(this, new DownloadEventArgs(downloadItem, (downloadItem.DownloadFinished - downloadItem.DownloadStarted), failedToDownload)); break; } try { SaveChapter(source, downloadItem, chapter, chapAtual, numChapters, hqDirectory); } catch (Exception e) { DownloadEventHub.OnDownloadError(this, new DownloadErrorEventArgs(downloadItem, e, DateTime.Now)); failedToDownload.Add(chapter.Link); } chapAtual++; } downloadItem.DownloadFinished = DateTime.Now; downloadItem.IsDownloaded = true; //downloadItem.Hq = hq.ToBytes(); _downloadContext.DownloadList.Update(downloadItem); DownloadEventHub.OnDownloadEnd(this, new DownloadEventArgs(downloadItem, (downloadItem.DownloadFinished - downloadItem.DownloadStarted), failedToDownload)); } }