2011-04-25 20:16:38 +02:00
|
|
|
|
//http://stackoverflow.com/questions/210375/problems-reading-rss-with-c-and-net-3-5
|
|
|
|
|
//https://connect.microsoft.com/VisualStudio/feedback/details/325421/syndicationfeed-load-fails-to-parse-datetime-against-a-real-world-feeds-ie7-can-read
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.ServiceModel.Syndication;
|
|
|
|
|
using System.Xml;
|
2012-02-22 04:59:45 +01:00
|
|
|
|
using NLog;
|
2011-04-25 20:16:38 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers.Indexer
|
|
|
|
|
{
|
|
|
|
|
public class SyndicationFeedXmlReader : XmlTextReader
|
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
private static readonly string[] rss20DateTimeHints = { "pubDate" };
|
|
|
|
|
private static readonly string[] atom10DateTimeHints = { "updated", "published", "lastBuildDate" };
|
|
|
|
|
private bool _isRss2DateTime;
|
|
|
|
|
private bool _isAtomDateTime;
|
|
|
|
|
|
|
|
|
|
private static readonly MethodInfo rss20FeedFormatterMethodInfo = typeof(Rss20FeedFormatter).GetMethod("DateFromString", BindingFlags.NonPublic | BindingFlags.Static);
|
|
|
|
|
private static readonly MethodInfo atom10FeedFormatterMethodInfo = typeof(Atom10FeedFormatter).GetMethod("DateFromString", BindingFlags.NonPublic | BindingFlags.Static);
|
2011-04-25 20:16:38 +02:00
|
|
|
|
|
|
|
|
|
public SyndicationFeedXmlReader(Stream stream) : base(stream) { }
|
|
|
|
|
|
|
|
|
|
public override bool IsStartElement(string localname, string ns)
|
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
_isRss2DateTime = rss20DateTimeHints.Contains(localname);
|
|
|
|
|
_isAtomDateTime = atom10DateTimeHints.Contains(localname);
|
2011-04-25 20:16:38 +02:00
|
|
|
|
|
|
|
|
|
return base.IsStartElement(localname, ns);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ReadString()
|
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
var dateVal = base.ReadString();
|
2011-04-25 20:16:38 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
if (_isRss2DateTime)
|
2011-04-25 20:16:38 +02:00
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
rss20FeedFormatterMethodInfo.Invoke(null, new object[] { dateVal, this });
|
2011-04-25 20:16:38 +02:00
|
|
|
|
}
|
2012-02-22 04:59:45 +01:00
|
|
|
|
if (_isAtomDateTime)
|
2011-04-25 20:16:38 +02:00
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
atom10FeedFormatterMethodInfo.Invoke(new Atom10FeedFormatter(), new object[] { dateVal, this });
|
2011-04-25 20:16:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-22 04:59:45 +01:00
|
|
|
|
catch (TargetInvocationException e)
|
2011-04-25 20:16:38 +02:00
|
|
|
|
{
|
2012-02-22 04:59:45 +01:00
|
|
|
|
DateTime parsedDate;
|
|
|
|
|
|
|
|
|
|
if (!DateTime.TryParse(dateVal, new CultureInfo("en-US"), DateTimeStyles.None, out parsedDate))
|
|
|
|
|
{
|
|
|
|
|
parsedDate = DateTime.UtcNow;
|
|
|
|
|
logger.WarnException("Unable to parse Feed date " + dateVal, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dateVal = parsedDate.ToString(CultureInfo.CurrentCulture.DateTimeFormat.RFC1123Pattern);
|
2011-04-25 20:16:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dateVal;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|