From a8815cd5eaecddb62b9deca193a21e2325b40550 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 25 Apr 2011 11:16:38 -0700 Subject: [PATCH] Fixed rss datetime bug --- .gitignore | 3 +- NzbDrone.Core.Test/IndexerProviderTest.cs | 6 +- .../Instrumentation/SubsonicTarget.cs | 2 - NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.Core/Providers/Core/HttpProvider.cs | 58 +- .../Providers/Indexer/IndexerProviderBase.cs | 22 +- .../Indexer/SyndicationFeedXmlReader.cs | 67 + NzbDrone.Web/NzbDrone.Web.Publish.xml | 1136 ----------------- 8 files changed, 100 insertions(+), 1195 deletions(-) create mode 100644 NzbDrone.Core/Providers/Indexer/SyndicationFeedXmlReader.cs delete mode 100644 NzbDrone.Web/NzbDrone.Web.Publish.xml diff --git a/.gitignore b/.gitignore index 5cbd6a1a1..5a23bee56 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,5 @@ _ReSharper*/ /[Pp]ackage/ #NZBDrone specific *.db -*Web.Publish.xml \ No newline at end of file +*Web.Publish.xml +NzbDrone.Web/NzbDrone.Web.Publish.xml \ No newline at end of file diff --git a/NzbDrone.Core.Test/IndexerProviderTest.cs b/NzbDrone.Core.Test/IndexerProviderTest.cs index de00de58b..ed38159ab 100644 --- a/NzbDrone.Core.Test/IndexerProviderTest.cs +++ b/NzbDrone.Core.Test/IndexerProviderTest.cs @@ -28,11 +28,9 @@ namespace NzbDrone.Core.Test { var mocker = new AutoMoqer(); - var xmlReader = XmlReader.Create(File.OpenRead(".\\Files\\Rss\\" + fileName)); - mocker.GetMock() - .Setup(h => h.DownloadXml(It.IsAny())) - .Returns(xmlReader); + .Setup(h => h.DownloadStream(It.IsAny())) + .Returns(File.OpenRead(".\\Files\\Rss\\" + fileName)); var fakeSettings = Builder.CreateNew().Build(); mocker.GetMock() diff --git a/NzbDrone.Core/Instrumentation/SubsonicTarget.cs b/NzbDrone.Core/Instrumentation/SubsonicTarget.cs index 88aa61628..ba53d985b 100644 --- a/NzbDrone.Core/Instrumentation/SubsonicTarget.cs +++ b/NzbDrone.Core/Instrumentation/SubsonicTarget.cs @@ -25,8 +25,6 @@ namespace NzbDrone.Core.Instrumentation log.Method = logEvent.UserStackFrame.GetMethod().Name; } - - log.Logger = logEvent.LoggerName; if (logEvent.Exception != null) diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 94a53b6bd..b7fa36edb 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -166,6 +166,7 @@ + diff --git a/NzbDrone.Core/Providers/Core/HttpProvider.cs b/NzbDrone.Core/Providers/Core/HttpProvider.cs index 75b2ebceb..91ff84661 100644 --- a/NzbDrone.Core/Providers/Core/HttpProvider.cs +++ b/NzbDrone.Core/Providers/Core/HttpProvider.cs @@ -1,80 +1,54 @@ using System; +using System.IO; using System.Net; -using System.Xml; using NLog; + namespace NzbDrone.Core.Providers.Core { public class HttpProvider { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public virtual string DownloadString(string request) - { - try - { - return new WebClient().DownloadString(request); - } - catch (Exception ex) - { - Logger.Warn("Failed to get response from: {0}", request); - Logger.TraceException(ex.Message, ex); - } - - return String.Empty; - } - - public virtual string DownloadString(string request, string username, string password) - { - try - { - var webClient = new WebClient(); - webClient.Credentials = new NetworkCredential(username, password); - return webClient.DownloadString(request); - } - catch (Exception ex) - { - Logger.Warn("Failed to get response from: {0}", request); - Logger.TraceException(ex.Message, ex); - } - - return String.Empty; - } - - public virtual void DownloadFile(string request, string filename) + public virtual string DownloadString(string address) { try { - var webClient = new WebClient(); - webClient.DownloadFile(request, filename); + return new WebClient().DownloadString(address); } catch (Exception ex) { - Logger.Warn("Failed to get response from: {0}", request); + Logger.Warn("Failed to get response from: {0}", address); Logger.TraceException(ex.Message, ex); throw; } } - public virtual void DownloadFile(string request, string filename, string username, string password) + public virtual string DownloadString(string address, string username, string password) { try { var webClient = new WebClient(); webClient.Credentials = new NetworkCredential(username, password); - webClient.DownloadFile(request, filename); + return webClient.DownloadString(address); } catch (Exception ex) { - Logger.Warn("Failed to get response from: {0}", request); + Logger.Warn("Failed to get response from: {0}", address); Logger.TraceException(ex.Message, ex); throw; } } - public virtual XmlReader DownloadXml(string url) + public virtual Stream DownloadStream(string url) { - return XmlReader.Create(url); + var request = WebRequest.Create(url); + + var response = request.GetResponse(); + + return response.GetResponseStream(); } + + } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/Indexer/IndexerProviderBase.cs b/NzbDrone.Core/Providers/Indexer/IndexerProviderBase.cs index 33a38a644..82c9aedf7 100644 --- a/NzbDrone.Core/Providers/Indexer/IndexerProviderBase.cs +++ b/NzbDrone.Core/Providers/Indexer/IndexerProviderBase.cs @@ -69,7 +69,9 @@ namespace NzbDrone.Core.Providers.Indexer try { _logger.Trace("Downloading RSS " + url); - var feed = SyndicationFeed.Load(_httpProvider.DownloadXml(url)).Items; + + var reader = new SyndicationFeedXmlReader(_httpProvider.DownloadStream(url)); + var feed = SyndicationFeed.Load(reader).Items; foreach (var item in feed) { @@ -135,17 +137,17 @@ namespace NzbDrone.Core.Providers.Indexer return; } - var sabTitle = _sabProvider.GetSabTitle(parseResult); + //var sabTitle = _sabProvider.GetSabTitle(parseResult); - if (_sabProvider.IsInQueue(sabTitle)) - { - return; - } + //if (_sabProvider.IsInQueue(sabTitle)) + //{ + // return; + //} - if (!_sabProvider.AddByUrl(NzbDownloadUrl(feedItem), sabTitle)) - { - return; - } + //if (!_sabProvider.AddByUrl(NzbDownloadUrl(feedItem), sabTitle)) + //{ + // return; + //} foreach (var episode in episodes) { diff --git a/NzbDrone.Core/Providers/Indexer/SyndicationFeedXmlReader.cs b/NzbDrone.Core/Providers/Indexer/SyndicationFeedXmlReader.cs new file mode 100644 index 000000000..df9d54cc5 --- /dev/null +++ b/NzbDrone.Core/Providers/Indexer/SyndicationFeedXmlReader.cs @@ -0,0 +1,67 @@ +//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; + +namespace NzbDrone.Core.Providers.Indexer +{ + + public class SyndicationFeedXmlReader : XmlTextReader + { + readonly string[] Rss20DateTimeHints = { "pubDate" }; + readonly string[] Atom10DateTimeHints = { "updated", "published", "lastBuildDate" }; + private bool isRss2DateTime = false; + private bool isAtomDateTime = false; + + public SyndicationFeedXmlReader(Stream stream) : base(stream) { } + + public override bool IsStartElement(string localname, string ns) + { + isRss2DateTime = false; + isAtomDateTime = false; + + if (Rss20DateTimeHints.Contains(localname)) isRss2DateTime = true; + if (Atom10DateTimeHints.Contains(localname)) isAtomDateTime = true; + + return base.IsStartElement(localname, ns); + } + + public override string ReadString() + { + string dateVal = base.ReadString(); + + try + { + if (isRss2DateTime) + { + MethodInfo objMethod = typeof(Rss20FeedFormatter).GetMethod("DateFromString", BindingFlags.NonPublic | BindingFlags.Static); + Debug.Assert(objMethod != null); + objMethod.Invoke(null, new object[] { dateVal, this }); + + } + if (isAtomDateTime) + { + MethodInfo objMethod = typeof(Atom10FeedFormatter).GetMethod("DateFromString", BindingFlags.NonPublic | BindingFlags.Instance); + Debug.Assert(objMethod != null); + objMethod.Invoke(new Atom10FeedFormatter(), new object[] { dateVal, this }); + } + } + catch (TargetInvocationException) + { + DateTimeFormatInfo dtfi = CultureInfo.CurrentCulture.DateTimeFormat; + return DateTimeOffset.UtcNow.ToString(dtfi.RFC1123Pattern); + } + + return dateVal; + + } + + } +} diff --git a/NzbDrone.Web/NzbDrone.Web.Publish.xml b/NzbDrone.Web/NzbDrone.Web.Publish.xml deleted file mode 100644 index f2cd33dbd..000000000 --- a/NzbDrone.Web/NzbDrone.Web.Publish.xml +++ /dev/null @@ -1,1136 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file