New: (Orpheus) Add options to prevent downloads without FL tokens

pull/1884/head
Bogdan 1 year ago
parent 7dd289b5f9
commit 93c81bb7d3

@ -10,6 +10,7 @@ using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Annotations; using NzbDrone.Core.Annotations;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Exceptions;
using NzbDrone.Core.Indexers.Definitions.Gazelle; using NzbDrone.Core.Indexers.Definitions.Gazelle;
using NzbDrone.Core.Indexers.Exceptions; using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Indexers.Settings; using NzbDrone.Core.Indexers.Settings;
@ -80,40 +81,70 @@ namespace NzbDrone.Core.Indexers.Definitions
.SetHeader("Authorization", $"token {Settings.Apikey}") .SetHeader("Authorization", $"token {Settings.Apikey}")
.Build(); .Build();
var downloadBytes = Array.Empty<byte>(); byte[] fileData;
try try
{ {
var response = await _httpClient.ExecuteProxiedAsync(request, Definition); var response = await _httpClient.ExecuteProxiedAsync(request, Definition);
downloadBytes = response.ResponseData; fileData = response.ResponseData;
if (downloadBytes.Length >= 1 if (Settings.UseFreeleechToken == (int)OrpheusUseFreeleechTokens.Preferred
&& downloadBytes[0] != 'd' // simple test for torrent vs HTML content && fileData.Length >= 1
&& fileData[0] != 'd' // simple test for torrent vs HTML content
&& link.Query.Contains("usetoken=1")) && link.Query.Contains("usetoken=1"))
{ {
var html = Encoding.GetString(downloadBytes); var html = Encoding.GetString(fileData);
if (html.Contains("You do not have any freeleech tokens left.") if (html.Contains("You do not have any freeleech tokens left.")
|| html.Contains("You do not have enough freeleech tokens") || html.Contains("You do not have enough freeleech tokens")
|| html.Contains("This torrent is too large.") || html.Contains("This torrent is too large.")
|| html.Contains("You cannot use tokens here")) || html.Contains("You cannot use tokens here"))
{ {
// download again without usetoken // Try to download again without usetoken
request.Url = new HttpUri(link.ToString().Replace("&usetoken=1", "")); request.Url = new HttpUri(link.ToString().Replace("&usetoken=1", ""));
response = await _httpClient.ExecuteProxiedAsync(request, Definition); response = await _httpClient.ExecuteProxiedAsync(request, Definition);
downloadBytes = response.ResponseData; fileData = response.ResponseData;
} }
} }
_logger.Debug("Downloaded for release finished ({0} bytes from {1})", fileData.Length, link.AbsoluteUri);
}
catch (HttpException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
{
_logger.Error(ex, "Downloading file for release failed since it no longer exists ({0})", link.AbsoluteUri);
throw new ReleaseUnavailableException("Download failed", ex);
}
if (ex.Response.StatusCode == HttpStatusCode.TooManyRequests)
{
_logger.Error("API Grab Limit reached for {0}", link.AbsoluteUri);
}
else
{
_logger.Error(ex, "Downloading for release failed ({0})", link.AbsoluteUri);
}
throw new ReleaseDownloadException("Download failed", ex);
}
catch (WebException ex)
{
_logger.Error(ex, "Downloading for release failed ({0})", link.AbsoluteUri);
throw new ReleaseDownloadException("Download failed", ex);
} }
catch (Exception) catch (Exception)
{ {
_indexerStatusService.RecordFailure(Definition.Id); _indexerStatusService.RecordFailure(Definition.Id);
_logger.Error("Download failed"); _logger.Error("Download failed");
throw;
} }
ValidateDownloadData(downloadBytes); ValidateDownloadData(fileData);
return downloadBytes; return fileData;
} }
} }
@ -375,7 +406,7 @@ namespace NzbDrone.Core.Indexers.Definitions
.AddQueryParam("id", torrentId); .AddQueryParam("id", torrentId);
// Orpheus fails to download if usetoken=0 so we need to only add if we will use one // Orpheus fails to download if usetoken=0 so we need to only add if we will use one
if (_settings.UseFreeleechToken) if (_settings.UseFreeleechToken is (int)OrpheusUseFreeleechTokens.Preferred or (int)OrpheusUseFreeleechTokens.Required)
{ {
url = url.AddQueryParam("usetoken", "1"); url = url.AddQueryParam("usetoken", "1");
} }
@ -409,18 +440,30 @@ namespace NzbDrone.Core.Indexers.Definitions
public OrpheusSettings() public OrpheusSettings()
{ {
Apikey = ""; Apikey = "";
UseFreeleechToken = false; UseFreeleechToken = (int)OrpheusUseFreeleechTokens.Never;
} }
[FieldDefinition(2, Label = "API Key", HelpText = "API Key from the Site (Found in Settings => Access Settings)", Privacy = PrivacyLevel.ApiKey)] [FieldDefinition(2, Label = "API Key", HelpText = "API Key from the Site (Found in Settings => Access Settings)", Privacy = PrivacyLevel.ApiKey)]
public string Apikey { get; set; } public string Apikey { get; set; }
[FieldDefinition(3, Label = "Use Freeleech Tokens", HelpText = "Use freeleech tokens when available", Type = FieldType.Checkbox)] [FieldDefinition(3, Type = FieldType.Select, Label = "Use Freeleech Tokens", SelectOptions = typeof(OrpheusUseFreeleechTokens), HelpText = "When to use freeleech tokens")]
public bool UseFreeleechToken { get; set; } public int UseFreeleechToken { get; set; }
public override NzbDroneValidationResult Validate() public override NzbDroneValidationResult Validate()
{ {
return new NzbDroneValidationResult(Validator.Validate(this)); return new NzbDroneValidationResult(Validator.Validate(this));
} }
} }
public enum OrpheusUseFreeleechTokens
{
[FieldOption(Label = "Never", Hint = "Do not use tokens")]
Never = 0,
[FieldOption(Label = "Preferred", Hint = "Use token if possible")]
Preferred = 1,
[FieldOption(Label = "Required", Hint = "Abort download if unable to use token")]
Required = 2,
}
} }

Loading…
Cancel
Save