2013-04-07 09:30:37 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.ServiceModel.Syndication;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
{
|
|
|
|
public interface IParseFeed
|
|
|
|
{
|
2013-04-07 21:01:24 +02:00
|
|
|
IEnumerable<IndexerParseResult> Process(Stream source);
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class BasicRssParser : IParseFeed
|
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public BasicRssParser()
|
|
|
|
{
|
|
|
|
_logger = LogManager.GetCurrentClassLogger();
|
|
|
|
}
|
|
|
|
|
2013-04-07 21:01:24 +02:00
|
|
|
public IEnumerable<IndexerParseResult> Process(Stream source)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
var reader = new SyndicationFeedXmlReader(source);
|
|
|
|
var feed = SyndicationFeed.Load(reader).Items;
|
|
|
|
|
2013-04-07 21:01:24 +02:00
|
|
|
var result = new List<IndexerParseResult>();
|
2013-04-07 09:30:37 +02:00
|
|
|
|
|
|
|
foreach (var syndicationItem in feed)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var parsedEpisode = ParseFeed(syndicationItem);
|
|
|
|
if (parsedEpisode != null)
|
|
|
|
{
|
|
|
|
parsedEpisode.NzbUrl = GetNzbUrl(syndicationItem);
|
|
|
|
parsedEpisode.NzbInfoUrl = GetNzbUrl(syndicationItem);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
2013-04-07 21:01:24 +02:00
|
|
|
protected virtual IndexerParseResult PostProcessor(SyndicationItem item, IndexerParseResult currentResult)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
return currentResult;
|
|
|
|
}
|
|
|
|
|
2013-04-07 21:01:24 +02:00
|
|
|
private IndexerParseResult ParseFeed(SyndicationItem item)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
var title = GetTitle(item);
|
|
|
|
|
2013-04-07 21:01:24 +02:00
|
|
|
var episodeParseResult = Parser.ParseTitle<IndexerParseResult>(title);
|
2013-04-07 09:30:37 +02:00
|
|
|
if (episodeParseResult != null)
|
|
|
|
{
|
|
|
|
episodeParseResult.Age = DateTime.Now.Date.Subtract(item.PublishDate.Date).Days;
|
|
|
|
episodeParseResult.OriginalString = title;
|
|
|
|
episodeParseResult.SceneSource = true;
|
2013-04-07 21:01:24 +02:00
|
|
|
episodeParseResult.ReleaseGroup = ParseReleaseGroup(title);
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Trace("Parsed: {0} from: {1}", episodeParseResult, item.Title.Text);
|
|
|
|
|
|
|
|
return PostProcessor(item, episodeParseResult);
|
|
|
|
}
|
2013-04-07 21:01:24 +02:00
|
|
|
|
|
|
|
private static string ParseReleaseGroup(string title)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (group.Length == title.Length)
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
}
|