miniflux/reader/rss/rss.go

342 lines
8 KiB
Go
Raw Normal View History

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"
"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"
"miniflux.app/reader/sanitizer"
"miniflux.app/url"
2017-11-21 00:15:10 +01:00
)
2017-11-20 06:10:04 +01:00
2017-11-21 00:15:10 +01:00
type rssFeed struct {
2017-11-20 06:10:04 +01:00
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Title string `xml:"channel>title"`
2017-11-21 00:15:10 +01:00
Links []rssLink `xml:"channel>link"`
2017-11-20 06:10:04 +01:00
Language string `xml:"channel>language"`
Description string `xml:"channel>description"`
PubDate string `xml:"channel>pubDate"`
ItunesAuthor string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>author"`
2017-11-21 00:15:10 +01:00
Items []rssItem `xml:"channel>item"`
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
}
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"`
Inner string `xml:",innerxml"`
}
type rssEnclosure struct {
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
Length string `xml:"length,attr"`
}
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 {
GUID string `xml:"guid"`
Title string `xml:"title"`
Links []rssLink `xml:"link"`
OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
CommentLinks []rssCommentLink `xml:"comments"`
Description string `xml:"description"`
EncodedContent string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
PubDate string `xml:"pubDate"`
Date string `xml:"http://purl.org/dc/elements/1.1/ date"`
Authors []rssAuthor `xml:"author"`
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
EnclosureLinks []rssEnclosure `xml:"enclosure"`
OrigEnclosureLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origEnclosureLink"`
MediaGroup []rssMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
MediaContents []rssMediaContent `xml:"http://search.yahoo.com/mrss/ content"`
MediaThumbnails []rssMediaThumbnails `xml:"http://search.yahoo.com/mrss/ thumbnail"`
}
type rssMediaGroup struct {
MediaList []rssMediaContent `xml:"content"`
}
type rssMediaContent struct {
URL string `xml:"url,attr"`
Type string `xml:"type,attr"`
FileSize string `xml:"fileSize,attr"`
Medium string `xml:"medium,attr"`
}
func (mediaContent *rssMediaContent) MimeType() string {
switch {
case mediaContent.Type == "" && mediaContent.Medium == "image":
return "image/*"
case mediaContent.Type == "" && mediaContent.Medium == "video":
return "video/*"
case mediaContent.Type == "" && mediaContent.Medium == "audio":
return "audio/*"
case mediaContent.Type == "" && mediaContent.Medium == "video":
return "video/*"
case mediaContent.Type != "":
return mediaContent.Type
default:
return "application/octet-stream"
}
}
func (mediaContent *rssMediaContent) Size() int64 {
if mediaContent.FileSize == "" {
return 0
}
size, _ := strconv.ParseInt(mediaContent.FileSize, 10, 0)
return size
}
type rssMediaThumbnails struct {
URL string `xml:"url,attr"`
2017-11-20 06:10:04 +01:00
}
2018-04-10 05:38:12 +02:00
func (r *rssFeed) SiteURL() string {
2017-11-21 00:48:26 +01:00
for _, element := range r.Links {
if element.XMLName.Space == "" {
2017-11-22 23:52:31 +01:00
return strings.TrimSpace(element.Data)
2017-11-20 06:10:04 +01:00
}
}
return ""
}
2018-04-10 05:38:12 +02:00
func (r *rssFeed) FeedURL() string {
2017-11-21 00:48:26 +01:00
for _, element := range r.Links {
if element.XMLName.Space == "http://www.w3.org/2005/Atom" {
2017-11-22 23:52:31 +01:00
return strings.TrimSpace(element.Href)
2017-11-20 06:10:04 +01:00
}
}
return ""
}
2017-11-21 00:15:10 +01:00
func (r *rssFeed) Transform() *model.Feed {
2017-11-20 06:10:04 +01:00
feed := new(model.Feed)
2018-04-10 05:38:12 +02:00
feed.SiteURL = r.SiteURL()
feed.FeedURL = r.FeedURL()
2017-11-22 23:52:31 +01:00
feed.Title = strings.TrimSpace(r.Title)
2017-11-20 06:10:04 +01:00
if feed.Title == "" {
feed.Title = feed.SiteURL
}
for _, item := range r.Items {
entry := item.Transform()
if entry.Author == "" && r.ItunesAuthor != "" {
entry.Author = r.ItunesAuthor
}
entry.Author = strings.TrimSpace(sanitizer.StripTags(entry.Author))
2017-11-20 06:10:04 +01:00
2017-11-21 00:15:10 +01:00
if entry.URL == "" {
entry.URL = feed.SiteURL
2017-12-14 05:16:15 +01:00
} else {
entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
if err == nil {
entry.URL = entryURL
}
}
if entry.Title == "" {
entry.Title = entry.URL
2017-11-21 00:15:10 +01:00
}
2017-11-20 06:10:04 +01:00
feed.Entries = append(feed.Entries, entry)
}
return feed
}
2017-11-22 23:52:31 +01:00
2018-04-10 05:38:12 +02:00
func (r *rssItem) PublishedDate() time.Time {
2017-11-21 00:48:26 +01:00
value := r.PubDate
if r.Date != "" {
value = r.Date
2017-11-20 06:10:04 +01:00
}
if value != "" {
result, err := date.Parse(value)
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("rss: %v", err)
2017-11-20 06:10:04 +01:00
return time.Now()
}
return result
}
return time.Now()
}
2018-04-10 05:38:12 +02:00
func (r *rssItem) Author() string {
2017-11-21 00:48:26 +01:00
for _, element := range r.Authors {
2017-11-20 06:10:04 +01:00
if element.Name != "" {
return element.Name
}
if element.Inner != "" {
return element.Inner
2017-11-20 06:10:04 +01:00
}
}
2017-11-21 00:48:26 +01:00
return r.Creator
2017-11-20 06:10:04 +01:00
}
2018-04-10 05:38:12 +02:00
func (r *rssItem) Hash() string {
for _, value := range []string{r.GUID, r.URL()} {
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 ""
}
2018-04-10 05:38:12 +02:00
func (r *rssItem) Content() string {
if r.EncodedContent != "" {
return r.EncodedContent
2017-11-20 06:10:04 +01:00
}
2017-11-21 00:48:26 +01:00
return r.Description
2017-11-20 06:10:04 +01:00
}
2018-04-10 05:38:12 +02:00
func (r *rssItem) URL() string {
2017-11-21 00:48:26 +01:00
if r.OriginalLink != "" {
return r.OriginalLink
}
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
}
2018-04-10 05:38:12 +02:00
func (r *rssItem) Enclosures() model.EnclosureList {
2017-11-20 06:10:04 +01:00
enclosures := make(model.EnclosureList, 0)
duplicates := make(map[string]bool, 0)
for _, mediaThumbnail := range r.MediaThumbnails {
if _, found := duplicates[mediaThumbnail.URL]; !found {
duplicates[mediaThumbnail.URL] = true
enclosures = append(enclosures, &model.Enclosure{
URL: mediaThumbnail.URL,
MimeType: "image/*",
Size: 0,
})
}
}
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
2017-11-21 00:48:26 +01:00
if r.OrigEnclosureLink != "" {
filename := path.Base(r.OrigEnclosureLink)
2017-11-20 06:10:04 +01:00
if strings.Contains(enclosureURL, filename) {
2017-11-21 00:48:26 +01:00
enclosureURL = r.OrigEnclosureLink
2017-11-20 06:10:04 +01:00
}
}
if _, found := duplicates[enclosureURL]; !found {
duplicates[enclosureURL] = true
enclosures = append(enclosures, &model.Enclosure{
URL: enclosureURL,
MimeType: enclosure.Type,
Size: enclosure.Size(),
})
}
}
for _, mediaContentItem := range r.MediaGroup {
for _, mediaContent := range mediaContentItem.MediaList {
r.MediaContents = append(r.MediaContents, mediaContent)
}
}
for _, mediaContent := range r.MediaContents {
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
}
return enclosures
}
func (r *rssItem) CommentsURL() string {
for _, commentLink := range r.CommentLinks {
if commentLink.XMLName.Space == "" {
return strings.TrimSpace(commentLink.Data)
}
}
return ""
}
2017-11-21 00:48:26 +01:00
func (r *rssItem) Transform() *model.Entry {
2017-11-20 06:10:04 +01:00
entry := new(model.Entry)
2018-04-10 05:38:12 +02:00
entry.URL = r.URL()
entry.CommentsURL = r.CommentsURL()
2018-04-10 05:38:12 +02:00
entry.Date = r.PublishedDate()
entry.Author = r.Author()
entry.Hash = r.Hash()
entry.Content = r.Content()
2017-11-22 23:52:31 +01:00
entry.Title = strings.TrimSpace(r.Title)
2018-04-10 05:38:12 +02:00
entry.Enclosures = r.Enclosures()
2017-11-20 06:10:04 +01:00
return entry
}
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
}
}