mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-25 22:36:59 -04:00
New: Add Headphones VIP Indexer (#147)
* New: Add Headphones VIP Indexer * fixup! String Format Invalid * fixup! Remove hyphen from search string * Add Tests for Headphones Indexer
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Parser;
|
||||
using NzbDrone.Core.Indexers.Newznab;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Headphones
|
||||
{
|
||||
public class Headphones : HttpIndexerBase<HeadphonesSettings>
|
||||
{
|
||||
private readonly IHeadphonesCapabilitiesProvider _capabilitiesProvider;
|
||||
|
||||
public override string Name => "Headphones VIP";
|
||||
|
||||
public override DownloadProtocol Protocol => DownloadProtocol.Usenet;
|
||||
|
||||
public override int PageSize => _capabilitiesProvider.GetCapabilities(Settings).DefaultPageSize;
|
||||
|
||||
public override IIndexerRequestGenerator GetRequestGenerator()
|
||||
{
|
||||
return new HeadphonesRequestGenerator(_capabilitiesProvider)
|
||||
{
|
||||
PageSize = PageSize,
|
||||
Settings = Settings
|
||||
};
|
||||
}
|
||||
|
||||
public override IParseIndexerResponse GetParser()
|
||||
{
|
||||
return new NewznabRssParser();
|
||||
}
|
||||
|
||||
public Headphones(IHeadphonesCapabilitiesProvider capabilitiesProvider, IHttpClient httpClient, IIndexerStatusService indexerStatusService, IConfigService configService, IParsingService parsingService, Logger logger)
|
||||
: base(httpClient, indexerStatusService, configService, parsingService, logger)
|
||||
{
|
||||
_capabilitiesProvider = capabilitiesProvider;
|
||||
}
|
||||
|
||||
protected override void Test(List<ValidationFailure> failures)
|
||||
{
|
||||
base.Test(failures);
|
||||
|
||||
if (failures.Any()) return;
|
||||
failures.AddIfNotNull(TestCapabilities());
|
||||
}
|
||||
|
||||
protected virtual ValidationFailure TestCapabilities()
|
||||
{
|
||||
try
|
||||
{
|
||||
var capabilities = _capabilitiesProvider.GetCapabilities(Settings);
|
||||
|
||||
if (capabilities.SupportedSearchParameters != null && capabilities.SupportedSearchParameters.Contains("q"))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ValidationFailure(string.Empty, "Indexer does not support required search parameters");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Unable to connect to indexer: " + ex.Message);
|
||||
|
||||
return new ValidationFailure(string.Empty, "Unable to connect to indexer, check the log for more details");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.Indexers.Newznab;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Headphones
|
||||
{
|
||||
public class HeadphonesCapabilities
|
||||
{
|
||||
public int DefaultPageSize { get; set; }
|
||||
public int MaxPageSize { get; set; }
|
||||
public string[] SupportedSearchParameters { get; set; }
|
||||
public List<NewznabCategory> Categories { get; set; }
|
||||
|
||||
public HeadphonesCapabilities()
|
||||
{
|
||||
DefaultPageSize = 100;
|
||||
MaxPageSize = 100;
|
||||
SupportedSearchParameters = new[] { "q" };
|
||||
Categories = new List<NewznabCategory>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Indexers.Newznab;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Headphones
|
||||
{
|
||||
public interface IHeadphonesCapabilitiesProvider
|
||||
{
|
||||
HeadphonesCapabilities GetCapabilities(HeadphonesSettings settings);
|
||||
}
|
||||
|
||||
public class HeadphonesCapabilitiesProvider : IHeadphonesCapabilitiesProvider
|
||||
{
|
||||
private readonly ICached<HeadphonesCapabilities> _capabilitiesCache;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public HeadphonesCapabilitiesProvider(ICacheManager cacheManager, IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_capabilitiesCache = cacheManager.GetCache<HeadphonesCapabilities>(GetType());
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public HeadphonesCapabilities GetCapabilities(HeadphonesSettings indexerSettings)
|
||||
{
|
||||
var key = indexerSettings.ToJson();
|
||||
var capabilities = _capabilitiesCache.Get(key, () => FetchCapabilities(indexerSettings), TimeSpan.FromDays(7));
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
private HeadphonesCapabilities FetchCapabilities(HeadphonesSettings indexerSettings)
|
||||
{
|
||||
var capabilities = new HeadphonesCapabilities();
|
||||
|
||||
var url = string.Format("{0}{1}?t=caps", indexerSettings.BaseUrl.TrimEnd('/'), indexerSettings.ApiPath.TrimEnd('/'));
|
||||
|
||||
if (indexerSettings.ApiKey.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
url += "&apikey=" + indexerSettings.ApiKey;
|
||||
}
|
||||
|
||||
var request = new HttpRequest(url, HttpAccept.Rss);
|
||||
|
||||
request.AddBasicAuthentication(indexerSettings.Username, indexerSettings.Password);
|
||||
|
||||
HttpResponse response;
|
||||
|
||||
try
|
||||
{
|
||||
response = _httpClient.Get(request);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Debug(ex, "Failed to get headphones api capabilities from {0}", indexerSettings.BaseUrl);
|
||||
throw;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
capabilities = ParseCapabilities(response);
|
||||
}
|
||||
catch (XmlException ex)
|
||||
{
|
||||
_logger.Debug(ex, "Failed to parse headphones api capabilities for {0}", indexerSettings.BaseUrl);
|
||||
ex.WithData(response);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, "Failed to determine headphones api capabilities for {0}, using the defaults instead till Lidarr restarts", indexerSettings.BaseUrl);
|
||||
}
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
private HeadphonesCapabilities ParseCapabilities(HttpResponse response)
|
||||
{
|
||||
var capabilities = new HeadphonesCapabilities();
|
||||
|
||||
var xDoc = XDocument.Parse(response.Content);
|
||||
|
||||
if (xDoc == null)
|
||||
{
|
||||
throw new XmlException("Invalid XML");
|
||||
}
|
||||
|
||||
var xmlRoot = xDoc.Element("caps");
|
||||
|
||||
if (xmlRoot == null)
|
||||
{
|
||||
throw new XmlException("Unexpected XML");
|
||||
}
|
||||
|
||||
var xmlLimits = xmlRoot.Element("limits");
|
||||
if (xmlLimits != null)
|
||||
{
|
||||
capabilities.DefaultPageSize = int.Parse(xmlLimits.Attribute("default").Value);
|
||||
capabilities.MaxPageSize = int.Parse(xmlLimits.Attribute("max").Value);
|
||||
}
|
||||
|
||||
var xmlSearching = xmlRoot.Element("searching");
|
||||
if (xmlSearching != null)
|
||||
{
|
||||
var xmlBasicSearch = xmlSearching.Element("search");
|
||||
if (xmlBasicSearch == null || xmlBasicSearch.Attribute("available").Value != "yes")
|
||||
{
|
||||
capabilities.SupportedSearchParameters = null;
|
||||
}
|
||||
else if (xmlBasicSearch.Attribute("supportedParams") != null)
|
||||
{
|
||||
capabilities.SupportedSearchParameters = xmlBasicSearch.Attribute("supportedParams").Value.Split(',');
|
||||
}
|
||||
}
|
||||
|
||||
var xmlCategories = xmlRoot.Element("categories");
|
||||
if (xmlCategories != null)
|
||||
{
|
||||
foreach (var xmlCategory in xmlCategories.Elements("category"))
|
||||
{
|
||||
var cat = new NewznabCategory
|
||||
{
|
||||
Id = int.Parse(xmlCategory.Attribute("id").Value),
|
||||
Name = xmlCategory.Attribute("name").Value,
|
||||
Description = xmlCategory.Attribute("description") != null ? xmlCategory.Attribute("description").Value : string.Empty,
|
||||
Subcategories = new List<NewznabCategory>()
|
||||
};
|
||||
|
||||
foreach (var xmlSubcat in xmlCategory.Elements("subcat"))
|
||||
{
|
||||
cat.Subcategories.Add(new NewznabCategory
|
||||
{
|
||||
Id = int.Parse(xmlSubcat.Attribute("id").Value),
|
||||
Name = xmlSubcat.Attribute("name").Value,
|
||||
Description = xmlSubcat.Attribute("description") != null ? xmlSubcat.Attribute("description").Value : string.Empty
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
capabilities.Categories.Add(cat);
|
||||
}
|
||||
}
|
||||
|
||||
return capabilities;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Headphones
|
||||
{
|
||||
public class HeadphonesRequestGenerator : IIndexerRequestGenerator
|
||||
{
|
||||
private readonly IHeadphonesCapabilitiesProvider _capabilitiesProvider;
|
||||
public int MaxPages { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
public HeadphonesSettings Settings { get; set; }
|
||||
|
||||
public HeadphonesRequestGenerator(IHeadphonesCapabilitiesProvider capabilitiesProvider)
|
||||
{
|
||||
_capabilitiesProvider = capabilitiesProvider;
|
||||
|
||||
MaxPages = 30;
|
||||
PageSize = 100;
|
||||
}
|
||||
|
||||
public virtual IndexerPageableRequestChain GetRecentRequests()
|
||||
{
|
||||
var pageableRequests = new IndexerPageableRequestChain();
|
||||
|
||||
pageableRequests.Add(GetPagedRequests(MaxPages, Settings.Categories, "search", ""));
|
||||
|
||||
return pageableRequests;
|
||||
}
|
||||
|
||||
public virtual IndexerPageableRequestChain GetSearchRequests(AlbumSearchCriteria searchCriteria)
|
||||
{
|
||||
var pageableRequests = new IndexerPageableRequestChain();
|
||||
|
||||
pageableRequests.AddTier();
|
||||
|
||||
pageableRequests.Add(GetPagedRequests(MaxPages, Settings.Categories, "search",
|
||||
string.Format("&q={0}",
|
||||
NewsnabifyTitle(string.Format("{0} {1}",
|
||||
searchCriteria.Artist.Name,
|
||||
searchCriteria.AlbumTitle)))));
|
||||
|
||||
return pageableRequests;
|
||||
}
|
||||
|
||||
public virtual IndexerPageableRequestChain GetSearchRequests(ArtistSearchCriteria searchCriteria)
|
||||
{
|
||||
var pageableRequests = new IndexerPageableRequestChain();
|
||||
|
||||
pageableRequests.AddTier();
|
||||
|
||||
pageableRequests.Add(GetPagedRequests(MaxPages, Settings.Categories, "search",
|
||||
string.Format("&q={0}",
|
||||
NewsnabifyTitle(searchCriteria.Artist.Name))));
|
||||
|
||||
return pageableRequests;
|
||||
}
|
||||
|
||||
private IEnumerable<IndexerRequest> GetPagedRequests(int maxPages, IEnumerable<int> categories, string searchType, string parameters)
|
||||
{
|
||||
if (categories.Empty())
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
var categoriesQuery = string.Join(",", categories.Distinct());
|
||||
|
||||
var baseUrl = string.Format("{0}{1}?t={2}&cat={3}&extended=1", Settings.BaseUrl.TrimEnd('/'), Settings.ApiPath.TrimEnd('/'), searchType, categoriesQuery);
|
||||
|
||||
if (Settings.ApiKey.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
baseUrl += "&apikey=" + Settings.ApiKey;
|
||||
}
|
||||
|
||||
if (PageSize == 0)
|
||||
{
|
||||
var request = new IndexerRequest($"{baseUrl}{parameters}", HttpAccept.Rss);
|
||||
request.HttpRequest.AddBasicAuthentication(Settings.Username, Settings.Password);
|
||||
|
||||
yield return request;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var page = 0; page < maxPages; page++)
|
||||
{
|
||||
var request = new IndexerRequest(string.Format("{0}&offset={1}&limit={2}{3}", baseUrl, page * PageSize, PageSize, parameters), HttpAccept.Rss);
|
||||
request.HttpRequest.AddBasicAuthentication(Settings.Username, Settings.Password);
|
||||
|
||||
yield return request;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string NewsnabifyTitle(string title)
|
||||
{
|
||||
return title.Replace("+", "%20");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Headphones
|
||||
{
|
||||
public class HeadphonesSettingsValidator : AbstractValidator<HeadphonesSettings>
|
||||
{
|
||||
public HeadphonesSettingsValidator()
|
||||
{
|
||||
Custom(newznab =>
|
||||
{
|
||||
if (newznab.Categories.Empty())
|
||||
{
|
||||
return new ValidationFailure("", "'Categories' must be provided");
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
RuleFor(c => c.Username).NotEmpty();
|
||||
RuleFor(c => c.Password).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class HeadphonesSettings : IIndexerSettings
|
||||
{
|
||||
private static readonly HeadphonesSettingsValidator Validator = new HeadphonesSettingsValidator();
|
||||
|
||||
public HeadphonesSettings()
|
||||
{
|
||||
ApiPath = "/api";
|
||||
BaseUrl = "https://indexer.codeshy.com";
|
||||
ApiKey = "964d601959918a578a670984bdee9357";
|
||||
Categories = new[] { 3000, 3010, 3020, 3030, 3040 };
|
||||
}
|
||||
|
||||
public string BaseUrl { get; set; }
|
||||
|
||||
public string ApiPath { get; set; }
|
||||
|
||||
public string ApiKey { get; set; }
|
||||
|
||||
[FieldDefinition(0, Label = "Categories", HelpText = "Comma Separated list, leave blank to disable standard/daily shows", Advanced = true)]
|
||||
public IEnumerable<int> Categories { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Username")]
|
||||
public string Username { get; set; }
|
||||
|
||||
[FieldDefinition(2, Label = "Password", Type = FieldType.Password)]
|
||||
public string Password { get; set; }
|
||||
|
||||
public virtual NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,7 @@ namespace NzbDrone.Core.Indexers.Newznab
|
||||
|
||||
pageableRequests.Add(GetPagedRequests(MaxPages, Settings.Categories, "search",
|
||||
string.Format("&q={0}",
|
||||
NewsnabifyTitle(string.Format("{0} - {1}",
|
||||
NewsnabifyTitle(string.Format("{0} {1}",
|
||||
searchCriteria.Artist.Name,
|
||||
searchCriteria.AlbumTitle)))));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user