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

101 lines
5.6 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.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
public ChannelInfo ChannelInfo { get; private set; }
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>();
}
private 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
var f = string.Format(@"{0:ddd, dd MMM yyyy HH:mm:ss }{1}", dt, string.Format("{0:zzz}", dt).Replace(":", ""));
return f;
}
private XElement getTorznabElement(string name, object value) => value == null ? null : 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),
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),
new XElement("image",
new XElement("url", ChannelInfo.ImageUrl.AbsoluteUri),
2015-04-13 06:25:21 +00:00
new XElement("title", ChannelInfo.ImageTitle),
new XElement("link", ChannelInfo.ImageLink.AbsoluteUri),
2015-04-13 06:25:21 +00:00
new XElement("description", ChannelInfo.ImageDescription)
2015-04-16 06:04:28 +00:00
),
from r in Releases
select new XElement("item",
new XElement("title", 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.Comments == null ? null : new XElement("comments", r.Comments.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),
2015-04-16 06:04:28 +00:00
new XElement("description", 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("magneturl", r.MagnetUri?.AbsoluteUri),
2015-04-16 06:04:28 +00:00
getTorznabElement("rageid", r.RageID),
getTorznabElement("thetvdb", r.TVDBId),
getTorznabElement("imdb", r.Imdb == null ? null : ((long)r.Imdb).ToString("D7")),
2015-04-16 06:04:28 +00:00
getTorznabElement("seeders", r.Seeders),
getTorznabElement("peers", r.Peers),
getTorznabElement("infohash", r.InfoHash),
getTorznabElement("minimumratio", r.MinimumRatio),
getTorznabElement("minimumseedtime", r.MinimumSeedTime),
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
getTorznabElement("downloadvolumefactor", r.DownloadVolumeFactor),
getTorznabElement("uploadvolumefactor", r.UploadVolumeFactor)
2015-04-16 06:04:28 +00:00
)
2015-04-13 06:25:21 +00:00
)
)
);
2015-04-16 06:04:28 +00:00
return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
2015-04-13 06:25:21 +00:00
}
}
}