New: Added Torznab as generic indexer.

pull/3113/head
Taloth Saldono 9 years ago
parent 37e4a06b5d
commit c7470a426a

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://torznab.com/schemas/2015/feed"
xmlns:torznab="http://torznab.com/schemas/2015/feed">
<xs:simpleType name="attrNames">
<xs:restriction base="xs:string">
<!-- https://github.com/nZEDb/nZEDb/blob/master/docs/newznab_api_specification.txt -->
<!-- http://newznab.readthedocs.org/en/latest/misc/api/ -->
<!-- Original newznab attributes -->
<!-- All -->
<xs:enumeration value="size" />
<xs:enumeration value="category" />
<xs:enumeration value="guid" />
<xs:enumeration value="poster" />
<xs:enumeration value="team" />
<xs:enumeration value="grabs" />
<xs:enumeration value="comments" />
<xs:enumeration value="year" />
<!-- TV -->
<xs:enumeration value="season" />
<xs:enumeration value="episode" />
<xs:enumeration value="rageid" />
<xs:enumeration value="tvtitle" />
<xs:enumeration value="tvairdate" />
<!-- TV, Movies, Audio -->
<xs:enumeration value="video" />
<xs:enumeration value="audio" />
<xs:enumeration value="resolution" />
<xs:enumeration value="framerate" />
<xs:enumeration value="language" />
<xs:enumeration value="subs" />
<!-- Movies -->
<xs:enumeration value="imdb" />
<xs:enumeration value="imdbscore" />
<xs:enumeration value="imdbtitle" />
<xs:enumeration value="imdbtagline" />
<xs:enumeration value="imdbscore" />
<xs:enumeration value="imdbtitle" />
<xs:enumeration value="imdbtagline" />
<xs:enumeration value="imdbplot" />
<xs:enumeration value="imdbyear" />
<xs:enumeration value="imdbdirector" />
<xs:enumeration value="imdbactors" />
<!-- TV, Movies -->
<xs:enumeration value="genre" />
<!-- Music -->
<xs:enumeration value="artist" />
<xs:enumeration value="album" />
<xs:enumeration value="publisher" />
<xs:enumeration value="tracks" />
<!-- Mixed -->
<xs:enumeration value="coverurl" />
<xs:enumeration value="backdropcoverurl" />
<xs:enumeration value="review" />
<!-- Book -->
<xs:enumeration value="booktitle" />
<xs:enumeration value="publishdate" />
<xs:enumeration value="author" />
<xs:enumeration value="pages" />
<!-- Generic extensions -->
<xs:enumeration value="type" /> <!-- series|movie|music|book if unknown just omit -->
<xs:enumeration value="tvdbid" />
<xs:enumeration value="bannerurl" />
<!-- Nzb extensions -->
<xs:enumeration value="nzbhash" /> <!-- TBD, hash of sorted article headers of relevant content (relevant excludes stuff like par,nfo,nzb etc) -->
<!-- Torrent extensions -->
<xs:enumeration value="infohash" />
<xs:enumeration value="magneturl" />
<xs:enumeration value="seeders" />
<xs:enumeration value="leechers" />
<xs:enumeration value="peers" /> <!-- seeders + leechers -->
<xs:enumeration value="seedtype" /> <!-- TBD, which criteria must be met. was going for 'ratio,seedtime,both' but afaik it's always 'either' -->
<xs:enumeration value="minimumratio" />
<xs:enumeration value="minimumseedtime" />
</xs:restriction>
</xs:simpleType>
<xs:element name="attr">
<xs:complexType>
<xs:attribute name="name" type="torznab:attrNames" />
<xs:attribute name="value" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>

