2017-11-20 06:10:04 +01: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 06:51:50 +02:00
|
|
|
package rss // import "miniflux.app/reader/rss"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-02-13 22:26:55 +01:00
|
|
|
"html"
|
2017-11-20 06:10:04 +01:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/reader/date"
|
2019-11-29 08:47:53 +01:00
|
|
|
"miniflux.app/reader/media"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/reader/sanitizer"
|
|
|
|
"miniflux.app/url"
|
2017-11-21 00:15:10 +01:00
|
|
|
)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
// Specs: https://cyber.harvard.edu/rss/rss.html
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssFeed struct {
|
2019-12-23 22:29:53 +01:00
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"version,attr"`
|
|
|
|
Title string `xml:"channel>title"`
|
|
|
|
Links []rssLink `xml:"channel>link"`
|
|
|
|
Language string `xml:"channel>language"`
|
|
|
|
Description string `xml:"channel>description"`
|
|
|
|
PubDate string `xml:"channel>pubDate"`
|
|
|
|
ManagingEditor string `xml:"channel>managingEditor"`
|
|
|
|
Webmaster string `xml:"channel>webMaster"`
|
|
|
|
Items []rssItem `xml:"channel>item"`
|
|
|
|
PodcastFeedElement
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
func (r *rssFeed) Transform(baseURL string) *model.Feed {
|
|
|
|
var err error
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
feed := new(model.Feed)
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
siteURL := r.siteURL()
|
|
|
|
feed.SiteURL, err = url.AbsoluteURL(baseURL, siteURL)
|
|
|
|
if err != nil {
|
|
|
|
feed.SiteURL = siteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
feedURL := r.feedURL()
|
|
|
|
feed.FeedURL, err = url.AbsoluteURL(baseURL, feedURL)
|
|
|
|
if err != nil {
|
|
|
|
feed.FeedURL = feedURL
|
|
|
|
}
|
|
|
|
|
2021-05-01 07:49:17 +02:00
|
|
|
feed.Title = html.UnescapeString(strings.TrimSpace(r.Title))
|
2019-12-23 22:29:53 +01:00
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range r.Items {
|
|
|
|
entry := item.Transform()
|
|
|
|
if entry.Author == "" {
|
|
|
|
entry.Author = r.feedAuthor()
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.URL == "" {
|
|
|
|
entry.URL = feed.SiteURL
|
|
|
|
} else {
|
|
|
|
entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
|
|
|
|
if err == nil {
|
|
|
|
entry.URL = entryURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.Title == "" {
|
|
|
|
entry.Title = entry.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
feed.Entries = append(feed.Entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rssFeed) siteURL() string {
|
|
|
|
for _, element := range r.Links {
|
|
|
|
if element.XMLName.Space == "" {
|
|
|
|
return strings.TrimSpace(element.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rssFeed) feedURL() string {
|
|
|
|
for _, element := range r.Links {
|
|
|
|
if element.XMLName.Space == "http://www.w3.org/2005/Atom" {
|
|
|
|
return strings.TrimSpace(element.Href)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r rssFeed) feedAuthor() string {
|
|
|
|
author := r.PodcastAuthor()
|
|
|
|
switch {
|
|
|
|
case r.ManagingEditor != "":
|
|
|
|
author = r.ManagingEditor
|
|
|
|
case r.Webmaster != "":
|
|
|
|
author = r.Webmaster
|
|
|
|
}
|
2021-01-03 20:33:10 +01:00
|
|
|
return sanitizer.StripTags(strings.TrimSpace(author))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssLink struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
Href string `xml:"href,attr"`
|
2017-11-21 00:48:26 +01:00
|
|
|
Rel string `xml:"rel,attr"`
|
2017-11-21 00:15:10 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 05:30:55 +02:00
|
|
|
type rssCommentLink struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
}
|
|
|
|
|
2018-04-10 05:38:12 +02:00
|
|
|
type rssAuthor struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
Name string `xml:"name"`
|
2019-12-23 22:29:53 +01:00
|
|
|
Email string `xml:"email"`
|
2018-04-10 05:38:12 +02:00
|
|
|
Inner string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
|
2020-06-30 03:08:19 +02:00
|
|
|
type rssTitle struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
Inner string `xml:",innerxml"`
|
|
|
|
}
|
|
|
|
|
2018-04-10 05:38:12 +02:00
|
|
|
type rssEnclosure struct {
|
|
|
|
URL string `xml:"url,attr"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Length string `xml:"length,attr"`
|
|
|
|
}
|
|
|
|
|
2019-11-29 06:21:00 +01:00
|
|
|
func (enclosure *rssEnclosure) Size() int64 {
|
|
|
|
if enclosure.Length == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
size, _ := strconv.ParseInt(enclosure.Length, 10, 0)
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssItem struct {
|
2019-12-23 22:29:53 +01:00
|
|
|
GUID string `xml:"guid"`
|
2020-06-30 03:08:19 +02:00
|
|
|
Title []rssTitle `xml:"title"`
|
2019-12-23 22:29:53 +01:00
|
|
|
Links []rssLink `xml:"link"`
|
|
|
|
Description string `xml:"description"`
|
|
|
|
PubDate string `xml:"pubDate"`
|
|
|
|
Authors []rssAuthor `xml:"author"`
|
|
|
|
CommentLinks []rssCommentLink `xml:"comments"`
|
|
|
|
EnclosureLinks []rssEnclosure `xml:"enclosure"`
|
|
|
|
DublinCoreElement
|
|
|
|
FeedBurnerElement
|
|
|
|
PodcastEntryElement
|
2019-11-29 08:47:53 +01:00
|
|
|
media.Element
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) Transform() *model.Entry {
|
|
|
|
entry := new(model.Entry)
|
|
|
|
entry.URL = r.entryURL()
|
|
|
|
entry.CommentsURL = r.entryCommentsURL()
|
|
|
|
entry.Date = r.entryDate()
|
|
|
|
entry.Author = r.entryAuthor()
|
|
|
|
entry.Hash = r.entryHash()
|
|
|
|
entry.Content = r.entryContent()
|
|
|
|
entry.Title = r.entryTitle()
|
|
|
|
entry.Enclosures = r.entryEnclosures()
|
|
|
|
return entry
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-11-22 23:52:31 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryDate() time.Time {
|
2017-11-21 00:48:26 +01:00
|
|
|
value := r.PubDate
|
2019-12-23 22:29:53 +01:00
|
|
|
if r.DublinCoreDate != "" {
|
|
|
|
value = r.DublinCoreDate
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if value != "" {
|
|
|
|
result, err := date.Parse(value)
|
|
|
|
if err != nil {
|
2020-11-17 06:31:39 +01:00
|
|
|
logger.Error("rss: %v (entry GUID = %s)", err, r.GUID)
|
2017-11-20 06:10:04 +01:00
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryAuthor() string {
|
|
|
|
author := ""
|
|
|
|
|
|
|
|
for _, rssAuthor := range r.Authors {
|
|
|
|
switch rssAuthor.XMLName.Space {
|
|
|
|
case "http://www.itunes.com/dtds/podcast-1.0.dtd", "http://www.google.com/schemas/play-podcasts/1.0":
|
|
|
|
author = rssAuthor.Data
|
|
|
|
case "http://www.w3.org/2005/Atom":
|
|
|
|
if rssAuthor.Name != "" {
|
|
|
|
author = rssAuthor.Name
|
|
|
|
} else if rssAuthor.Email != "" {
|
|
|
|
author = rssAuthor.Email
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if rssAuthor.Name != "" {
|
|
|
|
author = rssAuthor.Name
|
2021-02-28 21:26:52 +01:00
|
|
|
} else if strings.Contains(rssAuthor.Inner, "<![CDATA[") {
|
|
|
|
author = rssAuthor.Data
|
2019-12-23 22:29:53 +01:00
|
|
|
} else {
|
|
|
|
author = rssAuthor.Inner
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
if author == "" {
|
|
|
|
author = r.DublinCoreCreator
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 20:33:10 +01:00
|
|
|
return sanitizer.StripTags(strings.TrimSpace(author))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryHash() string {
|
|
|
|
for _, value := range []string{r.GUID, r.entryURL()} {
|
2017-11-20 06:10:04 +01:00
|
|
|
if value != "" {
|
2018-01-03 04:15:08 +01:00
|
|
|
return crypto.Hash(value)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryTitle() string {
|
2020-06-30 03:08:19 +02:00
|
|
|
var title string
|
|
|
|
|
|
|
|
for _, rssTitle := range r.Title {
|
|
|
|
switch rssTitle.XMLName.Space {
|
|
|
|
case "http://search.yahoo.com/mrss/":
|
|
|
|
// Ignore title in media namespace
|
|
|
|
case "http://purl.org/dc/elements/1.1/":
|
|
|
|
title = rssTitle.Data
|
|
|
|
default:
|
|
|
|
title = rssTitle.Data
|
|
|
|
}
|
|
|
|
|
|
|
|
if title != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 22:26:55 +01:00
|
|
|
return html.UnescapeString(strings.TrimSpace(title))
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryContent() string {
|
|
|
|
for _, value := range []string{r.DublinCoreContent, r.Description, r.PodcastDescription()} {
|
|
|
|
if value != "" {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryURL() string {
|
|
|
|
if r.FeedBurnerLink != "" {
|
|
|
|
return r.FeedBurnerLink
|
2017-11-21 00:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range r.Links {
|
|
|
|
if link.XMLName.Space == "http://www.w3.org/2005/Atom" && link.Href != "" && isValidLinkRelation(link.Rel) {
|
2017-11-22 23:52:31 +01:00
|
|
|
return strings.TrimSpace(link.Href)
|
2017-11-21 00:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if link.Data != "" {
|
2017-11-22 23:52:31 +01:00
|
|
|
return strings.TrimSpace(link.Data)
|
2017-11-21 00:48:26 +01:00
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 00:48:26 +01:00
|
|
|
return ""
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryEnclosures() model.EnclosureList {
|
2017-11-20 06:10:04 +01:00
|
|
|
enclosures := make(model.EnclosureList, 0)
|
2021-03-23 05:04:10 +01:00
|
|
|
duplicates := make(map[string]bool)
|
2019-11-29 06:21:00 +01:00
|
|
|
|
2019-11-29 08:47:53 +01:00
|
|
|
for _, mediaThumbnail := range r.AllMediaThumbnails() {
|
2019-11-29 06:21:00 +01:00
|
|
|
if _, found := duplicates[mediaThumbnail.URL]; !found {
|
|
|
|
duplicates[mediaThumbnail.URL] = true
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: mediaThumbnail.URL,
|
2019-11-29 08:47:53 +01:00
|
|
|
MimeType: mediaThumbnail.MimeType(),
|
|
|
|
Size: mediaThumbnail.Size(),
|
2019-11-29 06:21:00 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2018-04-10 05:38:12 +02:00
|
|
|
for _, enclosure := range r.EnclosureLinks {
|
2017-11-21 00:15:10 +01:00
|
|
|
enclosureURL := enclosure.URL
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
if r.FeedBurnerEnclosureLink != "" {
|
|
|
|
filename := path.Base(r.FeedBurnerEnclosureLink)
|
2017-11-20 06:10:04 +01:00
|
|
|
if strings.Contains(enclosureURL, filename) {
|
2019-12-23 22:29:53 +01:00
|
|
|
enclosureURL = r.FeedBurnerEnclosureLink
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 06:08:11 +01:00
|
|
|
if enclosureURL == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-29 06:21:00 +01:00
|
|
|
if _, found := duplicates[enclosureURL]; !found {
|
|
|
|
duplicates[enclosureURL] = true
|
|
|
|
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: enclosureURL,
|
|
|
|
MimeType: enclosure.Type,
|
|
|
|
Size: enclosure.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:47:53 +01:00
|
|
|
for _, mediaContent := range r.AllMediaContents() {
|
2019-11-29 06:21:00 +01:00
|
|
|
if _, found := duplicates[mediaContent.URL]; !found {
|
|
|
|
duplicates[mediaContent.URL] = true
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: mediaContent.URL,
|
|
|
|
MimeType: mediaContent.MimeType(),
|
|
|
|
Size: mediaContent.Size(),
|
|
|
|
})
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-11-29 08:47:53 +01:00
|
|
|
for _, mediaPeerLink := range r.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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
return enclosures
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryCommentsURL() string {
|
2018-04-10 05:30:55 +02:00
|
|
|
for _, commentLink := range r.CommentLinks {
|
|
|
|
if commentLink.XMLName.Space == "" {
|
2020-01-05 00:18:24 +01:00
|
|
|
commentsURL := strings.TrimSpace(commentLink.Data)
|
|
|
|
// The comments URL is supposed to be absolute (some feeds publishes incorrect comments URL)
|
|
|
|
// See https://cyber.harvard.edu/rss/rss.html#ltcommentsgtSubelementOfLtitemgt
|
|
|
|
if url.IsAbsoluteURL(commentsURL) {
|
|
|
|
return commentsURL
|
|
|
|
}
|
2018-04-10 05:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:48:26 +01:00
|
|
|
func isValidLinkRelation(rel string) bool {
|
|
|
|
switch rel {
|
|
|
|
case "", "alternate", "enclosure", "related", "self", "via":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
if strings.HasPrefix(rel, "http") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|