Add support for Dublin Core date in RDF feeds

This commit is contained in:
Frédéric Guillot 2018-04-10 18:13:05 -07:00
parent 15202b8675
commit dcbb5047b1
2 changed files with 73 additions and 1 deletions

View File

@ -322,6 +322,61 @@ func TestParseItemWithoutLink(t *testing.T) {
}
}
func TestParseItemWithDublicCoreDate(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title>Example</title>
<link>http://example.org</link>
</channel>
<item>
<title>Title</title>
<description>Test</description>
<link>http://example.org/test.html</link>
<dc:creator>Tester</dc:creator>
<dc:date>2018-04-10T05:00:00+00:00</dc:date>
</item>
</rdf:RDF>`
feed, err := Parse(bytes.NewBufferString(data))
if err != nil {
t.Error(err)
}
expectedDate := time.Date(2018, time.April, 10, 5, 0, 0, 0, time.UTC)
if !feed.Entries[0].Date.Equal(expectedDate) {
t.Errorf("Incorrect entry date, got: %v, want: %v", feed.Entries[0].Date, expectedDate)
}
}
func TestParseItemWithoutDate(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">
<channel>
<title>Example</title>
<link>http://example.org</link>
</channel>
<item>
<title>Title</title>
<description>Test</description>
<link>http://example.org/test.html</link>
</item>
</rdf:RDF>`
feed, err := Parse(bytes.NewBufferString(data))
if err != nil {
t.Error(err)
}
expectedDate := time.Now().In(time.Local)
diff := expectedDate.Sub(feed.Entries[0].Date)
if diff > time.Second {
t.Errorf("Incorrect entry date, got: %v", diff)
}
}
func TestParseInvalidXml(t *testing.T) {
data := `garbage`
_, err := Parse(bytes.NewBufferString(data))

View File

@ -10,7 +10,9 @@ import (
"time"
"github.com/miniflux/miniflux/crypto"
"github.com/miniflux/miniflux/logger"
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/reader/date"
"github.com/miniflux/miniflux/reader/sanitizer"
"github.com/miniflux/miniflux/url"
)
@ -54,6 +56,7 @@ type rdfItem struct {
Link string `xml:"link"`
Description string `xml:"description"`
Creator string `xml:"creator"`
Date string `xml:"date"`
}
func (r *rdfItem) Transform() *model.Entry {
@ -63,10 +66,24 @@ func (r *rdfItem) Transform() *model.Entry {
entry.URL = r.Link
entry.Content = r.Description
entry.Hash = getHash(r)
entry.Date = time.Now()
entry.Date = getDate(r)
return entry
}
func getDate(r *rdfItem) time.Time {
if r.Date != "" {
result, err := date.Parse(r.Date)
if err != nil {
logger.Error("rdf: %v", err)
return time.Now()
}
return result
}
return time.Now()
}
func getHash(r *rdfItem) string {
value := r.Link
if value == "" {