@ -0,0 +1,143 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="1.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:torznab="http://torznab.com/schemas/2015/feed">
<channel>
<atom:link href="https://hdaccess.net/api" rel="self" type="application/rss+xml" />
<title>HDAccess</title>
<description>HDAccess API</description>
<link>https://hdaccess.net</link>
<language>en-us</language>
<webMaster>($email) (HDA Invites)</webMaster>
<category>search</category>
<image>
<url>https://hdaccess.net/logo_small.png</url>
<title>HDAccess</title>
<link>https://hdaccess.net</link>
<description>HDAccess API</description>
</image>
<item>
<title>Better Call Saul S01E05 Alpine Shepherd 1080p NF WEBRip DD5.1 x264</title>
<guid isPermaLink="true">https://hdaccess.net/details.php?id=11515</guid>
<link>https://hdaccess.net/download.php?torrent=11515&amp;passkey=123456</link>
<comments>https://hdaccess.net/details.php?id=11515&amp;hit=1#comments</comments>
<pubDate>Sat, 14 Mar 2015 17:10:42 -0400</pubDate>
<category>HDTV 1080p</category>
<size>2538463390</size>
<description>Better.Call.Saul.S01E05.Alpine.Shepherd.1080p.NF.WEBRip.DD5.1.x264.torrent</description>
<enclosure url="https://hdaccess.net/download.php?torrent=11515&amp;passkey=123456" length="2538463390" type="application/x-bittorrent" />
<torznab:attr name="rageid" value="37780" />
<torznab:attr name="imdb" value="3032476" />
<torznab:attr name="tvdbid" value="273181" />
<torznab:attr name="category" value="5000" />
<torznab:attr name="category" value="5040" />
<torznab:attr name="category" value="100009" />
<torznab:attr name="category" value="100036" />
<torznab:attr name="type" value="series" />
<torznab:attr name="seeders" value="7" />
<torznab:attr name="peers" value="7" />
<torznab:attr name="coverurl" value="https://hdaccess.net/images/posters/273181.jpg" />
<torznab:attr name="bannerurl" value="https://hdaccess.net/images/banners/273181.jpg" />
<torznab:attr name="infohash" value="63e07ff523710ca268567dad344ce1e0e6b7e8a3" />
<torznab:attr name="minimumratio" value="1.0" />
<torznab:attr name="minimumseedtime" value="172800" />
</item>
<item>
<title>Ocean Giants 2013 1080p 3D BluRay Remux MVC DTS-HD MA 5.1-HDAccess</title>
<guid isPermaLink="true">https://hdaccess.net/details.php?id=11511</guid>
<link>https://hdaccess.net/download.php?torrent=11511&amp;passkey=123456</link>
<comments>https://hdaccess.net/details.php?id=11511&amp;hit=1#comments</comments>
<pubDate>Sat, 14 Mar 2015 16:33:42 -0400</pubDate>
<category>CUSTOM 3D BD</category>
<size>15330508800</size>
<description>Ocean Giants 2013 1080p 3D BluRay Remux MVC DTS-HD MA 5.1-HDAccess.torrent</description>
<enclosure url="https://hdaccess.net/download.php?torrent=11511&amp;passkey=123456" length="15330508800" type="application/x-bittorrent" />
<torznab:attr name="category" value="2000" />
<torznab:attr name="category" value="2060" />
<torznab:attr name="category" value="100001" />
<torznab:attr name="category" value="100018" />
<torznab:attr name="type" value="movie" />
<torznab:attr name="seeders" value="41" />
<torznab:attr name="peers" value="41" />
<torznab:attr name="coverurl" value="http://s5.postimg.org/gl7z658on/screenshot_1008.png" />
<torznab:attr name="infohash" value="ec039a525a6feac4b15889323f4f443de381e7cc" />
<torznab:attr name="minimumratio" value="1.0" />
<torznab:attr name="minimumseedtime" value="172800" />
</item>
<item>
<title>Wild 2014 720p BluRay DTS x264-HDAccess</title>
<guid isPermaLink="true">https://hdaccess.net/details.php?id=11506</guid>
<link>https://hdaccess.net/download.php?torrent=11506&amp;passkey=123456</link>
<comments>https://hdaccess.net/details.php?id=11506&amp;hit=1#comments</comments>
<pubDate>Sat, 14 Mar 2015 14:28:43 -0400</pubDate>
<category>720p</category>
<size>6501510357</size>
<description>Wild.2014.720p.BluRay.DTS.x264-HDAccess.torrent</description>
<enclosure url="https://hdaccess.net/download.php?torrent=11506&amp;passkey=123456" length="6501510357" type="application/x-bittorrent" />
<torznab:attr name="imdb" value="2305051" />
<torznab:attr name="category" value="2000" />
<torznab:attr name="category" value="2060" />
<torznab:attr name="category" value="100002" />
<torznab:attr name="category" value="100022" />
<torznab:attr name="type" value="movie" />
<torznab:attr name="seeders" value="57" />
<torznab:attr name="peers" value="58" />
<torznab:attr name="coverurl" value="https://hdaccess.net/imdb/images/2305051.jpg" />
<torznab:attr name="infohash" value="6704c29a00304f01b7dbb7959bfea5ccefe7d7d5" />
<torznab:attr name="minimumratio" value="1.0" />
<torznab:attr name="minimumseedtime" value="172800" />
</item>
<item>
<title>Absolute Power 1997.1080p BluRay Remux AVC DTS-HD MA 5.1-HDX</title>
<guid isPermaLink="true">https://hdaccess.net/details.php?id=11504</guid>
<link>https://hdaccess.net/download.php?torrent=11504&amp;passkey=123456</link>
<comments>https://hdaccess.net/details.php?id=11504&amp;hit=1#comments</comments>
<pubDate>Sat, 14 Mar 2015 13:34:08 -0400</pubDate>
<category>REMUX</category>
<size>25267070253</size>
<description>Absolute.Power.1997.1080p.BluRay.Remux.AVC.DTS-HD.MA.5.1-HDX.mkv.torrent</description>
<enclosure url="https://hdaccess.net/download.php?torrent=11504&amp;passkey=123456" length="25267070253" type="application/x-bittorrent" />
<torznab:attr name="imdb" value="0118548" />
<torznab:attr name="category" value="2000" />
<torznab:attr name="category" value="2060" />
<torznab:attr name="category" value="100002" />
<torznab:attr name="category" value="100026" />
<torznab:attr name="type" value="movie" />
<torznab:attr name="seeders" value="6" />
<torznab:attr name="peers" value="7" />
<torznab:attr name="coverurl" value="https://hdaccess.net/imdb/images/0118548.jpg" />
<torznab:attr name="infohash" value="668c1fed4b6bad43b1c84656da30d5f4eb58afdb" />
<torznab:attr name="minimumratio" value="1.0" />
<torznab:attr name="minimumseedtime" value="172800" />
</item>
<item>
<title>12 Monkeys S01E09 Tomorrow 720p WEB-DL DD5.1 H.264-BS</title>
<guid isPermaLink="true">https://hdaccess.net/details.php?id=11501</guid>
<link>https://hdaccess.net/download.php?torrent=11501&amp;passkey=123456</link>
<comments>https://hdaccess.net/details.php?id=11501&amp;hit=1#comments</comments>
<pubDate>Sat, 14 Mar 2015 12:42:19 -0400</pubDate>
<category>TV 720p WEB-DL</category>
<size>1397243303</size>
<description>12.Monkeys.S01E09.Tomorrow.720p.WEB-DL.DD5.1.H.264-BS.torrent</description>
<enclosure url="https://hdaccess.net/download.php?torrent=11501&amp;passkey=123456" length="1397243303" type="application/x-bittorrent" />
<torznab:attr name="rageid" value="36903" />
<torznab:attr name="imdb" value="3148266" />
<torznab:attr name="tvdbid" value="272644" />
<torznab:attr name="category" value="5000" />
<torznab:attr name="category" value="5040" />
<torznab:attr name="category" value="100004" />
<torznab:attr name="category" value="100038" />
<torznab:attr name="type" value="series" />
<torznab:attr name="seeders" value="6" />
<torznab:attr name="peers" value="6" />
<torznab:attr name="coverurl" value="https://hdaccess.net/images/posters/272644.jpg" />
<torznab:attr name="bannerurl" value="https://hdaccess.net/images/banners/272644.jpg" />
<torznab:attr name="infohash" value="9fbf7d6d52eb9847700591dad758988fb0799c53" />
<torznab:attr name="minimumratio" value="1.0" />
<torznab:attr name="minimumseedtime" value="172800" />
</item>
</channel>
</rss>

