Jackett/src/Jackett.Common/Models/ResultPage.cs

121 lines
6.4 KiB
C#
Raw Normal View History

2020-02-09 02:35:16 +00:00
using System;
2015-04-13 06:25:21 +00:00
using System.Collections.Generic;
using System.Globalization;
2015-04-13 06:25:21 +00:00
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
2015-04-13 06:25:21 +00:00
using System.Xml.Linq;
namespace Jackett.Common.Models
2015-04-13 06:25:21 +00:00
{
public class ResultPage
{
private static readonly XNamespace _AtomNs = "http://www.w3.org/2005/Atom";
private static readonly XNamespace _TorznabNs = "http://torznab.com/schemas/2015/feed";
2015-04-13 06:25:21 +00:00
// filters control characters but allows only properly-formed surrogate sequences
// https://stackoverflow.com/a/961504
private static readonly Regex _InvalidXmlChars = new Regex(
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
RegexOptions.Compiled);
private ChannelInfo ChannelInfo { get; }
Feature/new api (#1584) * Introducing API v2 There were multiple inconsistencies in the old API and I have been toying with the idea to replace it. This will suck for everyone who was building on top of the Jackett API, however as it was probably too painful to do so I'd say no one really tried. Now API v2.0 should be much more constistent as it uses DTObjects, and instead of manually constructing a json response it is handled by the ASP.NET web api. It is much more RESTful than it was, proper GET endpoints are introduced, and updating resources are now done via POST - it might be improved by introducing other type of REST methods. I know this sucks as completely breaks backward compatibility, however it'll probably make it easier to maintain and build on top of in the long run. * Use DELETE method to unconfigure an indexer * Remove debugging format from NLog * Fixing an null exception * Properly implementing IExceptionFilter interface * Enable adding public indexers without configuration * Fix missing manual search results * Basic modularization of the JS API * Introduce API versioning * Fix redirects to the dashboard * Cleaning up a little bit * Revamping Torznab and Potato as well * Move preconditions to FilterAttributes and simplify logic * Remove legacy filtering... will move to IResultFilter * Minor adjustment on results interface * Use Interpolated strings in ResultsController * DTO-ify * Remove fallback logic from potato results * DTO everywhere!!! * DTO-ify everything! * I hope this is my last piece of modification to this PR * Remove test variables... * Left out a couple conflicts... It's late
2017-08-08 15:02:16 +00:00
public IEnumerable<ReleaseInfo> Releases { get; set; }
2015-04-13 06:25:21 +00:00
public ResultPage(ChannelInfo channelInfo)
{
ChannelInfo = channelInfo;
Releases = new List<ReleaseInfo>();
}
/// <summary>
/// removes any unusual unicode characters that can't be encoded into XML (eg 0x1A)
/// </summary>
private static string RemoveInvalidXMLChars(string text)
{
if (text == null)
return null;
return _InvalidXmlChars.Replace(text, "");
}
private static string XmlDateFormat(DateTime dt)
2015-04-13 06:25:21 +00:00
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
2015-04-13 06:25:21 +00:00
//Sat, 14 Mar 2015 17:10:42 -0400
return $"{dt:ddd, dd MMM yyyy HH:mm:ss} " + $"{dt:zzz}".Replace(":", "");
2015-04-13 06:25:21 +00:00
}
private static XElement GetTorznabElement(string name, object value)
{
if (value == null)
return null;
return new XElement(_TorznabNs + "attr", new XAttribute("name", name), new XAttribute("value", value));
}
2015-04-13 06:25:21 +00:00
public string ToXml(Uri selfAtom)
{
// IMPORTANT: We can't use Uri.ToString(), because it generates URLs without URL encode (links with unicode
// characters are broken). We must use Uri.AbsoluteUri instead that handles encoding correctly
2015-04-16 06:04:28 +00:00
var xdoc = new XDocument(
2015-04-13 06:25:21 +00:00
new XDeclaration("1.0", "UTF-8", null),
new XElement("rss",
new XAttribute("version", "1.0"),
new XAttribute(XNamespace.Xmlns + "atom", _AtomNs.NamespaceName),
new XAttribute(XNamespace.Xmlns + "torznab", _TorznabNs.NamespaceName),
2015-04-13 06:25:21 +00:00
new XElement("channel",
new XElement(_AtomNs + "link",
new XAttribute("href", selfAtom.AbsoluteUri),
2015-04-13 06:25:21 +00:00
new XAttribute("rel", "self"),
new XAttribute("type", "application/rss+xml")
),
new XElement("title", ChannelInfo.Title),
new XElement("description", ChannelInfo.Description),
new XElement("link", ChannelInfo.Link.AbsoluteUri),
2018-04-06 15:46:49 +00:00
new XElement("language", ChannelInfo.Language),
2015-04-13 06:25:21 +00:00
new XElement("category", ChannelInfo.Category),
2015-04-16 06:04:28 +00:00
from r in Releases
select new XElement("item",
new XElement("title", RemoveInvalidXMLChars(r.Title)),
new XElement("guid", r.Guid.AbsoluteUri), // GUID and (Link or Magnet) are mandatory
new XElement("jackettindexer", new XAttribute("id", r.Origin.Id), r.Origin.DisplayName),
r.Details == null ? null : new XElement("comments", r.Details.AbsoluteUri),
r.PublishDate == DateTime.MinValue ? new XElement("pubDate", XmlDateFormat(DateTime.Now)) : new XElement("pubDate", XmlDateFormat(r.PublishDate)),
2015-05-05 04:25:28 +00:00
r.Size == null ? null : new XElement("size", r.Size),
r.Files == null ? null : new XElement("files", r.Files),
r.Grabs == null ? null : new XElement("grabs", r.Grabs),
new XElement("description", RemoveInvalidXMLChars(r.Description)),
new XElement("link", r.Link?.AbsoluteUri ?? r.MagnetUri.AbsoluteUri),
r.Category == null ? null : from c in r.Category select new XElement("category", c),
2015-04-16 06:04:28 +00:00
new XElement(
"enclosure",
new XAttribute("url", r.Link?.AbsoluteUri ?? r.MagnetUri.AbsoluteUri),
2015-05-05 04:25:28 +00:00
r.Size == null ? null : new XAttribute("length", r.Size),
2015-04-19 20:21:22 +00:00
new XAttribute("type", "application/x-bittorrent")
2015-04-16 06:04:28 +00:00
),
r.Category == null ? null : from c in r.Category select GetTorznabElement("category", c),
GetTorznabElement("imdb", r.Imdb?.ToString("D7")),
GetTorznabElement("imdbid", r.Imdb != null ? "tt" + r.Imdb?.ToString("D7") : null),
GetTorznabElement("rageid", r.RageID),
GetTorznabElement("tvdbid", r.TVDBId),
GetTorznabElement("tmdbid", r.TMDb),
GetTorznabElement("author", RemoveInvalidXMLChars(r.Author)),
GetTorznabElement("booktitle", RemoveInvalidXMLChars(r.BookTitle)),
GetTorznabElement("seeders", r.Seeders),
GetTorznabElement("peers", r.Peers),
GetTorznabElement("magneturl", r.MagnetUri?.AbsoluteUri),
GetTorznabElement("infohash", RemoveInvalidXMLChars(r.InfoHash)),
GetTorznabElement("minimumratio", r.MinimumRatio),
GetTorznabElement("minimumseedtime", r.MinimumSeedTime),
GetTorznabElement("downloadvolumefactor", r.DownloadVolumeFactor),
GetTorznabElement("uploadvolumefactor", r.UploadVolumeFactor),
GetTorznabElement("coverurl", r.Poster?.AbsoluteUri)
2015-04-16 06:04:28 +00:00
)
2015-04-13 06:25:21 +00:00
)
)
);
return xdoc.Declaration + Environment.NewLine + xdoc;
2015-04-13 06:25:21 +00:00
}
}
}