Пример #1
0
        /// <summary>
        /// Скачать страницу.
        /// </summary>
        /// <param name="chapterFolder">Папка для файлов.</param>
        public async Task Download(string chapterFolder)
        {
            this.IsDownloaded = false;

            if (restartCounter > MaxAttempt)
            {
                throw new DownloadAttemptFailed(restartCounter, this);
            }

            try
            {
                await DownloadManager.CheckPause().ConfigureAwait(false);

                using (await ThrottleService.WaitAsync().ConfigureAwait(false))
                {
                    await DownloadManager.CheckPause().ConfigureAwait(false);

                    if (!DirectoryHelpers.ValidateSettingPath(chapterFolder))
                    {
                        throw new DirectoryNotFoundException($"Попытка скачивания в папку {chapterFolder}, папка не существует.");
                    }

                    if (!Directory.Exists(chapterFolder))
                    {
                        Directory.CreateDirectory(chapterFolder);
                    }

                    var manga  = Chapter?.Volume?.Manga ?? Chapter?.Manga ?? Manga;
                    var plugin = ConfigStorage.Plugins.Single(p => p.MangaType == manga.GetType());
                    var cache  = MangaSettingCache.Get(plugin.GetType());

                    var file = await DownloadManager.DownloadImage(this.ImageLink, cache).ConfigureAwait(false);

                    if (!file.Exist)
                    {
                        OnDownloadFailed();
                    }
                    var fileName = this.Number.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0') + "." + file.Extension;
                    await file.Save(Path.Combine(chapterFolder, fileName)).ConfigureAwait(false);

                    this.IsDownloaded = true;
                }
            }
            catch (System.Exception ex)
            {
                Log.Exception(ex, this.Uri.OriginalString);
                ++restartCounter;
                await Download(chapterFolder).ConfigureAwait(false);
            }
        }
Пример #2
0
        private static async Task <(HttpResponseMessage, System.Exception)> DoWithRestarts(Uri uri, HttpClient client,
                                                                                           Func <Uri, HttpClient, Task <HttpResponseMessage> > func, int restartCounter = 0)
        {
            try
            {
                if (restartCounter > 3)
                {
                    throw new DownloadAttemptFailed(restartCounter, uri);
                }

                HttpResponseMessage content;
                using (await ThrottleService.WaitAsync().ConfigureAwait(false))
                {
                    content = await func(uri, client).ConfigureAwait(false);
                }

                if (await RequestCanBeRetry(content).ConfigureAwait(false))
                {
                    Log.Error($"{Strings.Page_GetPage_SiteOff}, код {content.StatusCode}, ссылка: {uri}, попытка номер - {restartCounter}");
                    ++restartCounter;
                    return(await DoWithRestarts(uri, client, func, restartCounter).ConfigureAwait(false));
                }

                return(content, null);
            }
            catch (TaskCanceledException ex)
            {
                Log.Exception(ex, $"{Strings.Page_GetPage_SiteOff}, ссылка: {uri}, попытка номер - {restartCounter}");
                ++restartCounter;
                return(await DoWithRestarts(uri, client, func, restartCounter).ConfigureAwait(false));
            }
            catch (HttpRequestException ex) when(ex.InnerException is System.Net.Sockets.SocketException || ex.InnerException is System.IO.IOException)
            {
                Log.Exception(ex, $"{Strings.Page_GetPage_SiteOff}, ссылка: {uri}, попытка номер - {restartCounter}");
                ++restartCounter;
                return(await DoWithRestarts(uri, client, func, restartCounter).ConfigureAwait(false));
            }
            catch (System.Exception ex)
            {
                Log.Exception(ex, $"Не удалось получить страницу: {uri}");
                return(null, ex);
            }
        }
Пример #3
0
        /// <summary>
        /// Скачать страницу.
        /// </summary>
        /// <param name="chapterFolder">Папка для файлов.</param>
        public async Task Download(string chapterFolder)
        {
            this.IsDownloaded = false;

            if (restartCounter > MaxAttempt)
            {
                throw new DownloadAttemptFailed(restartCounter, this);
            }

            try
            {
                await DownloadManager.CheckPause();

                using (await ThrottleService.WaitAsync())
                {
                    await DownloadManager.CheckPause();

                    chapterFolder = DirectoryHelpers.MakeValidPath(chapterFolder);
                    if (!Directory.Exists(chapterFolder))
                    {
                        Directory.CreateDirectory(chapterFolder);
                    }

                    var file = await DownloadManager.DownloadImage(this.ImageLink);

                    if (!file.Exist)
                    {
                        OnDownloadFailed();
                    }
                    var fileName = this.Number.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0') + "." + file.Extension;
                    await file.Save(Path.Combine(chapterFolder, fileName));

                    this.IsDownloaded = true;
                }
            }
            catch (System.Exception ex)
            {
                Log.Exception(ex, this.Uri.OriginalString);
                ++restartCounter;
                await Download(chapterFolder);
            }
        }