@ -0,0 +1,61 @@
using System;
using System.Linq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Indexers.Torznab;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
{
[TestFixture]
public class TorznabFixture : CoreTest<Torznab>
{
[SetUp]
public void Setup()
{
Subject.Definition = new IndexerDefinition()
{
Name = "Torznab",
Settings = new TorznabSettings()
{
Url = "http://indexer.local/",
Categories = new Int32[] { 1 }
}
};
}
[Test]
public void should_parse_recent_feed_from_torznab_hdaccess_net()
{
var recentFeed = ReadAllText(@"Files/RSS/torznab_hdaccess_net.xml");
Mocker.GetMock<IHttpClient>()
.Setup(o => o.Execute(It.Is<HttpRequest>(v => v.Method == HttpMethod.GET)))
.Returns<HttpRequest>(r => new HttpResponse(r, new HttpHeader(), recentFeed));
var releases = Subject.FetchRecent();
releases.Should().HaveCount(5);
releases.First().Should().BeOfType<TorrentInfo>();
var releaseInfo = releases.First() as TorrentInfo;
releaseInfo.Title.Should().Be("Better Call Saul S01E05 Alpine Shepherd 1080p NF WEBRip DD5.1 x264");
releaseInfo.DownloadProtocol.Should().Be(DownloadProtocol.Torrent);
releaseInfo.DownloadUrl.Should().Be("https://hdaccess.net/download.php?torrent=11515&passkey=123456");
releaseInfo.InfoUrl.Should().Be("https://hdaccess.net/details.php?id=11515&hit=1");
releaseInfo.CommentUrl.Should().Be("https://hdaccess.net/details.php?id=11515&hit=1#comments");
releaseInfo.Indexer.Should().Be(Subject.Definition.Name);
releaseInfo.PublishDate.Should().Be(DateTime.Parse("2015/03/14 21:10:42"));
releaseInfo.Size.Should().Be(2538463390);
releaseInfo.TvRageId.Should().Be(37780);
releaseInfo.InfoHash.Should().Be("63e07ff523710ca268567dad344ce1e0e6b7e8a3");
releaseInfo.Seeders.Should().Be(7);
releaseInfo.Peers.Should().Be(7);
}
}
}

