From 5365f31e901757ad7a2b69aebf1fabc34e7f9a68 Mon Sep 17 00:00:00 2001
From: neepl <1105374+neepl@users.noreply.github.com>
Date: Wed, 18 Jul 2018 07:52:05 +0300
Subject: [PATCH] Add support for published tag in Atom feeds
---
reader/atom/atom.go | 10 ++++++--
reader/atom/parser_test.go | 51 ++++++++++++++++++++++++++++++++++++++
2 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/reader/atom/atom.go b/reader/atom/atom.go
index 96a429fb..274d67f7 100644
--- a/reader/atom/atom.go
+++ b/reader/atom/atom.go
@@ -30,6 +30,7 @@ type atomFeed struct {
type atomEntry struct {
ID string `xml:"id"`
Title atomContent `xml:"title"`
+ Published string `xml:"published"`
Updated string `xml:"updated"`
Links []atomLink `xml:"link"`
Summary string `xml:"summary"`
@@ -128,8 +129,13 @@ func getRelationURL(links []atomLink, relation string) string {
}
func getDate(a *atomEntry) time.Time {
- if a.Updated != "" {
- result, err := date.Parse(a.Updated)
+ dateText := a.Updated
+ if dateText == "" {
+ dateText = a.Published
+ }
+
+ if dateText != "" {
+ result, err := date.Parse(dateText)
if err != nil {
logger.Error("atom: %v", err)
return time.Now()
diff --git a/reader/atom/parser_test.go b/reader/atom/parser_test.go
index bfd3d3a2..e6cfdd78 100644
--- a/reader/atom/parser_test.go
+++ b/reader/atom/parser_test.go
@@ -422,6 +422,57 @@ func TestParseEntryWithEnclosures(t *testing.T) {
}
}
+func TestParseEntryWithPublished(t *testing.T) {
+ data := `
+
+ Example Feed
+
+
+
+
+ urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a
+ 2003-12-13T18:30:02Z
+ Some text.
+
+
+ `
+
+ feed, err := Parse(bytes.NewBufferString(data))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if !feed.Entries[0].Date.Equal(time.Date(2003, time.December, 13, 18, 30, 2, 0, time.UTC)) {
+ t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
+ }
+}
+
+func TestParseEntryWithPublishedAndUpdated(t *testing.T) {
+ data := `
+
+ Example Feed
+
+
+
+
+ urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a
+ 2002-11-12T18:30:02Z
+ 2003-12-13T18:30:02Z
+ Some text.
+
+
+ `
+
+ feed, err := Parse(bytes.NewBufferString(data))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if !feed.Entries[0].Date.Equal(time.Date(2003, time.December, 13, 18, 30, 2, 0, time.UTC)) {
+ t.Errorf("Incorrect entry date, got: %v", feed.Entries[0].Date)
+ }
+}
+
func TestParseInvalidXml(t *testing.T) {
data := `garbage`
_, err := Parse(bytes.NewBufferString(data))