Skip to content

Commit

Permalink
fixes MediaBrowser#2588 - OpenSubtitlesDownloader doesn't respect X-R…
Browse files Browse the repository at this point in the history
…atelimit-Requests-Remaining
  • Loading branch information
LukePulverenti committed Jun 22, 2017
1 parent 6ada10f commit 9c756a9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
21 changes: 21 additions & 0 deletions MediaBrowser.Common/Extensions/ResourceNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,25 @@ public ResourceNotFoundException(string message)

}
}

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)
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ private async Task<SubtitleResponse> GetSubtitlesInternal(string id,

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);

if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1)
{
_lastRateLimitException = DateTime.UtcNow;
throw new Exception("OpenSubtitles rate limit reached");
throw new RateLimitExceededException("OpenSubtitles rate limit reached");
}

if (!(resultDownLoad is MethodResponseSubtitleDownload))
Expand Down
6 changes: 5 additions & 1 deletion MediaBrowser.Providers/MediaInfo/SubtitleDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;

namespace MediaBrowser.Providers.MediaInfo
{
Expand Down Expand Up @@ -139,7 +140,7 @@ private async Task<bool> DownloadSubtitles(Video video,
request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName;
}

try
{
var searchResults = await _subtitleManager.SearchSubtitles(request, cancellationToken).ConfigureAwait(false);
Expand All @@ -154,6 +155,9 @@ await _subtitleManager.DownloadSubtitles(video, result.Id, cancellationToken)
return true;
}
}
catch (RateLimitExceededException)
{
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles", ex);
Expand Down
7 changes: 6 additions & 1 deletion MediaBrowser.Providers/Subtitles/SubtitleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public async Task DownloadSubtitles(Video video,
{
//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);
}
Expand All @@ -154,6 +155,10 @@ public async Task DownloadSubtitles(Video video,
}
}
}
catch (RateLimitExceededException)
{
throw;
}
catch (Exception ex)
{
EventHelper.FireEventIfNotNull(SubtitleDownloadFailure, this, new SubtitleDownloadFailureEventArgs
Expand Down

0 comments on commit 9c756a9

Please sign in to comment.