mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-04-27 23:06:29 -04:00
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml.Linq;
|
||||
using MonoTorrent;
|
||||
@@ -9,12 +10,27 @@ namespace NzbDrone.Core.Indexers
|
||||
{
|
||||
public class TorrentRssParser : RssParser
|
||||
{
|
||||
// Use to sum/calculate Peers as Leechers+Seeders
|
||||
public bool CalculatePeersAsSum { get; set; } = false;
|
||||
|
||||
// Use the specified element name to determine the Infohash
|
||||
public string InfoHashElementName { get; set; }
|
||||
|
||||
// Parse various seeder/leecher/peers formats in the description element to determine number of seeders.
|
||||
public bool ParseSeedersInDescription { get; set; }
|
||||
|
||||
// Use the specified element name to determine the size
|
||||
// Use the specified element name to determine the Peers
|
||||
public string PeersElementName { get; set; }
|
||||
|
||||
// Use the specified element name to determine the Seeds
|
||||
public string SeedsElementName { get; set; }
|
||||
|
||||
// Use the specified element name to determine the Size
|
||||
public string SizeElementName { get; set; }
|
||||
|
||||
// Use the specified element name to determine the Magnet link
|
||||
public string MagnetElementName { get; set; }
|
||||
|
||||
public TorrentRssParser()
|
||||
{
|
||||
PreferredEnclosureMimeTypes = TorrentEnclosureMimeTypes;
|
||||
@@ -40,14 +56,27 @@ namespace NzbDrone.Core.Indexers
|
||||
result.InfoHash = GetInfoHash(item);
|
||||
result.MagnetUrl = GetMagnetUrl(item);
|
||||
result.Seeders = GetSeeders(item);
|
||||
result.Peers = GetPeers(item);
|
||||
|
||||
if (CalculatePeersAsSum)
|
||||
{
|
||||
result.Peers = GetPeers(item) + result.Seeders;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Peers = GetPeers(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual string GetInfoHash(XElement item)
|
||||
{
|
||||
if (InfoHashElementName.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
return item.FindDecendants(InfoHashElementName).FirstOrDefault().Value;
|
||||
}
|
||||
|
||||
var magnetUrl = GetMagnetUrl(item);
|
||||
|
||||
if (magnetUrl.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
try
|
||||
@@ -64,10 +93,21 @@ namespace NzbDrone.Core.Indexers
|
||||
|
||||
protected virtual string GetMagnetUrl(XElement item)
|
||||
{
|
||||
var downloadUrl = GetDownloadUrl(item);
|
||||
if (downloadUrl.IsNotNullOrWhiteSpace() && downloadUrl.StartsWith("magnet:"))
|
||||
if (MagnetElementName.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
return downloadUrl;
|
||||
var magnetURL = item.FindDecendants(MagnetElementName).FirstOrDefault().Value;
|
||||
if (magnetURL.IsNotNullOrWhiteSpace() && magnetURL.StartsWith("magnet:"))
|
||||
{
|
||||
return magnetURL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var downloadUrl = GetDownloadUrl(item);
|
||||
if (downloadUrl.IsNotNullOrWhiteSpace() && downloadUrl.StartsWith("magnet:"))
|
||||
{
|
||||
return downloadUrl;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -75,6 +115,9 @@ namespace NzbDrone.Core.Indexers
|
||||
|
||||
protected virtual int? GetSeeders(XElement item)
|
||||
{
|
||||
// safe to always use the element if it's present (and valid)
|
||||
// fall back to description if ParseSeedersInDescription is enabled
|
||||
|
||||
if (ParseSeedersInDescription && item.Element("description") != null)
|
||||
{
|
||||
var matchSeeders = ParseSeedersRegex.Match(item.Element("description").Value);
|
||||
@@ -92,6 +135,11 @@ namespace NzbDrone.Core.Indexers
|
||||
return int.Parse(matchPeers.Groups["value"].Value) - int.Parse(matchLeechers.Groups["value"].Value);
|
||||
}
|
||||
}
|
||||
var seeds = item.FindDecendants(SeedsElementName).SingleOrDefault();
|
||||
if (seeds != null)
|
||||
{
|
||||
return (int)seeds;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -116,6 +164,12 @@ namespace NzbDrone.Core.Indexers
|
||||
}
|
||||
}
|
||||
|
||||
if (PeersElementName.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
var itempeers = item.FindDecendants(PeersElementName).SingleOrDefault();
|
||||
return int.Parse(itempeers.Value);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -124,12 +178,12 @@ namespace NzbDrone.Core.Indexers
|
||||
var size = base.GetSize(item);
|
||||
if (size == 0 && SizeElementName.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
if (item.Element(SizeElementName) != null)
|
||||
var itemsize = item.FindDecendants(SizeElementName).SingleOrDefault();
|
||||
if (itemsize != null)
|
||||
{
|
||||
size = ParseSize(item.Element(SizeElementName).Value, true);
|
||||
size = ParseSize(itemsize.Value, true);
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user