2013-04-07 07:30:37 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-04-07 22:40:13 +00:00
|
|
|
using System.Globalization;
|
2013-04-07 07:30:37 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.ServiceModel.Syndication;
|
2013-04-07 22:40:13 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2013-04-07 07:30:37 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
{
|
|
|
|
public interface IParseFeed
|
|
|
|
{
|
2013-04-15 01:41:39 +00:00
|
|
|
IEnumerable<ReportInfo> Process(Stream source);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class BasicRssParser : IParseFeed
|
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public BasicRssParser()
|
|
|
|
{
|
|
|
|
_logger = LogManager.GetCurrentClassLogger();
|
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
public IEnumerable<ReportInfo> Process(Stream source)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
var reader = new SyndicationFeedXmlReader(source);
|
|
|
|
var feed = SyndicationFeed.Load(reader).Items;
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
var result = new List<ReportInfo>();
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
foreach (var syndicationItem in feed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var parsedEpisode = ParseFeed(syndicationItem);
|
|
|
|
if (parsedEpisode != null)
|
|
|
|
{
|
|
|
|
parsedEpisode.NzbUrl = GetNzbUrl(syndicationItem);
|
2013-04-15 01:41:39 +00:00
|
|
|
parsedEpisode.NzbInfoUrl = GetNzbInfoUrl(syndicationItem);
|
2013-04-07 07:30:37 +00:00
|
|
|
result.Add(parsedEpisode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception itemEx)
|
|
|
|
{
|
|
|
|
itemEx.Data.Add("Item", syndicationItem.Title);
|
|
|
|
_logger.ErrorException("An error occurred while processing feed item", itemEx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual string GetTitle(SyndicationItem syndicationItem)
|
|
|
|
{
|
|
|
|
return syndicationItem.Title.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual string GetNzbUrl(SyndicationItem item)
|
|
|
|
{
|
|
|
|
return item.Links[0].Uri.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual string GetNzbInfoUrl(SyndicationItem item)
|
|
|
|
{
|
2013-04-07 22:40:13 +00:00
|
|
|
return String.Empty;
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
protected virtual ReportInfo PostProcessor(SyndicationItem item, ReportInfo currentResult)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
return currentResult;
|
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
private ReportInfo ParseFeed(SyndicationItem item)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
var title = GetTitle(item);
|
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
var reportInfo = new ReportInfo();
|
2013-04-15 01:41:39 +00:00
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
reportInfo.Title = title;
|
|
|
|
reportInfo.Age = DateTime.Now.Date.Subtract(item.PublishDate.Date).Days;
|
|
|
|
reportInfo.ReleaseGroup = ParseReleaseGroup(title);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
_logger.Trace("Parsed: {0} from: {1}", reportInfo, item.Title.Text);
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-04-23 06:14:55 +00:00
|
|
|
return PostProcessor(item, reportInfo);
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
2013-04-07 19:01:24 +00:00
|
|
|
|
2013-04-07 22:40:13 +00:00
|
|
|
public static string ParseReleaseGroup(string title)
|
2013-04-07 19:01:24 +00:00
|
|
|
{
|
|
|
|
title = title.Trim();
|
|
|
|
var index = title.LastIndexOf('-');
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
index = title.LastIndexOf(' ');
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
var group = title.Substring(index + 1);
|
|
|
|
|
2013-04-07 22:40:13 +00:00
|
|
|
if (@group.Length == title.Length)
|
2013-04-07 19:01:24 +00:00
|
|
|
return String.Empty;
|
|
|
|
|
2013-04-07 22:40:13 +00:00
|
|
|
return @group;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static readonly Regex[] HeaderRegex = new[]
|
|
|
|
{
|
|
|
|
new Regex(@"(?:\[.+\]\-\[.+\]\-\[.+\]\-\[)(?<nzbTitle>.+)(?:\]\-.+)",
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
|
|
|
|
|
|
|
new Regex(@"(?:\[.+\]\W+\[.+\]\W+\[.+\]\W+\"")(?<nzbTitle>.+)(?:\"".+)",
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
|
|
|
|
|
|
|
new Regex(@"(?:\[)(?<nzbTitle>.+)(?:\]\-.+)",
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled),
|
|
|
|
};
|
|
|
|
|
|
|
|
public static string ParseHeader(string header)
|
|
|
|
{
|
|
|
|
foreach (var regex in HeaderRegex)
|
|
|
|
{
|
|
|
|
var match = regex.Matches(header);
|
|
|
|
|
|
|
|
if (match.Count != 0)
|
|
|
|
return match[0].Groups["nzbTitle"].Value.Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static readonly Regex ReportSizeRegex = new Regex(@"(?<value>\d+\.\d{1,2}|\d+\,\d+\.\d{1,2})\W?(?<unit>GB|MB|GiB|MiB)",
|
|
|
|
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
|
|
|
|
|
|
|
|
|
|
|
public static long GetReportSize(string sizeString)
|
|
|
|
{
|
|
|
|
var match = ReportSizeRegex.Matches(sizeString);
|
|
|
|
|
|
|
|
if (match.Count != 0)
|
|
|
|
{
|
|
|
|
var cultureInfo = new CultureInfo("en-US");
|
|
|
|
var value = Decimal.Parse(Regex.Replace(match[0].Groups["value"].Value, "\\,", ""), cultureInfo);
|
|
|
|
|
|
|
|
var unit = match[0].Groups["unit"].Value;
|
|
|
|
|
|
|
|
if (unit.Equals("MB", StringComparison.InvariantCultureIgnoreCase) || unit.Equals("MiB", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
return Convert.ToInt64(value * 1048576L);
|
|
|
|
|
|
|
|
if (unit.Equals("GB", StringComparison.InvariantCultureIgnoreCase) || unit.Equals("GiB", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
return Convert.ToInt64(value * 1073741824L);
|
|
|
|
}
|
|
|
|
return 0;
|
2013-04-07 19:01:24 +00:00
|
|
|
}
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
}
|