mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
Lots of workings to search processing, UI Categories
This commit is contained in:
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using AngleSharp.Dom;
|
||||
@@ -9,7 +10,6 @@ using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
|
||||
namespace NzbDrone.Core.Indexers.Cardigann
|
||||
{
|
||||
@@ -22,7 +22,8 @@ namespace NzbDrone.Core.Indexers.Cardigann
|
||||
|
||||
protected string SiteLink { get; private set; }
|
||||
|
||||
/* protected readonly List<CategoryMapping> categoryMapping = new List<CategoryMapping>(); */
|
||||
protected readonly List<CategoryMapping> _categoryMapping = new List<CategoryMapping>();
|
||||
protected readonly List<string> _defaultCategories = new List<string>();
|
||||
|
||||
protected readonly string[] OptionalFields = new string[] { "imdb", "rageid", "tvdbid", "banner" };
|
||||
|
||||
@@ -54,6 +55,70 @@ namespace NzbDrone.Core.Indexers.Cardigann
|
||||
_logger = logger;
|
||||
|
||||
SiteLink = definition.Links.First();
|
||||
|
||||
if (_definition.Caps.Categories != null)
|
||||
{
|
||||
foreach (var category in _definition.Caps.Categories)
|
||||
{
|
||||
var cat = TorznabCatType.GetCatByName(category.Value);
|
||||
if (cat == null)
|
||||
{
|
||||
_logger.Error(string.Format("CardigannIndexer ({0}): invalid Torznab category for id {1}: {2}", _definition.Id, category.Key, category.Value));
|
||||
continue;
|
||||
}
|
||||
|
||||
AddCategoryMapping(category.Key, cat);
|
||||
}
|
||||
}
|
||||
|
||||
if (_definition.Caps.Categorymappings != null)
|
||||
{
|
||||
foreach (var categorymapping in _definition.Caps.Categorymappings)
|
||||
{
|
||||
IndexerCategory torznabCat = null;
|
||||
|
||||
if (categorymapping.cat != null)
|
||||
{
|
||||
torznabCat = TorznabCatType.GetCatByName(categorymapping.cat);
|
||||
if (torznabCat == null)
|
||||
{
|
||||
_logger.Error(string.Format("CardigannIndexer ({0}): invalid Torznab category for id {1}: {2}", _definition.Id, categorymapping.id, categorymapping.cat));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
AddCategoryMapping(categorymapping.id, torznabCat, categorymapping.desc);
|
||||
|
||||
if (categorymapping.Default)
|
||||
{
|
||||
_defaultCategories.Add(categorymapping.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddCategoryMapping(string trackerCategory, IndexerCategory torznabCategory, string trackerCategoryDesc = null)
|
||||
{
|
||||
_categoryMapping.Add(new CategoryMapping(trackerCategory, trackerCategoryDesc, torznabCategory.Id));
|
||||
|
||||
if (trackerCategoryDesc == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// create custom cats (1:1 categories) if trackerCategoryDesc is defined
|
||||
// - if trackerCategory is "integer" we use that number to generate custom category id
|
||||
// - if trackerCategory is "string" we compute a hash to generate fixed integer id for the custom category
|
||||
// the hash is not perfect but it should work in most cases. we can't use sequential numbers because
|
||||
// categories are updated frequently and the id must be fixed to work in 3rd party apps
|
||||
if (!int.TryParse(trackerCategory, out var trackerCategoryInt))
|
||||
{
|
||||
var hashed = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(trackerCategory));
|
||||
trackerCategoryInt = BitConverter.ToUInt16(hashed, 0); // id between 0 and 65535 < 100000
|
||||
}
|
||||
|
||||
var customCat = new IndexerCategory(trackerCategoryInt + 100000, trackerCategoryDesc);
|
||||
_categoryMapping.Add(new CategoryMapping(trackerCategory, trackerCategoryDesc, customCat.Id));
|
||||
}
|
||||
|
||||
protected IElement QuerySelector(IElement element, string selector)
|
||||
@@ -186,53 +251,36 @@ namespace NzbDrone.Core.Indexers.Cardigann
|
||||
return variables;
|
||||
}
|
||||
|
||||
protected ICollection<int> MapTrackerCatToNewznab(string input)
|
||||
protected ICollection<IndexerCategory> MapTrackerCatToNewznab(string input)
|
||||
{
|
||||
if (input == null)
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return new List<int>();
|
||||
}
|
||||
|
||||
var cats = _definition.Caps.Categorymappings.Where(m => m.id != null && m.id.ToLowerInvariant() == input.ToLowerInvariant()).Select(c => TorznabCatType.GetCatByName(c.cat).Id).ToList();
|
||||
|
||||
// 1:1 category mapping
|
||||
try
|
||||
{
|
||||
var trackerCategoryInt = int.Parse(input);
|
||||
cats.Add(trackerCategoryInt + 100000);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// input is not an integer, continue
|
||||
return new List<IndexerCategory>();
|
||||
}
|
||||
|
||||
var cats = _categoryMapping
|
||||
.Where(m =>
|
||||
!string.IsNullOrWhiteSpace(m.TrackerCategory) &&
|
||||
string.Equals(m.TrackerCategory, input, StringComparison.InvariantCultureIgnoreCase))
|
||||
.Select(c => TorznabCatType.AllCats.FirstOrDefault(n => n.Id == c.NewzNabCategory) ?? new IndexerCategory { Id = c.NewzNabCategory })
|
||||
.ToList();
|
||||
return cats;
|
||||
}
|
||||
|
||||
public List<string> MapTorznabCapsToTrackers(int[] searchCategories, bool mapChildrenCatsToParent = false)
|
||||
{
|
||||
var queryCats = new List<string>();
|
||||
|
||||
if (searchCategories == null)
|
||||
{
|
||||
return queryCats;
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
foreach (var searchCat in searchCategories)
|
||||
{
|
||||
var match = TorznabCatType.AllCats.FirstOrDefault(c => c.Id == searchCat);
|
||||
var results = new List<string>();
|
||||
|
||||
if (match != null)
|
||||
{
|
||||
queryCats.Add(match.Name);
|
||||
}
|
||||
}
|
||||
results.AddRange(_categoryMapping
|
||||
.Where(c => searchCategories.Contains(c.NewzNabCategory))
|
||||
.Select(mapping => mapping.TrackerCategory).Distinct().ToList());
|
||||
|
||||
var result = _definition.Caps.Categorymappings
|
||||
.Where(c => queryCats.Contains(c.cat))
|
||||
.Select(mapping => mapping.id).Distinct().ToList();
|
||||
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
protected delegate string TemplateTextModifier(string str);
|
||||
|
Reference in New Issue
Block a user