@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Indexers.Torznab;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
{
public class TorznabRequestGeneratorFixture : CoreTest<TorznabRequestGenerator>
{
AnimeEpisodeSearchCriteria _animeSearchCriteria;
[SetUp]
public void SetUp()
{
Subject.Settings = new TorznabSettings()
{
Url = "http://127.0.0.1:1234/",
Categories = new [] { 1, 2 },
AnimeCategories = new [] { 3, 4 },
ApiKey = "abcd",
};
_animeSearchCriteria = new AnimeEpisodeSearchCriteria()
{
SceneTitles = new List<String>() { "Monkey+Island" },
AbsoluteEpisodeNumber = 100
};
}
[Test]
public void should_return_one_page_for_feed()
{
var results = Subject.GetRecentRequests();
results.Should().HaveCount(1);
var pages = results.First().Take(10).ToList();
pages.Should().HaveCount(1);
}
[Test]
public void should_use_all_categories_for_feed()
{
var results = Subject.GetRecentRequests();
results.Should().HaveCount(1);
var page = results.First().First();
page.Url.Query.Should().Contain("&cat=1,2,3,4&");
}
[Test]
public void should_not_have_duplicate_categories()
{
Subject.Settings.Categories = new[] { 1, 2, 3 };
var results = Subject.GetRecentRequests();
results.Should().HaveCount(1);
var page = results.First().First();
page.Url.Query.Should().Contain("&cat=1,2,3,4&");
}
[Test]
public void should_use_only_anime_categories_for_anime_search()
{
var results = Subject.GetSearchRequests(_animeSearchCriteria);
results.Should().HaveCount(1);
var page = results.First().First();
page.Url.Query.Should().Contain("&cat=3,4&");
}
[Test]
public void should_use_mode_search_for_anime()
{
var results = Subject.GetSearchRequests(_animeSearchCriteria);
results.Should().HaveCount(1);
var page = results.First().First();
page.Url.Query.Should().Contain("?t=search&");
}
[Test]
public void should_return_subsequent_pages()
{
var results = Subject.GetSearchRequests(_animeSearchCriteria);
results.Should().HaveCount(1);
var pages = results.First().Take(3).ToList();
pages[0].Url.Query.Should().Contain("&offset=0&");
pages[1].Url.Query.Should().Contain("&offset=100&");
pages[2].Url.Query.Should().Contain("&offset=200&");
}
[Test]
public void should_not_get_unlimited_pages()
{
var results = Subject.GetSearchRequests(_animeSearchCriteria);
results.Should().HaveCount(1);
var pages = results.First().Take(500).ToList();
pages.Count.Should().BeLessThan(500);
}
}
}

