2017-11-20 21:34:11 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package rdf // import "miniflux.app/reader/rdf"
|
2017-11-20 21:34:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-03-19 21:39:44 -04:00
|
|
|
"html"
|
2017-11-22 17:52:31 -05:00
|
|
|
"strings"
|
2017-11-20 22:25:30 -05:00
|
|
|
"time"
|
2017-11-20 21:34:11 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/reader/date"
|
|
|
|
"miniflux.app/reader/sanitizer"
|
|
|
|
"miniflux.app/url"
|
2017-11-20 21:34:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type rdfFeed struct {
|
|
|
|
XMLName xml.Name `xml:"RDF"`
|
|
|
|
Title string `xml:"channel>title"`
|
|
|
|
Link string `xml:"channel>link"`
|
|
|
|
Items []rdfItem `xml:"item"`
|
2019-12-23 17:39:54 -05:00
|
|
|
DublinCoreFeedElement
|
2017-11-20 21:34:11 -05:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:47:11 -05:00
|
|
|
func (r *rdfFeed) Transform(baseURL string) *model.Feed {
|
|
|
|
var err error
|
2017-11-20 21:34:11 -05:00
|
|
|
feed := new(model.Feed)
|
|
|
|
feed.Title = sanitizer.StripTags(r.Title)
|
2020-12-02 23:47:11 -05:00
|
|
|
feed.FeedURL = baseURL
|
|
|
|
feed.SiteURL, err = url.AbsoluteURL(baseURL, r.Link)
|
|
|
|
if err != nil {
|
|
|
|
feed.SiteURL = r.Link
|
|
|
|
}
|
2017-11-20 21:34:11 -05:00
|
|
|
|
|
|
|
for _, item := range r.Items {
|
|
|
|
entry := item.Transform()
|
2019-12-23 17:39:54 -05:00
|
|
|
if entry.Author == "" && r.DublinCoreCreator != "" {
|
|
|
|
entry.Author = strings.TrimSpace(r.DublinCoreCreator)
|
2017-11-20 21:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if entry.URL == "" {
|
|
|
|
entry.URL = feed.SiteURL
|
2017-12-13 23:16:15 -05:00
|
|
|
} else {
|
|
|
|
entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
|
|
|
|
if err == nil {
|
|
|
|
entry.URL = entryURL
|
|
|
|
}
|
2017-11-20 21:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
feed.Entries = append(feed.Entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
|
|
|
type rdfItem struct {
|
|
|
|
Title string `xml:"title"`
|
|
|
|
Link string `xml:"link"`
|
|
|
|
Description string `xml:"description"`
|
2019-12-23 17:39:54 -05:00
|
|
|
DublinCoreEntryElement
|
2017-11-20 21:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rdfItem) Transform() *model.Entry {
|
|
|
|
entry := new(model.Entry)
|
2019-12-23 17:39:54 -05:00
|
|
|
entry.Title = r.entryTitle()
|
|
|
|
entry.Author = r.entryAuthor()
|
|
|
|
entry.URL = r.entryURL()
|
|
|
|
entry.Content = r.entryContent()
|
|
|
|
entry.Hash = r.entryHash()
|
|
|
|
entry.Date = r.entryDate()
|
2017-11-20 21:34:11 -05:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
2019-12-23 17:39:54 -05:00
|
|
|
func (r *rdfItem) entryTitle() string {
|
2021-03-19 21:39:44 -04:00
|
|
|
return html.UnescapeString(strings.TrimSpace(r.Title))
|
2019-12-23 17:39:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rdfItem) entryContent() string {
|
|
|
|
switch {
|
|
|
|
case r.DublinCoreContent != "":
|
|
|
|
return r.DublinCoreContent
|
|
|
|
default:
|
|
|
|
return r.Description
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rdfItem) entryAuthor() string {
|
|
|
|
return strings.TrimSpace(r.DublinCoreCreator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rdfItem) entryURL() string {
|
|
|
|
return strings.TrimSpace(r.Link)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rdfItem) entryDate() time.Time {
|
|
|
|
if r.DublinCoreDate != "" {
|
|
|
|
result, err := date.Parse(r.DublinCoreDate)
|
2018-04-10 21:13:05 -04:00
|
|
|
if err != nil {
|
2020-11-17 20:01:59 -05:00
|
|
|
logger.Error("rdf: %v (entry link = %s)", err, r.Link)
|
2018-04-10 21:13:05 -04:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2019-12-23 17:39:54 -05:00
|
|
|
func (r *rdfItem) entryHash() string {
|
2017-11-20 21:34:11 -05:00
|
|
|
value := r.Link
|
|
|
|
if value == "" {
|
|
|
|
value = r.Title + r.Description
|
|
|
|
}
|
|
|
|
|
2018-01-02 22:15:08 -05:00
|
|
|
return crypto.Hash(value)
|
2017-11-20 21:34:11 -05:00
|
|
|
}
|