fixes #1391 - SubtitleDownloader: 407 Limit

This commit is contained in:
Luke Pulverenti 2016-01-14 12:36:15 -05:00
parent 04508df3ef
commit c9ca6a6ad2

View File

@ -18,6 +18,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Net;
namespace MediaBrowser.Providers.Subtitles namespace MediaBrowser.Providers.Subtitles
{ {
@ -30,15 +31,6 @@ namespace MediaBrowser.Providers.Subtitles
private readonly IServerConfigurationManager _config; private readonly IServerConfigurationManager _config;
private readonly IEncryptionManager _encryption; private readonly IEncryptionManager _encryption;
private Timer _dailyTimer;
// This is limited to 200 per day
private int _dailyDownloadCount;
// It's 200 but this will be in-exact so buffer a little
// And the user may restart the server
private const int MaxDownloadsPerDay = 150;
private readonly IJsonSerializer _json; private readonly IJsonSerializer _json;
public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption, IJsonSerializer json) public OpenSubtitleDownloader(ILogManager logManager, IHttpClient httpClient, IServerConfigurationManager config, IEncryptionManager encryption, IJsonSerializer json)
@ -51,9 +43,6 @@ namespace MediaBrowser.Providers.Subtitles
_config.NamedConfigurationUpdating += _config_NamedConfigurationUpdating; _config.NamedConfigurationUpdating += _config_NamedConfigurationUpdating;
// Reset the count every 24 hours
_dailyTimer = new Timer(state => _dailyDownloadCount = 0, null, TimeSpan.FromHours(24), TimeSpan.FromHours(24));
Utilities.HttpClient = httpClient; Utilities.HttpClient = httpClient;
OpenSubtitles.SetUserAgent("mediabrowser.tv"); OpenSubtitles.SetUserAgent("mediabrowser.tv");
} }
@ -123,6 +112,7 @@ namespace MediaBrowser.Providers.Subtitles
return GetSubtitlesInternal(id, GetOptions(), cancellationToken); return GetSubtitlesInternal(id, GetOptions(), cancellationToken);
} }
private DateTime _lastRateLimitException;
private async Task<SubtitleResponse> GetSubtitlesInternal(string id, private async Task<SubtitleResponse> GetSubtitlesInternal(string id,
SubtitleOptions options, SubtitleOptions options,
CancellationToken cancellationToken) CancellationToken cancellationToken)
@ -132,12 +122,6 @@ namespace MediaBrowser.Providers.Subtitles
throw new ArgumentNullException("id"); throw new ArgumentNullException("id");
} }
if (_dailyDownloadCount >= MaxDownloadsPerDay &&
!options.IsOpenSubtitleVipAccount)
{
throw new InvalidOperationException("Open Subtitle's daily download limit has been exceeded. Please try again tomorrow.");
}
var idParts = id.Split(new[] { '-' }, 3); var idParts = id.Split(new[] { '-' }, 3);
var format = idParts[0]; var format = idParts[0];
@ -148,8 +132,19 @@ namespace MediaBrowser.Providers.Subtitles
await Login(cancellationToken).ConfigureAwait(false); await Login(cancellationToken).ConfigureAwait(false);
if ((DateTime.UtcNow - _lastRateLimitException).TotalHours < 1)
{
throw new ApplicationException("OpenSubtitles rate limit reached");
}
var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false); var resultDownLoad = await OpenSubtitles.DownloadSubtitlesAsync(downloadsList, cancellationToken).ConfigureAwait(false);
if ((resultDownLoad.Status ?? string.Empty).IndexOf("407", StringComparison.OrdinalIgnoreCase) != -1)
{
_lastRateLimitException = DateTime.UtcNow;
throw new ApplicationException("OpenSubtitles rate limit reached");
}
if (!(resultDownLoad is MethodResponseSubtitleDownload)) if (!(resultDownLoad is MethodResponseSubtitleDownload))
{ {
throw new ApplicationException("Invalid response type"); throw new ApplicationException("Invalid response type");
@ -157,13 +152,15 @@ namespace MediaBrowser.Providers.Subtitles
var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results; var results = ((MethodResponseSubtitleDownload)resultDownLoad).Results;
_lastRateLimitException = DateTime.MinValue;
if (results.Count == 0) if (results.Count == 0)
{ {
var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}", var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
ossId, ossId,
resultDownLoad.Name ?? string.Empty, resultDownLoad.Name ?? string.Empty,
resultDownLoad.Message ?? string.Empty, resultDownLoad.Status ?? string.Empty,
resultDownLoad.Status ?? string.Empty); resultDownLoad.Message ?? string.Empty);
throw new ResourceNotFoundException(msg); throw new ResourceNotFoundException(msg);
} }
@ -339,12 +336,6 @@ namespace MediaBrowser.Providers.Subtitles
public void Dispose() public void Dispose()
{ {
_config.NamedConfigurationUpdating -= _config_NamedConfigurationUpdating; _config.NamedConfigurationUpdating -= _config_NamedConfigurationUpdating;
if (_dailyTimer != null)
{
_dailyTimer.Dispose();
_dailyTimer = null;
}
} }
} }
} }