2017-11-20 00:10:04 -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 atom // import "miniflux.app/reader/atom"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2019-01-01 16:01:19 -05:00
|
|
|
"html"
|
2017-11-20 21:50:16 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomFeed struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
|
|
|
ID string `xml:"id"`
|
|
|
|
Title string `xml:"title"`
|
2017-11-20 21:50:16 -05:00
|
|
|
Author atomAuthor `xml:"author"`
|
|
|
|
Links []atomLink `xml:"link"`
|
|
|
|
Entries []atomEntry `xml:"entry"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomEntry struct {
|
|
|
|
ID string `xml:"id"`
|
2018-02-17 15:21:58 -05:00
|
|
|
Title atomContent `xml:"title"`
|
2018-07-18 00:52:05 -04:00
|
|
|
Published string `xml:"published"`
|
2017-11-20 21:50:16 -05:00
|
|
|
Updated string `xml:"updated"`
|
|
|
|
Links []atomLink `xml:"link"`
|
2019-01-01 16:01:19 -05:00
|
|
|
Summary atomContent `xml:"summary"`
|
2017-11-20 21:50:16 -05:00
|
|
|
Content atomContent `xml:"content"`
|
|
|
|
MediaGroup atomMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
|
|
|
|
Author atomAuthor `xml:"author"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomAuthor struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Name string `xml:"name"`
|
|
|
|
Email string `xml:"email"`
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomLink struct {
|
|
|
|
URL string `xml:"href,attr"`
|
2017-11-20 00:10:04 -05:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Rel string `xml:"rel,attr"`
|
|
|
|
Length string `xml:"length,attr"`
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomContent struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Data string `xml:",chardata"`
|
2017-11-20 21:50:16 -05:00
|
|
|
XML string `xml:",innerxml"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
type atomMediaGroup struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Description string `xml:"http://search.yahoo.com/mrss/ description"`
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func (a *atomFeed) Transform() *model.Feed {
|
2017-11-20 00:10:04 -05:00
|
|
|
feed := new(model.Feed)
|
2017-11-20 21:50:16 -05:00
|
|
|
feed.FeedURL = getRelationURL(a.Links, "self")
|
|
|
|
feed.SiteURL = getURL(a.Links)
|
2017-11-22 17:52:31 -05:00
|
|
|
feed.Title = strings.TrimSpace(a.Title)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range a.Entries {
|
|
|
|
item := entry.Transform()
|
2017-12-13 23:16:15 -05:00
|
|
|
entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
|
|
|
|
if err == nil {
|
|
|
|
item.URL = entryURL
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
if item.Author == "" {
|
2017-11-20 21:50:16 -05:00
|
|
|
item.Author = getAuthor(a.Author)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:16:15 -05:00
|
|
|
if item.Title == "" {
|
|
|
|
item.Title = item.URL
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:10:04 -05:00
|
|
|
feed.Entries = append(feed.Entries, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func (a *atomEntry) Transform() *model.Entry {
|
|
|
|
entry := new(model.Entry)
|
|
|
|
entry.URL = getURL(a.Links)
|
|
|
|
entry.Date = getDate(a)
|
2017-11-22 17:52:31 -05:00
|
|
|
entry.Author = getAuthor(a.Author)
|
2017-11-20 21:50:16 -05:00
|
|
|
entry.Hash = getHash(a)
|
2017-12-12 01:16:32 -05:00
|
|
|
entry.Content = getContent(a)
|
2018-02-17 15:21:58 -05:00
|
|
|
entry.Title = getTitle(a)
|
2017-11-20 21:50:16 -05:00
|
|
|
entry.Enclosures = getEnclosures(a)
|
|
|
|
return entry
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getURL(links []atomLink) string {
|
|
|
|
for _, link := range links {
|
2017-11-20 00:10:04 -05:00
|
|
|
if strings.ToLower(link.Rel) == "alternate" {
|
2017-11-22 17:52:31 -05:00
|
|
|
return strings.TrimSpace(link.URL)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if link.Rel == "" && link.Type == "" {
|
2017-11-22 17:52:31 -05:00
|
|
|
return strings.TrimSpace(link.URL)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getRelationURL(links []atomLink, relation string) string {
|
|
|
|
for _, link := range links {
|
|
|
|
if strings.ToLower(link.Rel) == relation {
|
2017-11-22 17:52:31 -05:00
|
|
|
return strings.TrimSpace(link.URL)
|
2017-11-20 21:50:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getDate(a *atomEntry) time.Time {
|
2019-01-29 23:01:36 -05:00
|
|
|
// 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>
|
|
|
|
dateText := a.Published
|
2018-07-18 00:52:05 -04:00
|
|
|
if dateText == "" {
|
2019-01-29 23:01:36 -05:00
|
|
|
dateText = a.Updated
|
2018-07-18 00:52:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if dateText != "" {
|
|
|
|
result, err := date.Parse(dateText)
|
2017-11-20 21:50:16 -05:00
|
|
|
if err != nil {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Error("atom: %v", err)
|
2017-11-20 21:50:16 -05:00
|
|
|
return time.Now()
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
2017-11-20 21:50:16 -05:00
|
|
|
|
|
|
|
return result
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
return time.Now()
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-01-01 16:01:19 -05:00
|
|
|
func atomContentToString(c atomContent) string {
|
|
|
|
if c.Type == "xhtml" {
|
|
|
|
return c.XML
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-01-01 16:01:19 -05:00
|
|
|
if c.Type == "html" {
|
|
|
|
return c.Data
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2019-01-01 16:01:19 -05:00
|
|
|
if c.Type == "text" || c.Type == "" {
|
|
|
|
return html.EscapeString(c.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContent(a *atomEntry) string {
|
|
|
|
r := atomContentToString(a.Content)
|
|
|
|
if r != "" {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
r = atomContentToString(a.Summary)
|
|
|
|
if r != "" {
|
|
|
|
return r
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
if a.MediaGroup.Description != "" {
|
|
|
|
return a.MediaGroup.Description
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2018-02-17 15:21:58 -05:00
|
|
|
func getTitle(a *atomEntry) string {
|
2019-01-01 16:01:19 -05:00
|
|
|
title := atomContentToString(a.Title)
|
2018-02-17 15:21:58 -05:00
|
|
|
return strings.TrimSpace(sanitizer.StripTags(title))
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getHash(a *atomEntry) string {
|
|
|
|
for _, value := range []string{a.ID, getURL(a.Links)} {
|
|
|
|
if value != "" {
|
2018-01-02 22:15:08 -05:00
|
|
|
return crypto.Hash(value)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
return ""
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getEnclosures(a *atomEntry) model.EnclosureList {
|
|
|
|
enclosures := make(model.EnclosureList, 0)
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
for _, link := range a.Links {
|
|
|
|
if strings.ToLower(link.Rel) == "enclosure" {
|
2018-03-14 23:09:06 -04:00
|
|
|
length, _ := strconv.ParseInt(link.Length, 10, 0)
|
2017-11-20 21:50:16 -05:00
|
|
|
enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
|
|
|
|
}
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
return enclosures
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:50:16 -05:00
|
|
|
func getAuthor(author atomAuthor) string {
|
2017-11-20 00:10:04 -05:00
|
|
|
if author.Name != "" {
|
2017-11-22 17:52:31 -05:00
|
|
|
return strings.TrimSpace(author.Name)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if author.Email != "" {
|
2017-11-22 17:52:31 -05:00
|
|
|
return strings.TrimSpace(author.Email)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|