2019-12-23 07:18:21 +01:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
package atom // import "miniflux.app/reader/atom"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-02-14 20:09:06 +01:00
|
|
|
"html"
|
2019-12-23 07:18:21 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/reader/date"
|
|
|
|
"miniflux.app/reader/media"
|
2022-03-05 01:49:44 +01:00
|
|
|
"miniflux.app/reader/sanitizer"
|
2019-12-23 07:18:21 +01:00
|
|
|
"miniflux.app/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Specs:
|
|
|
|
// https://tools.ietf.org/html/rfc4287
|
|
|
|
// https://validator.w3.org/feed/docs/atom.html
|
|
|
|
type atom10Feed struct {
|
|
|
|
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
|
|
|
ID string `xml:"id"`
|
|
|
|
Title atom10Text `xml:"title"`
|
2022-01-15 05:12:22 +01:00
|
|
|
Authors atomAuthors `xml:"author"`
|
2019-12-23 07:18:21 +01:00
|
|
|
Links atomLinks `xml:"link"`
|
|
|
|
Entries []atom10Entry `xml:"entry"`
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
func (a *atom10Feed) Transform(baseURL string) *model.Feed {
|
|
|
|
var err error
|
|
|
|
|
2019-12-23 07:18:21 +01:00
|
|
|
feed := new(model.Feed)
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
feedURL := a.Links.firstLinkWithRelation("self")
|
|
|
|
feed.FeedURL, err = url.AbsoluteURL(baseURL, feedURL)
|
|
|
|
if err != nil {
|
|
|
|
feed.FeedURL = feedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
siteURL := a.Links.originalLink()
|
|
|
|
feed.SiteURL, err = url.AbsoluteURL(baseURL, siteURL)
|
|
|
|
if err != nil {
|
|
|
|
feed.SiteURL = siteURL
|
|
|
|
}
|
|
|
|
|
2021-03-19 05:34:09 +01:00
|
|
|
feed.Title = html.UnescapeString(a.Title.String())
|
2019-12-23 07:18:21 +01:00
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range a.Entries {
|
|
|
|
item := entry.Transform()
|
|
|
|
entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
|
|
|
|
if err == nil {
|
|
|
|
item.URL = entryURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.Author == "" {
|
2022-01-15 05:12:22 +01:00
|
|
|
item.Author = a.Authors.String()
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-05 01:49:44 +01:00
|
|
|
if item.Title == "" {
|
|
|
|
item.Title = sanitizer.TruncateHTML(item.Content, 100)
|
|
|
|
}
|
|
|
|
|
2019-12-23 07:18:21 +01:00
|
|
|
if item.Title == "" {
|
|
|
|
item.Title = item.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
feed.Entries = append(feed.Entries, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
|
|
|
type atom10Entry struct {
|
2023-02-25 05:52:45 +01:00
|
|
|
ID string `xml:"id"`
|
|
|
|
Title atom10Text `xml:"title"`
|
|
|
|
Published string `xml:"published"`
|
|
|
|
Updated string `xml:"updated"`
|
|
|
|
Links atomLinks `xml:"link"`
|
|
|
|
Summary atom10Text `xml:"summary"`
|
|
|
|
Content atom10Text `xml:"http://www.w3.org/2005/Atom content"`
|
|
|
|
Authors atomAuthors `xml:"author"`
|
|
|
|
Categories []atom10Category `xml:"category"`
|
2019-12-23 07:18:21 +01:00
|
|
|
media.Element
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atom10Entry) Transform() *model.Entry {
|
|
|
|
entry := new(model.Entry)
|
|
|
|
entry.URL = a.Links.originalLink()
|
|
|
|
entry.Date = a.entryDate()
|
2022-01-15 05:12:22 +01:00
|
|
|
entry.Author = a.Authors.String()
|
2019-12-23 07:18:21 +01:00
|
|
|
entry.Hash = a.entryHash()
|
|
|
|
entry.Content = a.entryContent()
|
|
|
|
entry.Title = a.entryTitle()
|
|
|
|
entry.Enclosures = a.entryEnclosures()
|
2020-01-05 00:18:24 +01:00
|
|
|
entry.CommentsURL = a.entryCommentsURL()
|
2023-02-25 05:52:45 +01:00
|
|
|
entry.Tags = a.entryCategories()
|
2019-12-23 07:18:21 +01:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atom10Entry) entryTitle() string {
|
2021-03-19 05:34:09 +01:00
|
|
|
return html.UnescapeString(a.Title.String())
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atom10Entry) entryContent() string {
|
|
|
|
content := a.Content.String()
|
|
|
|
if content != "" {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
summary := a.Summary.String()
|
|
|
|
if summary != "" {
|
|
|
|
return summary
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaDescription := a.FirstMediaDescription()
|
|
|
|
if mediaDescription != "" {
|
|
|
|
return mediaDescription
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: The published date represents the original creation date for YouTube feeds.
|
|
|
|
// Example:
|
|
|
|
// <published>2019-01-26T08:02:28+00:00</published>
|
|
|
|
// <updated>2019-01-29T07:27:27+00:00</updated>
|
|
|
|
func (a *atom10Entry) entryDate() time.Time {
|
|
|
|
dateText := a.Published
|
|
|
|
if dateText == "" {
|
|
|
|
dateText = a.Updated
|
|
|
|
}
|
|
|
|
|
|
|
|
if dateText != "" {
|
|
|
|
result, err := date.Parse(dateText)
|
|
|
|
if err != nil {
|
2020-11-17 06:31:39 +01:00
|
|
|
logger.Error("atom: %v (entry ID = %s)", err, a.ID)
|
2019-12-23 07:18:21 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atom10Entry) entryHash() string {
|
|
|
|
for _, value := range []string{a.ID, a.Links.originalLink()} {
|
|
|
|
if value != "" {
|
|
|
|
return crypto.Hash(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *atom10Entry) entryEnclosures() model.EnclosureList {
|
|
|
|
enclosures := make(model.EnclosureList, 0)
|
2021-03-23 05:04:10 +01:00
|
|
|
duplicates := make(map[string]bool)
|
2019-12-23 07:18:21 +01:00
|
|
|
|
|
|
|
for _, mediaThumbnail := range a.AllMediaThumbnails() {
|
|
|
|
if _, found := duplicates[mediaThumbnail.URL]; !found {
|
|
|
|
duplicates[mediaThumbnail.URL] = true
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: mediaThumbnail.URL,
|
|
|
|
MimeType: mediaThumbnail.MimeType(),
|
|
|
|
Size: mediaThumbnail.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range a.Links {
|
|
|
|
if strings.ToLower(link.Rel) == "enclosure" {
|
2020-01-31 06:08:11 +01:00
|
|
|
if link.URL == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-23 07:18:21 +01:00
|
|
|
if _, found := duplicates[link.URL]; !found {
|
|
|
|
duplicates[link.URL] = true
|
|
|
|
length, _ := strconv.ParseInt(link.Length, 10, 0)
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mediaContent := range a.AllMediaContents() {
|
|
|
|
if _, found := duplicates[mediaContent.URL]; !found {
|
|
|
|
duplicates[mediaContent.URL] = true
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: mediaContent.URL,
|
|
|
|
MimeType: mediaContent.MimeType(),
|
|
|
|
Size: mediaContent.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mediaPeerLink := range a.AllMediaPeerLinks() {
|
|
|
|
if _, found := duplicates[mediaPeerLink.URL]; !found {
|
|
|
|
duplicates[mediaPeerLink.URL] = true
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: mediaPeerLink.URL,
|
|
|
|
MimeType: mediaPeerLink.MimeType(),
|
|
|
|
Size: mediaPeerLink.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return enclosures
|
|
|
|
}
|
|
|
|
|
2023-02-25 05:52:45 +01:00
|
|
|
func (r *atom10Entry) entryCategories() []string {
|
|
|
|
var categoryList []string
|
|
|
|
|
|
|
|
for _, atomCategory := range r.Categories {
|
|
|
|
if strings.TrimSpace(atomCategory.Label) != "" {
|
|
|
|
categoryList = append(categoryList, strings.TrimSpace(atomCategory.Label))
|
|
|
|
} else {
|
|
|
|
categoryList = append(categoryList, strings.TrimSpace(atomCategory.Term))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return categoryList
|
|
|
|
}
|
|
|
|
|
2020-01-05 00:56:00 +01:00
|
|
|
// See https://tools.ietf.org/html/rfc4685#section-4
|
|
|
|
// If the type attribute of the atom:link is omitted, its value is assumed to be "application/atom+xml".
|
|
|
|
// We accept only HTML or XHTML documents for now since the intention is to have the same behavior as RSS.
|
2020-01-05 00:18:24 +01:00
|
|
|
func (a *atom10Entry) entryCommentsURL() string {
|
2020-01-05 00:56:00 +01:00
|
|
|
commentsURL := a.Links.firstLinkWithRelationAndType("replies", "text/html", "application/xhtml+xml")
|
2020-01-05 00:18:24 +01:00
|
|
|
if url.IsAbsoluteURL(commentsURL) {
|
|
|
|
return commentsURL
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-12-23 07:18:21 +01:00
|
|
|
type atom10Text struct {
|
2021-03-20 05:49:35 +01:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
CharData string `xml:",chardata"`
|
|
|
|
InnerXML string `xml:",innerxml"`
|
|
|
|
XHTMLRootElement atomXHTMLRootElement `xml:"http://www.w3.org/1999/xhtml div"`
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:52:45 +01:00
|
|
|
type atom10Category struct {
|
|
|
|
Term string `xml:"term,attr"`
|
|
|
|
Label string `xml:"label,attr"`
|
|
|
|
}
|
|
|
|
|
2022-03-04 23:18:43 +01:00
|
|
|
// Text: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.1
|
|
|
|
// HTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.2
|
|
|
|
// XHTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.3
|
2019-12-23 07:18:21 +01:00
|
|
|
func (a *atom10Text) String() string {
|
2021-03-19 05:34:09 +01:00
|
|
|
var content string
|
2021-03-20 05:49:35 +01:00
|
|
|
switch {
|
|
|
|
case a.Type == "", a.Type == "text", a.Type == "text/plain":
|
2022-01-17 22:31:11 +01:00
|
|
|
if strings.HasPrefix(strings.TrimSpace(a.InnerXML), `<![CDATA[`) {
|
2021-03-21 04:38:29 +01:00
|
|
|
content = html.EscapeString(a.CharData)
|
|
|
|
} else {
|
|
|
|
content = a.InnerXML
|
|
|
|
}
|
2021-03-20 05:49:35 +01:00
|
|
|
case a.Type == "xhtml":
|
2022-04-06 03:22:06 +02:00
|
|
|
var root = a.XHTMLRootElement
|
|
|
|
if root.XMLName.Local == "div" {
|
|
|
|
content = root.InnerXML
|
|
|
|
} else {
|
|
|
|
content = a.InnerXML
|
|
|
|
}
|
2021-03-20 05:49:35 +01:00
|
|
|
default:
|
2021-03-19 05:34:09 +01:00
|
|
|
content = a.CharData
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 05:34:09 +01:00
|
|
|
return strings.TrimSpace(content)
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
2021-03-20 05:49:35 +01:00
|
|
|
|
|
|
|
type atomXHTMLRootElement struct {
|
2022-04-06 03:22:06 +02:00
|
|
|
XMLName xml.Name `xml:"div"`
|
|
|
|
InnerXML string `xml:",innerxml"`
|
2021-03-20 05:49:35 +01:00
|
|
|
}
|