@ -0,0 +1,58 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Indexers.Torznab;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.IndexerTests.TorznabTests
{
public class TorznabSettingFixture : CoreTest
{
[TestCase("http://hdaccess.net")]
public void requires_apikey(string url)
{
var setting = new TorznabSettings()
{
ApiKey = "",
Url = url
};
setting.Validate().IsValid.Should().BeFalse();
setting.Validate().Errors.Should().Contain(c => c.PropertyName == "ApiKey");
}
[TestCase("")]
[TestCase(" ")]
[TestCase(null)]
public void invalid_url_should_not_apikey(string url)
{
var setting = new TorznabSettings
{
ApiKey = "",
Url = url
};
setting.Validate().IsValid.Should().BeFalse();
setting.Validate().Errors.Should().NotContain(c => c.PropertyName == "ApiKey");
setting.Validate().Errors.Should().Contain(c => c.PropertyName == "Url");
}
[TestCase("http://myfancytracker.net")]
public void doesnt_requires_apikey(string url)
{
var setting = new TorznabSettings()
{
ApiKey = "",
Url = url
};
setting.Validate().IsValid.Should().BeTrue();
}
}
}

@ -202,6 +202,9 @@
<Compile Include="IndexerTests\EztvTests\EztvFixture.cs" />
<Compile Include="IndexerTests\IndexerServiceFixture.cs" />
<Compile Include="IndexerTests\IntegrationTests\IndexerIntegrationTests.cs" />
<Compile Include="IndexerTests\TorznabTests\TorznabFixture.cs" />
<Compile Include="IndexerTests\TorznabTests\TorznabRequestGeneratorFixture.cs" />
<Compile Include="IndexerTests\TorznabTests\TorznabSettingFixture.cs" />
<Compile Include="IndexerTests\NewznabTests\NewznabFixture.cs" />
<Compile Include="IndexerTests\NewznabTests\NewznabRequestGeneratorFixture.cs" />
<Compile Include="IndexerTests\NewznabTests\NewznabSettingFixture.cs" />
@ -406,6 +409,9 @@
<Content Include="Files\QueueUnknownPriority.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\RSS\torznab_hdaccess_net.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Files\RSS\Nyaa.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

@ -0,0 +1,17 @@
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Indexers.Exceptions
{
public class UnsupportedFeedException : NzbDroneException
{
public UnsupportedFeedException(string message, params object[] args)
: base(message, args)
{
}
public UnsupportedFeedException(string message)
: base(message)
{
}
}
}

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
using NzbDrone.Core.ThingiProvider;
namespace NzbDrone.Core.Indexers.Torznab
{
public class Torznab : HttpIndexerBase<TorznabSettings>
{
public override DownloadProtocol Protocol { get { return DownloadProtocol.Torrent; } }
public override Int32 PageSize { get { return 100; } }
public override IIndexerRequestGenerator GetRequestGenerator()
{
return new TorznabRequestGenerator()
{
PageSize = PageSize,
Settings = Settings
};
}
public override IParseIndexerResponse GetParser()
{
return new TorznabRssParser();
}
public override IEnumerable<ProviderDefinition> DefaultDefinitions
{
get
{
yield return GetDefinition("HDAccess.net", GetSettings("http://hdaccess.net"));
}
}
public Torznab(IHttpClient httpClient, IConfigService configService, IParsingService parsingService, Logger logger)
: base(httpClient, configService, parsingService, logger)
{
}
private IndexerDefinition GetDefinition(String name, TorznabSettings settings)
{
return new IndexerDefinition
{
EnableRss = false,
EnableSearch = false,
Name = name,
Implementation = GetType().Name,
Settings = settings,
Protocol = DownloadProtocol.Usenet,
SupportsRss = SupportsRss,
SupportsSearch = SupportsSearch
};
}
private TorznabSettings GetSettings(String url, params int[] categories)
{
var settings = new TorznabSettings { Url = url };
if (categories.Any())
{
settings.Categories = categories;
}
return settings;
}
}
}

