<commit_before>﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using MangaReader.Core.Exception;
using MangaReader.Core.Services;

namespace MangaReader.Core.Manga
{
  [DebuggerDisplay("{Number} {Name}")]
  public class MangaPage : IDownloadable
  {
    #region Свойства

    /// <summary>
    /// Количество перезапусков загрузки.
    /// </summary>
    private int restartCounter;

    /// <summary>
    /// Название страницы.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Ссылка на страницу.
    /// </summary>
    public Uri Uri { get; set; }

    /// <summary>
    /// Ссылка на изображение.
    /// </summary>
    public Uri ImageLink { get; set; }

    /// <summary>
    /// Номер страницы.
    /// </summary>
    public int Number { get; set; }

    public bool IsDownloaded { get; set; }

    public double Downloaded
    {
      get { return this.IsDownloaded ? 100 : 0; }
      set { }
    }

    public string Folder { get; private set; }

    public event EventHandler<IManga> DownloadProgressChanged;

    protected void OnDownloadProgressChanged(IManga e)
    {
      var handler = DownloadProgressChanged;
      if (handler != null)
        handler(this, e);
    }

    #endregion

    #region Методы

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

      if (restartCounter > 3)
        throw new DownloadAttemptFailed(restartCounter, this);

      try
      {
        await DownloadManager.CheckPause();
        using (await ThrottleService.WaitAsync())
        {
          chapterFolder = DirectoryHelpers.MakeValidPath(chapterFolder);
          if (!Directory.Exists(chapterFolder))
            Directory.CreateDirectory(chapterFolder);

          var file = await DownloadManager.DownloadImage(this.ImageLink);
          if (!file.Exist)
            throw new System.Exception("Restart download, downloaded file is corrupted, link = " + this.ImageLink);
          var fileName = this.Number.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0') + "." + file.Extension;
          await file.Save(Path.Combine(chapterFolder, fileName));
          this.IsDownloaded = true;
          this.OnDownloadProgressChanged(null);
        }
      }
      catch (System.Exception ex)
      {
        Log.Exception(ex, this.Uri.OriginalString);<commit_msg>Add pause after throttler. #99<commit_after>﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using MangaReader.Core.Exception;
using MangaReader.Core.Services;

namespace MangaReader.Core.Manga
{
  [DebuggerDisplay("{Number} {Name}")]
  public class MangaPage : IDownloadable
  {
    #region Свойства

    /// <summary>
    /// Количество перезапусков загрузки.
    /// </summary>
    private int restartCounter;

    /// <summary>
    /// Название страницы.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Ссылка на страницу.
    /// </summary>
    public Uri Uri { get; set; }

    /// <summary>
    /// Ссылка на изображение.
    /// </summary>
    public Uri ImageLink { get; set; }

    /// <summary>
    /// Номер страницы.
    /// </summary>
    public int Number { get; set; }

    public bool IsDownloaded { get; set; }

    public double Downloaded
    {
      get { return this.IsDownloaded ? 100 : 0; }
      set { }
    }

    public string Folder { get; private set; }

    public event EventHandler<IManga> DownloadProgressChanged;

    protected void OnDownloadProgressChanged(IManga e)
    {
      var handler = DownloadProgressChanged;
      if (handler != null)
        handler(this, e);
    }

    #endregion

    #region Методы

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

      if (restartCounter > 3)
        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)
            throw new System.Exception("Restart download, downloaded file is corrupted, link = " + this.ImageLink);
          var fileName = this.Number.ToString(CultureInfo.InvariantCulture).PadLeft(4, '0') + "." + file.Extension;
          await file.Save(Path.Combine(chapterFolder, fileName));
          this.IsDownloaded = true;
          this.OnDownloadProgressChanged(null);
        }
      }
      catch (System.Exception ex)
      {
        Log.Exception(ex, this.Uri.OriginalString);
{'fraction_non_alphanumeric': 0.11748687025870454, 'fraction_numerical': 0.0031122349737405172, 'mean_word_length': 2.4030443414956983, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6065916', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1518, 'n_words': 353}