Don't crash when feedparser returns an unformattable date.
authorAdam Sampson <ats@offog.org>
Sun, 12 Jul 2015 13:23:46 +0000 (14:23 +0100)
committerAdam Sampson <ats@offog.org>
Sun, 12 Jul 2015 13:23:46 +0000 (14:23 +0100)
NEWS
rawdoglib/rawdog.py
test-rawdog

diff --git a/NEWS b/NEWS
index 0ae388ce3cbce2d842949beb74699a4fcec8695f..1905acfc4846a5cff445952878acd1c71e6832b5 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,11 @@ Fix some style problems reported by pylint.
 Use cStringIO rather than StringIO in all modules (rather than some
 using one and some using the other).
 
+Don't crash when feedparser returns a date that Python can't format.
+Since feedparser's date parser is pretty liberal, it can occasionally
+interpret an invalid date incorrectly (e.g. treating a time zone as a
+year number).
+
 - rawdog 2.20
 
 Add a test for the maxage option (suggested by joelmo).
index 3721f9d161849aa7d4ddaacc7438b5ac3cc41ac6..ca66568e7d6396dc27ec2fbc6afa1e66fa920c78 100644 (file)
@@ -73,7 +73,10 @@ def get_system_encoding():
 def safe_ftime(format, t):
        """Format a time value into a string in the current locale (as
        time.strftime), but encode the result as ASCII HTML."""
-       u = unicode(time.strftime(format, t), get_system_encoding())
+       try:
+               u = unicode(time.strftime(format, t), get_system_encoding())
+       except ValueError, e:
+               u = u"(bad time %s; %s)" % (repr(t), str(e))
        return encode_references(u)
 
 def format_time(secs, config):
index eadf2d65848c40629110cef0e3d7511d87ed5225..9ee173679324b08c4c1792020ca1357dfa6b6554 100755 (executable)
@@ -1279,6 +1279,39 @@ if [ -n "$ja_LC_ALL" ]; then
        contains $statedir/output.html "HEADING-Tuesday" "ITEMDATE-Tuesday"
 fi
 
+begin "two-digit year with timezone"
+# As of 2015-07, feedparser treats the time zone as the year number in the date
+# below -- seen in the real world in <http://zeptobars.ru/en/rss>.
+# But time.strftime doesn't know how to format dates in the year 300 AD!
+cat >$httpdir/feed.rss <<EOF
+<rss version="2.0">
+  <channel>
+    <title>example-feed-title</title>
+    <link>http://example.org/</link>
+    <description>example-feed-description</description>
+    <item>
+      <title>example-item-title</title>
+      <link>http://example.org/item</link>
+      <pubDate>Fri, 20 Mar 15 17:32:14 +0300</pubDate>
+    </item>
+  </channel>
+</rss>
+EOF
+add "feed 0 $httpurl/feed.rss"
+runs -uw
+
+begin "year before 1000"
+make_atom10 $httpdir/feed.atom
+add "feed 0 $httpurl/feed.atom"
+cat >$statedir/plugins/mangle.py <<EOF
+import rawdoglib.plugins
+def seen(rawdog, config, article, ignore):
+    # 25th July 385
+    article.date = -50000000000
+rawdoglib.plugins.attach_hook("article_seen", seen)
+EOF
+runs -uw
+
 begin "item authors"
 cat >$httpdir/feed.atom <<EOF
 <?xml version="1.0" encoding="utf-8"?>