Esempio n. 1
0
        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));
            }
        }
Esempio n. 2
0
        private void SaveChapter(IHqSourceManager source, DownloadItem downloadItem, Chapter chapter, int chpaterIndex, int totalChapters, string directory)
        {
            lock (_lock5) {
                var startChapterDownload = DateTime.Now;
                var chapterDirectory     = _directoryHelper.CreateHqDirectory(directory, chapter.Title);
                if (chapter.Pages == null || chapter.Pages.Count == 0)
                {
                    var chapterInfo = new Chapter();
                    source.GetInfo(chapter.Link, out chapterInfo);
                    chapter.Pages = chapterInfo.Pages;
                }
                DownloadEventHub.OnDownloadChapterStart(this, new ProgressEventArgs(DateTime.Now, downloadItem, chpaterIndex, totalChapters));

                var pageAtual  = 1;
                var totalPages = chapter.Pages.Count();
                downloadItem.NumPages = totalPages;
                foreach (var page in chapter.Pages)
                {
                    downloadItem.ActualPageIndex = pageAtual;
                    if (_stop)
                    {
                        break;
                    }
                    if (_paused)
                    {
                        DownloadEventHub.OnDownloadPause(this, new ProgressEventArgs(DateTime.Now, downloadItem, pageAtual, totalPages));
                        while (_paused)
                        {
                            ;
                        }
                        DownloadEventHub.OnDownloadResume(this, new ProgressEventArgs(DateTime.Now, downloadItem, pageAtual, totalPages));
                    }

                    DownloadEventHub.OnDownloadProgress(this, new ProgressEventArgs(DateTime.Now, downloadItem, pageAtual, totalPages));

                    try {
                        var pageSource = $"{chapterDirectory}\\{page.Number.ToString().PadLeft(3, '0')}{FormatPage(page.Source)}";
                        if (!File.Exists(pageSource))
                        {
                            ServicePointManager.DefaultConnectionLimit = 1000;
                            using (var webClient = new HttpClient()) {
                                //webClient.Proxy = null;
                                //webClient.DownloadFile(page.Source, pageSource);
                                using (var response = webClient.GetAsync(page.Source).Result) {
                                    var imageByte = response.Content.ReadAsByteArrayAsync().Result;
                                    using (var binaryWriter = new BinaryWriter(new FileStream(pageSource,
                                                                                              FileMode.Append, FileAccess.Write))) {
                                        binaryWriter.Write(imageByte);
                                    }
                                }
                            }
                        }

                        page.Source = pageSource;
                    } catch (Exception e) {
                        DownloadEventHub.OnDownloadError(this, new DownloadErrorEventArgs(null, e, DateTime.Now));
                    }

                    pageAtual++;
                }

                DownloadEventHub.OnDownloadChapterEnd(this, new DownloadEventArgs(downloadItem));
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
Esempio n. 3
0
        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));
            }
        }