Jackett/src/Jackett.Common/Models/TorznabCapabilities.cs

176 lines
6.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace Jackett.Common.Models
{
public class TorznabCapabilities
{
2017-08-17 07:48:54 +00:00
public int? LimitsMax { get; set; } = null;
public int? LimitsDefault { get; set; } = null;
public bool SearchAvailable { get; set; }
public bool TVSearchAvailable { get; set; }
2017-01-16 16:23:19 +00:00
public bool MovieSearchAvailable { get; set; }
public bool SupportsTVRageSearch { get; set; }
2019-05-11 03:27:25 +00:00
public bool SupportsImdbMovieSearch { get; set; }
2017-01-16 16:23:19 +00:00
public bool SupportsImdbTVSearch { get; set; }
2017-10-18 16:30:41 +00:00
public bool MusicSearchAvailable
{
get
{
return (SupportedMusicSearchParamsList.Count > 0);
}
}
public List<string> SupportedMusicSearchParamsList;
public List<TorznabCategory> Categories { get; private set; }
public TorznabCapabilities()
{
Categories = new List<TorznabCategory>();
SearchAvailable = true;
TVSearchAvailable = true;
2017-01-16 16:23:19 +00:00
MovieSearchAvailable = false;
SupportsTVRageSearch = false;
2019-05-11 03:27:25 +00:00
SupportsImdbMovieSearch = false;
SupportsImdbTVSearch = false;
2017-10-18 16:30:41 +00:00
SupportedMusicSearchParamsList = new List<string>();
}
public TorznabCapabilities(params TorznabCategory[] cats)
{
SearchAvailable = true;
TVSearchAvailable = true;
SupportsTVRageSearch = false;
2019-05-11 03:27:25 +00:00
SupportsImdbMovieSearch = false;
SupportsImdbTVSearch = false;
2017-10-18 16:30:41 +00:00
SupportedMusicSearchParamsList = new List<string>();
Categories = new List<TorznabCategory>();
Categories.AddRange(cats);
2017-01-16 16:23:19 +00:00
MovieSearchAvailable = Categories.Any(i => TorznabCatType.Movies.Contains(i));
}
string SupportedTVSearchParams
{
get
{
var parameters = new List<string>() { "q", "season", "ep" };
if (SupportsTVRageSearch)
parameters.Add("rid");
if (SupportsImdbTVSearch)
parameters.Add("imdbid");
return string.Join(",", parameters);
}
}
2017-01-16 16:23:19 +00:00
string SupportedMovieSearchParams
{
get
{
var parameters = new List<string>() { "q" };
2019-05-11 03:27:25 +00:00
if (SupportsImdbMovieSearch)
2017-01-16 16:23:19 +00:00
parameters.Add("imdbid");
return string.Join(",", parameters);
}
}
2017-10-18 16:30:41 +00:00
string SupportedMusicSearchParams
{
get
{
return string.Join(",", SupportedMusicSearchParamsList);
}
}
public bool SupportsCategories(int[] categories)
{
var subCategories = Categories.SelectMany(c => c.SubCategories);
var allCategories = Categories.Concat(subCategories);
var supportsCategory = allCategories.Any(i => categories.Any(c => c == i.ID));
return supportsCategory;
}
2018-04-06 15:43:18 +00:00
public XDocument GetXDocument()
{
var xdoc = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("caps",
2017-08-17 07:22:04 +00:00
new XElement("server",
new XAttribute("title", "Jackett")
),
2017-08-17 07:48:54 +00:00
LimitsMax != null || LimitsDefault != null ?
new XElement("limits",
LimitsMax != null ? new XAttribute("max", LimitsMax) : null,
LimitsDefault != null ? new XAttribute("default", LimitsDefault) : null
)
: null,
new XElement("searching",
new XElement("search",
new XAttribute("available", SearchAvailable ? "yes" : "no"),
new XAttribute("supportedParams", "q")
),
new XElement("tv-search",
new XAttribute("available", TVSearchAvailable ? "yes" : "no"),
new XAttribute("supportedParams", SupportedTVSearchParams)
2017-01-16 16:23:19 +00:00
),
new XElement("movie-search",
new XAttribute("available", MovieSearchAvailable ? "yes" : "no"),
new XAttribute("supportedParams", SupportedMovieSearchParams)
2017-10-18 16:30:41 +00:00
),
new XElement("music-search",
new XAttribute("available", MusicSearchAvailable ? "yes" : "no"),
2017-10-18 16:42:00 +00:00
new XAttribute("supportedParams", SupportedMusicSearchParams)
2017-10-23 16:02:53 +00:00
),
// inconsistend but apparently already used by various newznab indexers (see #1896)
new XElement("audio-search",
new XAttribute("available", MusicSearchAvailable ? "yes" : "no"),
new XAttribute("supportedParams", SupportedMusicSearchParams)
)
),
new XElement("categories",
from c in Categories.OrderBy(x => x.ID < 100000 ? "z" + x.ID.ToString() : x.Name)
select new XElement("category",
new XAttribute("id", c.ID),
new XAttribute("name", c.Name),
from sc in c.SubCategories
select new XElement("subcat",
new XAttribute("id", sc.ID),
new XAttribute("name", sc.Name)
)
)
)
)
);
2018-04-06 15:43:18 +00:00
return xdoc;
}
public string ToXml()
{
var xdoc = GetXDocument();
return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
}
public static TorznabCapabilities Concat(TorznabCapabilities lhs, TorznabCapabilities rhs)
{
lhs.SearchAvailable = lhs.SearchAvailable || rhs.SearchAvailable;
lhs.TVSearchAvailable = lhs.TVSearchAvailable || rhs.TVSearchAvailable;
lhs.MovieSearchAvailable = lhs.MovieSearchAvailable || rhs.MovieSearchAvailable;
lhs.SupportsTVRageSearch = lhs.SupportsTVRageSearch || rhs.SupportsTVRageSearch;
2019-05-11 03:27:25 +00:00
lhs.SupportsImdbMovieSearch = lhs.SupportsImdbMovieSearch || rhs.SupportsImdbMovieSearch;
lhs.SupportsImdbTVSearch = lhs.SupportsImdbTVSearch || rhs.SupportsImdbTVSearch;
lhs.Categories.AddRange(rhs.Categories.Where(x => x.ID < 100000).Except(lhs.Categories)); // exclude indexer specific categories (>= 100000)
return lhs;
}
}
}