2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package rss // import "miniflux.app/v2/internal/reader/rss"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-02-13 22:26:55 +01:00
|
|
|
"html"
|
2023-09-25 01:32:09 +02:00
|
|
|
"log/slog"
|
2017-11-20 06:10:04 +01:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
"miniflux.app/v2/internal/reader/date"
|
2023-09-09 01:50:06 +02:00
|
|
|
"miniflux.app/v2/internal/reader/dublincore"
|
2024-03-12 05:43:27 +01:00
|
|
|
"miniflux.app/v2/internal/reader/googleplay"
|
|
|
|
"miniflux.app/v2/internal/reader/itunes"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/reader/media"
|
|
|
|
"miniflux.app/v2/internal/reader/sanitizer"
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2017-11-21 00:15:10 +01:00
|
|
|
)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
// Specs: https://www.rssboard.org/rss-specification
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssFeed struct {
|
2024-03-12 04:43:14 +01:00
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"rss version,attr"`
|
|
|
|
Channel rssChannel `xml:"rss channel"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rssChannel struct {
|
2024-03-12 06:10:47 +01:00
|
|
|
Categories []string `xml:"rss category"`
|
2024-03-12 04:43:14 +01:00
|
|
|
Title string `xml:"rss title"`
|
|
|
|
Link string `xml:"rss link"`
|
|
|
|
ImageURL string `xml:"rss image>url"`
|
|
|
|
Language string `xml:"rss language"`
|
|
|
|
Description string `xml:"rss description"`
|
|
|
|
PubDate string `xml:"rss pubDate"`
|
|
|
|
ManagingEditor string `xml:"rss managingEditor"`
|
|
|
|
Webmaster string `xml:"rss webMaster"`
|
|
|
|
TimeToLive rssTTL `xml:"rss ttl"`
|
|
|
|
Items []rssItem `xml:"rss item"`
|
|
|
|
AtomLinks
|
2024-03-12 05:43:27 +01:00
|
|
|
itunes.ItunesFeedElement
|
|
|
|
googleplay.GooglePlayFeedElement
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
|
|
|
|
2023-10-21 04:39:32 +02:00
|
|
|
type rssTTL struct {
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rssTTL) Value() int {
|
|
|
|
if r.Data == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := strconv.Atoi(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
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()
|
2023-08-14 04:09:01 +02:00
|
|
|
feed.SiteURL, err = urllib.AbsoluteURL(baseURL, siteURL)
|
2020-12-03 05:47:11 +01:00
|
|
|
if err != nil {
|
|
|
|
feed.SiteURL = siteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
feedURL := r.feedURL()
|
2023-08-14 04:09:01 +02:00
|
|
|
feed.FeedURL, err = urllib.AbsoluteURL(baseURL, feedURL)
|
2020-12-03 05:47:11 +01:00
|
|
|
if err != nil {
|
|
|
|
feed.FeedURL = feedURL
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
feed.Title = html.UnescapeString(strings.TrimSpace(r.Channel.Title))
|
2019-12-23 22:29:53 +01:00
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
feed.IconURL = strings.TrimSpace(r.Channel.ImageURL)
|
|
|
|
feed.TTL = r.Channel.TimeToLive.Value()
|
2023-06-05 00:01:59 +02:00
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
for _, item := range r.Channel.Items {
|
2019-12-23 22:29:53 +01:00
|
|
|
entry := item.Transform()
|
|
|
|
if entry.Author == "" {
|
|
|
|
entry.Author = r.feedAuthor()
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.URL == "" {
|
|
|
|
entry.URL = feed.SiteURL
|
|
|
|
} else {
|
2023-08-14 04:09:01 +02:00
|
|
|
entryURL, err := urllib.AbsoluteURL(feed.SiteURL, entry.URL)
|
2019-12-23 22:29:53 +01:00
|
|
|
if err == nil {
|
|
|
|
entry.URL = entryURL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 01:49:44 +01:00
|
|
|
if entry.Title == "" {
|
|
|
|
entry.Title = sanitizer.TruncateHTML(entry.Content, 100)
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
if entry.Title == "" {
|
|
|
|
entry.Title = entry.URL
|
|
|
|
}
|
|
|
|
|
2024-03-12 06:10:47 +01:00
|
|
|
entry.Tags = append(entry.Tags, r.Channel.Categories...)
|
|
|
|
entry.Tags = append(entry.Tags, r.Channel.GetItunesCategories()...)
|
|
|
|
|
|
|
|
if r.Channel.GooglePlayCategory.Text != "" {
|
|
|
|
entry.Tags = append(entry.Tags, r.Channel.GooglePlayCategory.Text)
|
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
feed.Entries = append(feed.Entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rssFeed) siteURL() string {
|
2024-03-12 04:43:14 +01:00
|
|
|
return strings.TrimSpace(r.Channel.Link)
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rssFeed) feedURL() string {
|
2024-03-12 04:43:14 +01:00
|
|
|
for _, atomLink := range r.Channel.AtomLinks.Links {
|
|
|
|
if atomLink.Rel == "self" {
|
|
|
|
return strings.TrimSpace(atomLink.URL)
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r rssFeed) feedAuthor() string {
|
2024-03-12 05:43:27 +01:00
|
|
|
var author string
|
2019-12-23 22:29:53 +01:00
|
|
|
switch {
|
2024-03-12 05:43:27 +01:00
|
|
|
case r.Channel.ItunesAuthor != "":
|
|
|
|
author = r.Channel.ItunesAuthor
|
|
|
|
case r.Channel.GooglePlayAuthor != "":
|
|
|
|
author = r.Channel.GooglePlayAuthor
|
|
|
|
case r.Channel.ItunesOwner.String() != "":
|
|
|
|
author = r.Channel.ItunesOwner.String()
|
2024-03-12 04:43:14 +01:00
|
|
|
case r.Channel.ManagingEditor != "":
|
|
|
|
author = r.Channel.ManagingEditor
|
|
|
|
case r.Channel.Webmaster != "":
|
|
|
|
author = r.Channel.Webmaster
|
2019-12-23 22:29:53 +01:00
|
|
|
}
|
2021-01-03 20:33:10 +01:00
|
|
|
return sanitizer.StripTags(strings.TrimSpace(author))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2022-10-10 01:51:39 +02:00
|
|
|
type rssGUID struct {
|
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
IsPermaLink string `xml:"isPermaLink,attr"`
|
|
|
|
}
|
|
|
|
|
2018-04-10 05:38:12 +02:00
|
|
|
type rssAuthor struct {
|
2020-06-30 03:08:19 +02:00
|
|
|
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 {
|
2024-03-12 04:43:14 +01:00
|
|
|
GUID rssGUID `xml:"rss guid"`
|
|
|
|
Title string `xml:"rss title"`
|
|
|
|
Link string `xml:"rss link"`
|
|
|
|
Description string `xml:"rss description"`
|
|
|
|
PubDate string `xml:"rss pubDate"`
|
|
|
|
Author rssAuthor `xml:"rss author"`
|
|
|
|
Comments string `xml:"rss comments"`
|
|
|
|
EnclosureLinks []rssEnclosure `xml:"rss enclosure"`
|
2024-03-12 06:10:47 +01:00
|
|
|
Categories []string `xml:"rss category"`
|
2023-09-09 01:50:06 +02:00
|
|
|
dublincore.DublinCoreItemElement
|
2019-12-23 22:29:53 +01:00
|
|
|
FeedBurnerElement
|
2019-11-29 08:47:53 +01:00
|
|
|
media.Element
|
2024-03-12 04:43:14 +01:00
|
|
|
AtomAuthor
|
|
|
|
AtomLinks
|
2024-03-12 05:43:27 +01:00
|
|
|
itunes.ItunesItemElement
|
|
|
|
googleplay.GooglePlayItemElement
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) Transform() *model.Entry {
|
2023-09-09 07:45:17 +02:00
|
|
|
entry := model.NewEntry()
|
2019-12-23 22:29:53 +01:00
|
|
|
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()
|
2024-03-12 06:10:47 +01:00
|
|
|
entry.Tags = r.Categories
|
2024-03-12 05:43:27 +01:00
|
|
|
if duration, err := normalizeDuration(r.ItunesDuration); err == nil {
|
2023-08-12 12:01:15 +02:00
|
|
|
entry.ReadingTime = duration
|
|
|
|
}
|
2023-02-25 05:52:45 +01:00
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
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 {
|
2023-11-01 20:26:16 +01:00
|
|
|
slog.Debug("Unable to parse date from RSS feed",
|
2023-09-25 01:32:09 +02:00
|
|
|
slog.String("date", value),
|
|
|
|
slog.String("guid", r.GUID.Data),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
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 {
|
2024-03-12 04:43:14 +01:00
|
|
|
var author string
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
switch {
|
|
|
|
case r.GooglePlayAuthor != "":
|
|
|
|
author = r.GooglePlayAuthor
|
|
|
|
case r.ItunesAuthor != "":
|
|
|
|
author = r.ItunesAuthor
|
|
|
|
case r.DublinCoreCreator != "":
|
|
|
|
author = r.DublinCoreCreator
|
|
|
|
case r.AtomAuthor.String() != "":
|
|
|
|
author = r.AtomAuthor.String()
|
|
|
|
case strings.Contains(r.Author.Inner, "<![CDATA["):
|
|
|
|
author = r.Author.Data
|
|
|
|
default:
|
|
|
|
author = r.Author.Inner
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
return strings.TrimSpace(sanitizer.StripTags(author))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 22:29:53 +01:00
|
|
|
func (r *rssItem) entryHash() string {
|
2022-10-10 01:51:39 +02:00
|
|
|
for _, value := range []string{r.GUID.Data, 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 {
|
2024-03-12 04:43:14 +01:00
|
|
|
title := r.Title
|
2020-06-30 03:08:19 +02:00
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
if r.DublinCoreTitle != "" {
|
|
|
|
title = r.DublinCoreTitle
|
2020-06-30 03:08:19 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-03-12 05:43:27 +01:00
|
|
|
for _, value := range []string{
|
|
|
|
r.DublinCoreContent,
|
|
|
|
r.Description,
|
|
|
|
r.GooglePlayDescription,
|
|
|
|
r.ItunesSummary,
|
|
|
|
r.ItunesSubtitle,
|
|
|
|
} {
|
2019-12-23 22:29:53 +01:00
|
|
|
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 {
|
2024-03-12 04:43:14 +01:00
|
|
|
for _, link := range []string{r.FeedBurnerLink, r.Link} {
|
|
|
|
if link != "" {
|
|
|
|
return strings.TrimSpace(link)
|
2017-11-21 00:48:26 +01:00
|
|
|
}
|
2024-03-12 04:43:14 +01:00
|
|
|
}
|
2017-11-21 00:48:26 +01:00
|
|
|
|
2024-03-12 04:43:14 +01:00
|
|
|
for _, atomLink := range r.AtomLinks.Links {
|
|
|
|
if atomLink.URL != "" && (strings.EqualFold(atomLink.Rel, "alternate") || atomLink.Rel == "") {
|
|
|
|
return strings.TrimSpace(atomLink.URL)
|
2017-11-21 00:48:26 +01:00
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2022-10-10 01:51:39 +02:00
|
|
|
// Specs: https://cyber.harvard.edu/rss/rss.html#ltguidgtSubelementOfLtitemgt
|
|
|
|
// isPermaLink is optional, its default value is true.
|
|
|
|
// If its value is false, the guid may not be assumed to be a url, or a url to anything in particular.
|
|
|
|
if r.GUID.IsPermaLink == "true" || r.GUID.IsPermaLink == "" {
|
|
|
|
return strings.TrimSpace(r.GUID.Data)
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-03-12 04:43:14 +01:00
|
|
|
commentsURL := strings.TrimSpace(r.Comments)
|
|
|
|
if commentsURL != "" && urllib.IsAbsoluteURL(commentsURL) {
|
|
|
|
return commentsURL
|
2018-04-10 05:30:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|