Esempio n. 1
0
        public async ValueTask Execute(DownloadMangaScheduleTask downloadMangaTask)
        {
            var downloader = PluginManager.MangaDownloaders.FirstOrDefault(it => it.IsLegalUrl(downloadMangaTask.MangaUrl, DownloadTaskType.Manga));

            if (downloader == null)
            {
                downloadMangaTask.Update(TaskStatus.Fail);
                await SendExceptionMessage(downloadMangaTask.Name, "不存在匹配该链接的下载器");

                return;
            }

            if (!Directory.Exists(downloadMangaTask.LibraryPath))
            {
                downloadMangaTask.Update(TaskStatus.Fail);
                await SendExceptionMessage(downloadMangaTask.Name, $"下载路径{downloadMangaTask.LibraryPath}不存在");

                return;
            }
            try
            {
                downloadMangaTask.Update(TaskStatus.Executing);
                var mangaDetail = await downloader.GetManga(downloadMangaTask.MangaUrl);

                downloadMangaTask.Name      = mangaDetail.Name;
                downloadMangaTask.MangaPath = Path.Combine(downloadMangaTask.LibraryPath, downloader.Name, mangaDetail.Id);
                Directory.CreateDirectory(downloadMangaTask.MangaPath);
                await File.WriteAllTextAsync(Path.Combine(downloadMangaTask.MangaPath, "detail.json"), JsonConvert.SerializeObject(mangaDetail));

                File.Create(Path.Combine(downloadMangaTask.MangaPath, ".ignore")).Close();
                if (mangaDetail.Cover != null)
                {
                    var imageUrl  = mangaDetail.Cover;
                    var imagePath = Path.Combine(downloadMangaTask.MangaPath, "cover");
                    var imageTask = new DownloadImageScheduleTask(imageUrl, imagePath, "cover", downloadMangaTask);
                    downloadMangaTask.Children.Add(imageTask);
                }

                foreach (var chapterDetail in mangaDetail.Chapters)
                {
                    var chapterTask = new DownloadChapterScheduleTask(
                        chapterDetail.Url,
                        Path.Combine(downloadMangaTask.MangaPath, chapterDetail.Id),
                        chapterDetail.Name,
                        downloadMangaTask);
                    downloadMangaTask.Children.Add(chapterTask);
                }

                downloadMangaTask.Counter = new AtomicCounter(0, downloadMangaTask.Children.Count);
                downloadMangaTask.Update(TaskStatus.Running);
            }
            catch (Exception e)
            {
                Logger.Warn(e);
                downloadMangaTask.Update(TaskStatus.Fail);
                await SendExceptionMessage(downloadMangaTask.Name, e.Message);
            }
        }
Esempio n. 2
0
        public async ValueTask Execute(DownloadChapterScheduleTask downloadChapterTask)
        {
            var downloader = PluginManager.MangaDownloaders.FirstOrDefault(it => it.IsLegalUrl(downloadChapterTask.ChapterUrl, DownloadTaskType.Chapter));

            if (downloader == null)
            {
                downloadChapterTask.Update(TaskStatus.Fail);
                return;
            }
            try
            {
                downloadChapterTask.Update(TaskStatus.Executing);
                if (Directory.Exists(downloadChapterTask.ChapterPath) &&
                    !File.Exists(Path.Combine(downloadChapterTask.ChapterPath, ".ignore")))
                {
                    downloadChapterTask.Update(TaskStatus.Success);
                    return;
                }
                Directory.CreateDirectory(downloadChapterTask.ChapterPath);
                File.Create(Path.Combine(downloadChapterTask.ChapterPath, ".ignore")).Close();
                var imageList = await downloader.GetChapter(downloadChapterTask.ChapterUrl);

                for (var j = 0; j < imageList.Count; j++)
                {
                    var imageUrl  = imageList[j];
                    var imagePath = Path.Combine(downloadChapterTask.ChapterPath, j.ToString());
                    var imageTask = new DownloadImageScheduleTask(imageUrl, imagePath, j.ToString(), downloadChapterTask);
                    downloadChapterTask.Children.Add(imageTask);
                }

                downloadChapterTask.Counter = new AtomicCounter(0, downloadChapterTask.Children.Count);
                downloadChapterTask.Update(TaskStatus.Running);
            }
            catch (Exception e)
            {
                Logger.Warn(e);
                downloadChapterTask.Update(TaskStatus.Fail);
            }
        }