fixes #2588 - OpenSubtitlesDownloader doesn't respect X-Ratelimit-Requests-Remaining

This commit is contained in:
Luke Pulverenti 2017-06-22 15:14:58 -04:00
parent e7706fa8a3
commit 6ff89eab78
4 changed files with 34 additions and 4 deletions

View File

@ -25,4 +25,25 @@ namespace MediaBrowser.Common.Extensions
} }
} }
public class RateLimitExceededException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="RateLimitExceededException" /> class.
/// </summary>
public RateLimitExceededException()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RateLimitExceededException" /> class.
/// </summary>
/// <param name="message">The message.</param>
public RateLimitExceededException(string message)
: base(message)
{
}
}
} }

View File

@ -136,7 +136,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1) if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1)
{ {
throw new Exception("OpenSubtitles rate limit reached"); throw new RateLimitExceededException("OpenSubtitles rate limit reached");
} }
var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false); var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);
@ -144,7 +144,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1) if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1)
{ {
_lastRateLimitException = DateTime.UtcNow; _lastRateLimitException = DateTime.UtcNow;
throw new Exception("OpenSubtitles rate limit reached"); throw new RateLimitExceededException("OpenSubtitles rate limit reached");
} }
if (!(resultDownLoad is MethodResponseSubtitleDownload)) if (!(resultDownLoad is MethodResponseSubtitleDownload))

View File

@ -10,6 +10,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
namespace MediaBrowser.Providers.MediaInfo namespace MediaBrowser.Providers.MediaInfo
{ {
@ -139,7 +140,7 @@ namespace MediaBrowser.Providers.MediaInfo
request.IndexNumberEnd = episode.IndexNumberEnd; request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName; request.SeriesName = episode.SeriesName;
} }
try try
{ {
var searchResults = await _subtitleManager.SearchSubtitles(request, cancellationToken).ConfigureAwait(false); var searchResults = await _subtitleManager.SearchSubtitles(request, cancellationToken).ConfigureAwait(false);
@ -154,6 +155,9 @@ namespace MediaBrowser.Providers.MediaInfo
return true; return true;
} }
} }
catch (RateLimitExceededException)
{
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.ErrorException("Error downloading subtitles", ex); _logger.ErrorException("Error downloading subtitles", ex);

View File

@ -133,7 +133,8 @@ namespace MediaBrowser.Providers.Subtitles
{ {
//var isText = MediaStream.IsTextFormat(response.Format); //var isText = MediaStream.IsTextFormat(response.Format);
using (var fs = _fileSystem.GetFileStream(savePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true)) using (var fs = _fileSystem.GetFileStream(savePath, FileOpenMode.Create, FileAccessMode.Write,
FileShareMode.Read, true))
{ {
await stream.CopyToAsync(fs).ConfigureAwait(false); await stream.CopyToAsync(fs).ConfigureAwait(false);
} }
@ -154,6 +155,10 @@ namespace MediaBrowser.Providers.Subtitles
} }
} }
} }
catch (RateLimitExceededException)
{
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
EventHelper.FireEventIfNotNull(SubtitleDownloadFailure, this, new SubtitleDownloadFailureEventArgs EventHelper.FireEventIfNotNull(SubtitleDownloadFailure, this, new SubtitleDownloadFailureEventArgs