@ -0,0 +1,16 @@
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Indexers.Torznab
{
public class TorznabException : NzbDroneException
{
public TorznabException(string message, params object[] args) : base(message, args)
{
}
public TorznabException(string message)
: base(message)
{
}
}
}

@ -0,0 +1,175 @@
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.Torznab
{
public class TorznabRequestGenerator : IIndexerRequestGenerator
{
public Int32 MaxPages { get; set; }
public Int32 PageSize { get; set; }
public TorznabSettings Settings { get; set; }
public TorznabRequestGenerator()
{
MaxPages = 30;
PageSize = 100;
}
public virtual IList<IEnumerable<IndexerRequest>> GetRecentRequests()
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
// TODO: We might consider getting multiple pages in the future, but atm we limit it to 1 page.
pageableRequests.AddIfNotNull(GetPagedRequests(1, Settings.Categories.Concat(Settings.AnimeCategories), "tvsearch", ""));
return pageableRequests;
}
public virtual IList<IEnumerable<IndexerRequest>> GetSearchRequests(SingleEpisodeSearchCriteria searchCriteria)
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
if (searchCriteria.Series.TvRageId > 0)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&rid={0}&season={1}&ep={2}",
searchCriteria.Series.TvRageId,
searchCriteria.SeasonNumber,
searchCriteria.EpisodeNumber)));
}
else
{
foreach (var queryTitle in searchCriteria.QueryTitles)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&q={0}&season={1}&ep={2}",
NewsnabifyTitle(queryTitle),
searchCriteria.SeasonNumber,
searchCriteria.EpisodeNumber)));
}
}
return pageableRequests;
}
public virtual IList<IEnumerable<IndexerRequest>> GetSearchRequests(SeasonSearchCriteria searchCriteria)
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
if (searchCriteria.Series.TvRageId > 0)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&rid={0}&season={1}",
searchCriteria.Series.TvRageId,
searchCriteria.SeasonNumber)));
}
else
{
foreach (var queryTitle in searchCriteria.QueryTitles)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&q={0}&season={1}",
NewsnabifyTitle(queryTitle),
searchCriteria.SeasonNumber)));
}
}
return pageableRequests;
}
public virtual IList<IEnumerable<IndexerRequest>> GetSearchRequests(DailyEpisodeSearchCriteria searchCriteria)
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
if (searchCriteria.Series.TvRageId > 0)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}",
searchCriteria.Series.TvRageId,
searchCriteria.AirDate)));
}
else
{
foreach (var queryTitle in searchCriteria.QueryTitles)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories, "tvsearch",
String.Format("&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}",
NewsnabifyTitle(queryTitle),
searchCriteria.AirDate)));
}
}
return pageableRequests;
}
public virtual IList<IEnumerable<IndexerRequest>> GetSearchRequests(AnimeEpisodeSearchCriteria searchCriteria)
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
foreach (var queryTitle in searchCriteria.QueryTitles)
{
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.AnimeCategories, "search",
String.Format("&q={0}+{1:00}",
NewsnabifyTitle(queryTitle),
searchCriteria.AbsoluteEpisodeNumber)));
}
return pageableRequests;
}
public virtual IList<IEnumerable<IndexerRequest>> GetSearchRequests(SpecialEpisodeSearchCriteria searchCriteria)
{
var pageableRequests = new List<IEnumerable<IndexerRequest>>();
foreach (var queryTitle in searchCriteria.EpisodeQueryTitles)
{
var query = queryTitle.Replace('+', ' ');
query = System.Web.HttpUtility.UrlEncode(query);
pageableRequests.AddIfNotNull(GetPagedRequests(MaxPages, Settings.Categories.Concat(Settings.AnimeCategories), "search",
String.Format("&q={0}",
query)));
}
return pageableRequests;
}
private IEnumerable<IndexerRequest> GetPagedRequests(Int32 maxPages, IEnumerable<Int32> categories, String searchType, String parameters)
{
if (categories.Empty())
{
yield break;
}
var categoriesQuery = String.Join(",", categories.Distinct());
var baseUrl = String.Format("{0}/api?t={1}&cat={2}&extended=1{3}", Settings.Url.TrimEnd('/'), searchType, categoriesQuery, Settings.AdditionalParameters);
if (Settings.ApiKey.IsNotNullOrWhiteSpace())
{
baseUrl += "&apikey=" + Settings.ApiKey;
}
if (PageSize == 0)
{
yield return new IndexerRequest(String.Format("{0}{1}", baseUrl, parameters), HttpAccept.Rss);
}
else
{
for (var page = 0; page < maxPages; page++)
{
yield return new IndexerRequest(String.Format("{0}&offset={1}&limit={2}{3}", baseUrl, page * PageSize, PageSize, parameters), HttpAccept.Rss);
}
}
}
private static String NewsnabifyTitle(String title)
{
return title.Replace("+", "%20");
}
}
}

