1
0
Fork 0
miniflux/reader/rdf/rdf.go

95 lines
2 KiB
Go
Raw Normal View History

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"
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"`
Creator string `xml:"channel>creator"`
Items []rdfItem `xml:"item"`
}
func (r *rdfFeed) Transform() *model.Feed {
feed := new(model.Feed)
feed.Title = sanitizer.StripTags(r.Title)
feed.SiteURL = r.Link
for _, item := range r.Items {
entry := item.Transform()
if entry.Author == "" && r.Creator != "" {
entry.Author = sanitizer.StripTags(r.Creator)
}
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"`
Creator string `xml:"creator"`
Date string `xml:"date"`
2017-11-20 21:34:11 -05:00
}
func (r *rdfItem) Transform() *model.Entry {
entry := new(model.Entry)
2017-11-22 17:52:31 -05:00
entry.Title = strings.TrimSpace(r.Title)
entry.Author = strings.TrimSpace(r.Creator)
2019-12-01 17:58:07 -05:00
entry.URL = strings.TrimSpace(r.Link)
entry.Content = r.Description
2017-11-20 21:34:11 -05:00
entry.Hash = getHash(r)
entry.Date = getDate(r)
2017-11-20 21:34:11 -05:00
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()
}
2017-11-20 21:34:11 -05:00
func getHash(r *rdfItem) string {
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
}