Fixed: Handle query limit error and add rate limit for BroadcastheNet

Co-authored-by: bakerboy448 <55419169+bakerboy448@users.noreply.github.com>
This commit is contained in:
Bogdan 2023-12-15 03:23:07 +02:00 committed by GitHub
parent aa9d25211f
commit 96cb7c4d5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 70 deletions

View File

@ -1,4 +1,5 @@
using NLog;
using System;
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Localization;
@ -14,6 +15,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
public override bool SupportsRss => true;
public override bool SupportsSearch => true;
public override int PageSize => 100;
public override TimeSpan RateLimit => TimeSpan.FromSeconds(5);
public BroadcastheNet(IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger, ILocalizationService localizationService)
: base(httpClient, indexerStatusService, configService, parsingService, logger, localizationService)
@ -22,15 +24,13 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
public override IIndexerRequestGenerator GetRequestGenerator()
{
var requestGenerator = new BroadcastheNetRequestGenerator() { Settings = Settings, PageSize = PageSize };
var requestGenerator = new BroadcastheNetRequestGenerator { Settings = Settings, PageSize = PageSize };
var releaseInfo = _indexerStatusService.GetLastRssSyncReleaseInfo(Definition.Id);
if (releaseInfo != null)
if (releaseInfo != null && int.TryParse(releaseInfo.Guid.Replace("BTN-", string.Empty), out var torrentId))
{
if (int.TryParse(releaseInfo.Guid.Replace("BTN-", string.Empty), out var torrentId))
{
requestGenerator.LastRecentTorrentID = torrentId;
}
requestGenerator.LastRecentTorrentId = torrentId;
}
return requestGenerator;

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Text.RegularExpressions;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Parser.Model;
@ -10,13 +11,14 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
{
public class BroadcastheNetParser : IParseIndexerResponse
{
private static readonly Regex RegexProtocol = new Regex("^https?:", RegexOptions.Compiled);
private static readonly Regex RegexProtocol = new ("^https?:", RegexOptions.Compiled);
public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
{
var results = new List<ReleaseInfo>();
var indexerHttpResponse = indexerResponse.HttpResponse;
switch (indexerResponse.HttpResponse.StatusCode)
switch (indexerHttpResponse.StatusCode)
{
case HttpStatusCode.Unauthorized:
throw new ApiKeyException("API Key invalid or not authorized");
@ -25,25 +27,30 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
case HttpStatusCode.ServiceUnavailable:
throw new RequestLimitReachedException("Cannot do more than 150 API requests per hour.");
default:
if (indexerResponse.HttpResponse.StatusCode != HttpStatusCode.OK)
if (indexerHttpResponse.StatusCode != HttpStatusCode.OK)
{
throw new IndexerException(indexerResponse, "Indexer API call returned an unexpected StatusCode [{0}]", indexerResponse.HttpResponse.StatusCode);
throw new IndexerException(indexerResponse, "Indexer API call returned an unexpected StatusCode [{0}]", indexerHttpResponse.StatusCode);
}
break;
}
if (indexerResponse.HttpResponse.Headers.ContentType != null && indexerResponse.HttpResponse.Headers.ContentType.Contains("text/html"))
if (indexerHttpResponse.Headers.ContentType != null && indexerHttpResponse.Headers.ContentType.Contains("text/html"))
{
throw new IndexerException(indexerResponse, "Indexer responded with html content. Site is likely blocked or unavailable.");
}
if (indexerResponse.Content.ContainsIgnoreCase("Call Limit Exceeded"))
{
throw new RequestLimitReachedException("Cannot do more than 150 API requests per hour.");
}
if (indexerResponse.Content == "Query execution was interrupted")
{
throw new IndexerException(indexerResponse, "Indexer API returned an internal server error");
}
var jsonResponse = new HttpResponse<JsonRpcResponse<BroadcastheNetTorrents>>(indexerResponse.HttpResponse).Resource;
var jsonResponse = new HttpResponse<JsonRpcResponse<BroadcastheNetTorrents>>(indexerHttpResponse).Resource;
if (jsonResponse.Error != null || jsonResponse.Result == null)
{
@ -59,38 +66,34 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
foreach (var torrent in jsonResponse.Result.Torrents.Values)
{
var torrentInfo = new TorrentInfo();
var torrentInfo = new TorrentInfo
{
Guid = $"BTN-{torrent.TorrentID}",
InfoUrl = $"{protocol}//broadcasthe.net/torrents.php?id={torrent.GroupID}&torrentid={torrent.TorrentID}",
DownloadUrl = RegexProtocol.Replace(torrent.DownloadURL, protocol),
Title = CleanReleaseName(torrent.ReleaseName),
InfoHash = torrent.InfoHash,
Size = torrent.Size,
Seeders = torrent.Seeders,
Peers = torrent.Leechers + torrent.Seeders,
PublishDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToUniversalTime().AddSeconds(torrent.Time),
Origin = torrent.Origin,
Source = torrent.Source,
Container = torrent.Container,
Codec = torrent.Codec,
Resolution = torrent.Resolution
};
torrentInfo.Guid = string.Format("BTN-{0}", torrent.TorrentID);
torrentInfo.Title = CleanReleaseName(torrent.ReleaseName);
torrentInfo.Size = torrent.Size;
torrentInfo.DownloadUrl = RegexProtocol.Replace(torrent.DownloadURL, protocol);
torrentInfo.InfoUrl = string.Format("{0}//broadcasthe.net/torrents.php?id={1}&torrentid={2}", protocol, torrent.GroupID, torrent.TorrentID);
// torrentInfo.CommentUrl =
if (torrent.TvdbID.HasValue)
if (torrent.TvdbID is > 0)
{
torrentInfo.TvdbId = torrent.TvdbID.Value;
}
if (torrent.TvrageID.HasValue)
if (torrent.TvrageID is > 0)
{
torrentInfo.TvRageId = torrent.TvrageID.Value;
}
torrentInfo.PublishDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToUniversalTime().AddSeconds(torrent.Time);
// torrentInfo.MagnetUrl =
torrentInfo.InfoHash = torrent.InfoHash;
torrentInfo.Seeders = torrent.Seeders;
torrentInfo.Peers = torrent.Leechers + torrent.Seeders;
torrentInfo.Origin = torrent.Origin;
torrentInfo.Source = torrent.Source;
torrentInfo.Container = torrent.Container;
torrentInfo.Codec = torrent.Codec;
torrentInfo.Resolution = torrent.Resolution;
results.Add(torrentInfo);
}
@ -99,9 +102,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
private string CleanReleaseName(string releaseName)
{
releaseName = releaseName.Replace("\\", "");
return releaseName;
return releaseName.Replace("\\", "");
}
}
}

View File

@ -11,7 +11,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
public int PageSize { get; set; }
public BroadcastheNetSettings Settings { get; set; }
public int? LastRecentTorrentID { get; set; }
public int? LastRecentTorrentId { get; set; }
public BroadcastheNetRequestGenerator()
{
@ -23,15 +23,15 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
{
var pageableRequests = new IndexerPageableRequestChain();
if (LastRecentTorrentID.HasValue)
if (LastRecentTorrentId is > 0)
{
pageableRequests.Add(GetPagedRequests(MaxPages, new BroadcastheNetTorrentQuery()
pageableRequests.Add(GetPagedRequests(MaxPages, new BroadcastheNetTorrentQuery
{
Id = ">=" + (LastRecentTorrentID.Value - 100)
Id = ">=" + (LastRecentTorrentId.Value - 100)
}));
}
pageableRequests.AddTier(GetPagedRequests(MaxPages, new BroadcastheNetTorrentQuery()
pageableRequests.AddTier(GetPagedRequests(MaxPages, new BroadcastheNetTorrentQuery
{
Age = "<=86400"
}));
@ -51,7 +51,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}%E{1:00}%", episode.SeasonNumber, episode.EpisodeNumber);
parameters.Name = $"S{episode.SeasonNumber:00}%E{episode.EpisodeNumber:00}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -70,14 +70,14 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
foreach (var seasonNumber in searchCriteria.Episodes.Select(v => v.SeasonNumber).Distinct())
{
parameters.Category = "Season";
parameters.Name = string.Format("Season {0}%", seasonNumber);
parameters.Name = $"Season {seasonNumber}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}E%", seasonNumber);
parameters.Name = $"S{seasonNumber:00}E%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -94,7 +94,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
if (AddSeriesSearchParameters(parameters, searchCriteria))
{
parameters.Category = "Episode";
parameters.Name = string.Format("{0:yyyy}.{0:MM}.{0:dd}", searchCriteria.AirDate);
parameters.Name = searchCriteria.AirDate.ToString("yyyy.MM.dd");
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
@ -105,7 +105,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}E{1:00}", episode.SeasonNumber, episode.EpisodeNumber);
parameters.Name = $"S{episode.SeasonNumber:00}E{episode.EpisodeNumber:00}";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -122,7 +122,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
if (AddSeriesSearchParameters(parameters, searchCriteria))
{
parameters.Category = "Episode";
parameters.Name = string.Format("{0}%", searchCriteria.Year);
parameters.Name = $"{searchCriteria.Year}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
@ -133,7 +133,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}E{1:00}", episode.SeasonNumber, episode.EpisodeNumber);
parameters.Name = $"S{episode.SeasonNumber:00}E{episode.EpisodeNumber:00}";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -154,7 +154,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}E{1:00}", episode.SeasonNumber, episode.EpisodeNumber);
parameters.Name = $"S{episode.SeasonNumber:00}E{episode.EpisodeNumber:00}";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -164,7 +164,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
parameters = parameters.Clone();
parameters.Category = "Season";
parameters.Name = string.Format("Season {0}%", seasonNumber);
parameters.Name = $"Season {seasonNumber}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -183,14 +183,14 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
foreach (var seasonNumber in searchCriteria.Episodes.Select(v => v.SeasonNumber).Distinct())
{
parameters.Category = "Season";
parameters.Name = string.Format("Season {0}%", seasonNumber);
parameters.Name = $"Season {seasonNumber}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = string.Format("S{0:00}E%", seasonNumber);
parameters.Name = $"S{seasonNumber:00}E%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -207,15 +207,15 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
if (AddSeriesSearchParameters(parameters, searchCriteria))
{
var episodeQueryTitle = searchCriteria.Episodes.Where(e => !string.IsNullOrWhiteSpace(e.Title))
.Select(e => SearchCriteriaBase.GetCleanSceneTitle(e.Title))
.ToArray();
.Select(e => SearchCriteriaBase.GetCleanSceneTitle(e.Title))
.ToArray();
foreach (var queryTitle in episodeQueryTitle)
{
parameters = parameters.Clone();
parameters.Category = "Episode";
parameters.Name = "%" + queryTitle + "%";
parameters.Name = $"%{queryTitle}%";
pageableRequests.Add(GetPagedRequests(MaxPages, parameters));
}
@ -228,13 +228,13 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
{
if (searchCriteria.Series.TvdbId != 0)
{
parameters.Tvdb = string.Format("{0}", searchCriteria.Series.TvdbId);
parameters.Tvdb = $"{searchCriteria.Series.TvdbId}";
return true;
}
if (searchCriteria.Series.TvRageId != 0)
{
parameters.Tvrage = string.Format("{0}", searchCriteria.Series.TvRageId);
parameters.Tvrage = $"{searchCriteria.Series.TvRageId}";
return true;
}

View File

@ -17,7 +17,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
public class BroadcastheNetSettings : ITorrentIndexerSettings
{
private static readonly BroadcastheNetSettingsValidator Validator = new BroadcastheNetSettingsValidator();
private static readonly BroadcastheNetSettingsValidator Validator = new ();
public BroadcastheNetSettings()
{
@ -35,7 +35,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
public int MinimumSeeders { get; set; }
[FieldDefinition(3)]
public SeedCriteriaSettings SeedCriteria { get; set; } = new SeedCriteriaSettings();
public SeedCriteriaSettings SeedCriteria { get; set; } = new ();
[FieldDefinition(4, Type = FieldType.Checkbox, Label = "Reject Blocklisted Torrent Hashes While Grabbing", HelpText = "If a torrent is blocked by hash it may not properly be rejected during RSS/Search for some indexers, enabling this will allow it to be rejected after the torrent is grabbed, but before it is sent to the client.", Advanced = true)]
public bool RejectBlocklistedTorrentHashesWhileGrabbing { get; set; }

View File

@ -1,4 +1,4 @@
namespace NzbDrone.Core.Indexers.BroadcastheNet
namespace NzbDrone.Core.Indexers.BroadcastheNet
{
public class BroadcastheNetTorrent
{

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace NzbDrone.Core.Indexers.BroadcastheNet
{
@ -13,15 +14,15 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Search { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Codec { get; set; }
public IEnumerable<string> Codec { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Container { get; set; }
public IEnumerable<string> Container { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Source { get; set; }
public IEnumerable<string> Source { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Resolution { get; set; }
public IEnumerable<string> Resolution { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Origin { get; set; }
public IEnumerable<string> Origin { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Hash { get; set; }
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]

View File

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
namespace NzbDrone.Core.Indexers.BroadcastheNet
{