@ -0,0 +1,168 @@
using System;
using System.Linq;
using System.Xml.Linq;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Indexers.Exceptions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Indexers.Torznab
{
public class TorznabRssParser : TorrentRssParser
{
public const String ns = "{http://torznab.com/schemas/2015/feed}";
protected override bool PreProcess(IndexerResponse indexerResponse)
{
var xdoc = XDocument.Parse(indexerResponse.Content);
var error = xdoc.Descendants("error").FirstOrDefault();
if (error == null) return true;
var code = Convert.ToInt32(error.Attribute("code").Value);
var errorMessage = error.Attribute("description").Value;
if (code >= 100 && code <= 199) throw new ApiKeyException("Invalid API key");
if (!indexerResponse.Request.Url.ToString().Contains("apikey=") && errorMessage == "Missing parameter")
{
throw new ApiKeyException("Indexer requires an API key");
}
if (errorMessage == "Request limit reached")
{
throw new RequestLimitReachedException("API limit reached");
}
throw new TorznabException("Torznab error detected: {0}", errorMessage);
}
protected override ReleaseInfo ProcessItem(XElement item, ReleaseInfo releaseInfo)
{
var torrentInfo = base.ProcessItem(item, releaseInfo) as TorrentInfo;
torrentInfo.TvRageId = GetTvRageId(item);
return torrentInfo;
}
protected override ReleaseInfo PostProcess(XElement item, ReleaseInfo releaseInfo)
{
var enclosureType = item.Element("enclosure").Attribute("type").Value;
if (enclosureType != "application/x-bittorrent")
{
throw new UnsupportedFeedException("Feed contains {0} instead of application/x-bittorrent", enclosureType);
}
return base.PostProcess(item, releaseInfo);
}
protected override String GetInfoUrl(XElement item)
{
return item.TryGetValue("comments").TrimEnd("#comments");
}
protected override String GetCommentUrl(XElement item)
{
return item.TryGetValue("comments");
}
protected override Int64 GetSize(XElement item)
{
Int64 size;
var sizeString = TryGetTorznabAttribute(item, "size");
if (!sizeString.IsNullOrWhiteSpace() && Int64.TryParse(sizeString, out size))
{
return size;
}
size = GetEnclosureLength(item);
return size;
}
protected override DateTime GetPublishDate(XElement item)
{
return base.GetPublishDate(item);
}
protected override string GetDownloadUrl(XElement item)
{
var url = base.GetDownloadUrl(item);
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
url = item.Element("enclosure").Attribute("url").Value;
}
return url;
}
protected virtual Int32 GetTvRageId(XElement item)
{
var tvRageIdString = TryGetTorznabAttribute(item, "rageid");
Int32 tvRageId;
if (!tvRageIdString.IsNullOrWhiteSpace() && Int32.TryParse(tvRageIdString, out tvRageId))
{
return tvRageId;
}
return 0;
}
protected override String GetInfoHash(XElement item)
{
return TryGetTorznabAttribute(item, "infohash");
}
protected override String GetMagnetUrl(XElement item)
{
return TryGetTorznabAttribute(item, "magneturl");
}
protected override Int32? GetSeeders(XElement item)
{
var seeders = TryGetTorznabAttribute(item, "seeders");
if (seeders.IsNotNullOrWhiteSpace())
{
return Int32.Parse(seeders);
}
return base.GetSeeders(item);
}
protected override Int32? GetPeers(XElement item)
{
var peers = TryGetTorznabAttribute(item, "peers");
if (peers.IsNotNullOrWhiteSpace())
{
return Int32.Parse(peers);
}
var seeders = TryGetTorznabAttribute(item, "seeders");
var leechers = TryGetTorznabAttribute(item, "leechers");
if (seeders.IsNotNullOrWhiteSpace() && leechers.IsNotNullOrWhiteSpace())
{
return Int32.Parse(seeders) + Int32.Parse(leechers);
}
return base.GetPeers(item);
}
protected String TryGetTorznabAttribute(XElement item, String key, String defaultValue = "")
{
var attr = item.Elements(ns + "attr").SingleOrDefault(e => e.Attribute("name").Value.Equals(key, StringComparison.CurrentCultureIgnoreCase));
if (attr != null)
{
return attr.Attribute("value").Value;
}
return defaultValue;
}
}
}

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Torznab
{
public class TorznabSettingsValidator : AbstractValidator<TorznabSettings>
{
private static readonly string[] ApiKeyWhiteList =
{
"hdaccess.net",
};
private static bool ShouldHaveApiKey(TorznabSettings settings)
{
if (settings.Url == null)
{
return false;
}
return ApiKeyWhiteList.Any(c => settings.Url.ToLowerInvariant().Contains(c));
}
private static readonly Regex AdditionalParametersRegex = new Regex(@"(&.+?\=.+?)+", RegexOptions.Compiled);
public TorznabSettingsValidator()
{
RuleFor(c => c.Url).ValidRootUrl();
RuleFor(c => c.ApiKey).NotEmpty().When(ShouldHaveApiKey);
RuleFor(c => c.Categories).NotEmpty().When(c => !c.AnimeCategories.Any());
RuleFor(c => c.AnimeCategories).NotEmpty().When(c => !c.Categories.Any());
RuleFor(c => c.AdditionalParameters)
.Matches(AdditionalParametersRegex)
.When(c => !c.AdditionalParameters.IsNullOrWhiteSpace());
}
}
public class TorznabSettings : IProviderConfig
{
private static readonly TorznabSettingsValidator Validator = new TorznabSettingsValidator();
public TorznabSettings()
{
Categories = new[] { 5030, 5040 };
AnimeCategories = Enumerable.Empty<Int32>();
}
[FieldDefinition(0, Label = "URL")]
public String Url { get; set; }
[FieldDefinition(1, Label = "API Key")]
public String ApiKey { get; set; }
[FieldDefinition(2, Label = "Categories", HelpText = "Comma Separated list, leave blank to disable standard/daily shows", Advanced = true)]
public IEnumerable<Int32> Categories { get; set; }
[FieldDefinition(3, Label = "Anime Categories", HelpText = "Comma Separated list, leave blank to disable anime", Advanced = true)]
public IEnumerable<Int32> AnimeCategories { get; set; }
[FieldDefinition(4, Label = "Additional Parameters", HelpText = "Additional Torznab parameters", Advanced = true)]
public String AdditionalParameters { get; set; }
public ValidationResult Validate()
{
return Validator.Validate(this);
}
}
}

