1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-29 02:57:15 +02:00
Sonarr/src/NzbDrone.Core/Indexers/TorrentRssParser.cs

79 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Indexers
{
public class TorrentRssParser : RssParser
{
// Parse various seeder/leecher/peers formats in the description element to determine number of seeders.
public Boolean ParseSeedersInDescription { get; set; }
public TorrentRssParser()
{
}
protected override ReleaseInfo CreateNewReleaseInfo()
{
return new TorrentInfo();
}
protected override ReleaseInfo ProcessItem(XElement item, ReleaseInfo releaseInfo)
{
var result = base.ProcessItem(item, releaseInfo) as TorrentInfo;
result.InfoHash = GetInfoHash(item);
result.MagnetUrl = GetMagnetUrl(item);
result.Seeders = GetSeeders(item);
result.Peers = GetPeers(item);
return result;
}
protected virtual String GetInfoHash(XElement item)
{
return null;
}
protected virtual String GetMagnetUrl(XElement item)
{
return null;
}
protected virtual Int32? GetSeeders(XElement item)
{
if (ParseSeedersInDescription)
{
var match = ParseSeedersRegex.Match(item.Element("description").Value);
if (match.Success)
{
return Int32.Parse(match.Groups["value"].Value);
}
}
return null;
}
protected virtual Int32? GetPeers(XElement item)
{
if (ParseSeedersInDescription)
{
var match = ParsePeersRegex.Match(item.Element("description").Value);
if (match.Success)
{
return Int32.Parse(match.Groups["value"].Value);
}
}
return null;
}
private static readonly Regex ParseSeedersRegex = new Regex(@"(Seeder)s?:\s+(?<value>\d+)|(?<value>\d+)\s+(seeder)s?", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly Regex ParsePeersRegex = new Regex(@"(Leecher|Peer)s?:\s+(?<value>\d+)|(?<value>\d+)\s+(leecher|peer)s?", RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}