@ -451,6 +451,7 @@
<Compile Include="Indexers\Exceptions\ApiKeyException.cs" />
<Compile Include="Indexers\Exceptions\IndexerException.cs" />
<Compile Include="Indexers\Exceptions\RequestLimitReachedException.cs" />
<Compile Include="Indexers\Exceptions\UnsupportedFeedException.cs" />
<Compile Include="Indexers\EzrssTorrentRssParser.cs" />
<Compile Include="Indexers\Eztv\Eztv.cs" />
<Compile Include="Indexers\Eztv\EztvSettings.cs" />
@ -499,6 +500,11 @@
<Compile Include="Indexers\Torrentleech\Torrentleech.cs" />
<Compile Include="Indexers\Torrentleech\TorrentleechSettings.cs" />
<Compile Include="Indexers\TorrentRssParser.cs" />
<Compile Include="Indexers\Torznab\Torznab.cs" />
<Compile Include="Indexers\Torznab\TorznabException.cs" />
<Compile Include="Indexers\Torznab\TorznabRequestGenerator.cs" />
<Compile Include="Indexers\Torznab\TorznabRssParser.cs" />
<Compile Include="Indexers\Torznab\TorznabSettings.cs" />
<Compile Include="Indexers\Wombles\Wombles.cs" />
<Compile Include="Indexers\Wombles\WomblesRssParser.cs" />
<Compile Include="Indexers\XElementExtensions.cs" />

Loading…
Cancel
Save