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.
|
|
|
|
|
|
|
|
package rss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
"github.com/miniflux/miniflux2/helper"
|
|
|
|
"github.com/miniflux/miniflux2/model"
|
|
|
|
"github.com/miniflux/miniflux2/reader/feed/date"
|
|
|
|
"github.com/miniflux/miniflux2/reader/processor"
|
|
|
|
"github.com/miniflux/miniflux2/reader/sanitizer"
|
|
|
|
)
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rssItem struct {
|
|
|
|
GUID string `xml:"guid"`
|
2017-11-20 06:10:04 +01:00
|
|
|
Title string `xml:"title"`
|
|
|
|
Link string `xml:"link"`
|
|
|
|
OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
|
|
|
|
Description string `xml:"description"`
|
|
|
|
Content 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"`
|
2017-11-21 00:15:10 +01:00
|
|
|
Authors []rssAuthor `xml:"author"`
|
2017-11-20 06:10:04 +01:00
|
|
|
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
|
2017-11-21 00:15:10 +01:00
|
|
|
Enclosures []rssEnclosure `xml:"enclosure"`
|
2017-11-20 06:10:04 +01:00
|
|
|
OrigEnclosureLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origEnclosureLink"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssAuthor struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
XMLName xml.Name
|
|
|
|
Data string `xml:",chardata"`
|
|
|
|
Name string `xml:"name"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
type rssEnclosure struct {
|
|
|
|
URL string `xml:"url,attr"`
|
2017-11-20 06:10:04 +01:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Length string `xml:"length,attr"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (r *rssFeed) GetSiteURL() string {
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, elem := range r.Links {
|
|
|
|
if elem.XMLName.Space == "" {
|
|
|
|
return elem.Data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (r *rssFeed) GetFeedURL() string {
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, elem := range r.Links {
|
|
|
|
if elem.XMLName.Space == "http://www.w3.org/2005/Atom" {
|
|
|
|
return elem.Href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
feed.SiteURL = r.GetSiteURL()
|
|
|
|
feed.FeedURL = r.GetFeedURL()
|
|
|
|
feed.Title = sanitizer.StripTags(r.Title)
|
|
|
|
|
|
|
|
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 = sanitizer.StripTags(entry.Author)
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
if entry.URL == "" {
|
|
|
|
entry.URL = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
feed.Entries = append(feed.Entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetDate() time.Time {
|
2017-11-20 06:10:04 +01:00
|
|
|
value := i.PubDate
|
|
|
|
if i.Date != "" {
|
|
|
|
value = i.Date
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != "" {
|
|
|
|
result, err := date.Parse(value)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetAuthor() string {
|
2017-11-20 06:10:04 +01:00
|
|
|
for _, element := range i.Authors {
|
|
|
|
if element.Name != "" {
|
|
|
|
return element.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.Data != "" {
|
|
|
|
return element.Data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Creator
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetHash() string {
|
|
|
|
for _, value := range []string{i.GUID, i.Link} {
|
2017-11-20 06:10:04 +01:00
|
|
|
if value != "" {
|
|
|
|
return helper.Hash(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetContent() string {
|
2017-11-20 06:10:04 +01:00
|
|
|
if i.Content != "" {
|
|
|
|
return i.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Description
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetURL() string {
|
2017-11-20 06:10:04 +01:00
|
|
|
if i.OriginalLink != "" {
|
|
|
|
return i.OriginalLink
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.Link
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) GetEnclosures() model.EnclosureList {
|
2017-11-20 06:10:04 +01:00
|
|
|
enclosures := make(model.EnclosureList, 0)
|
|
|
|
|
|
|
|
for _, enclosure := range i.Enclosures {
|
|
|
|
length, _ := strconv.Atoi(enclosure.Length)
|
2017-11-21 00:15:10 +01:00
|
|
|
enclosureURL := enclosure.URL
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
if i.OrigEnclosureLink != "" {
|
|
|
|
filename := path.Base(i.OrigEnclosureLink)
|
|
|
|
if strings.Contains(enclosureURL, filename) {
|
|
|
|
enclosureURL = i.OrigEnclosureLink
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{
|
|
|
|
URL: enclosureURL,
|
|
|
|
MimeType: enclosure.Type,
|
|
|
|
Size: length,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return enclosures
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:15:10 +01:00
|
|
|
func (i *rssItem) Transform() *model.Entry {
|
2017-11-20 06:10:04 +01:00
|
|
|
entry := new(model.Entry)
|
|
|
|
entry.URL = i.GetURL()
|
|
|
|
entry.Date = i.GetDate()
|
|
|
|
entry.Author = i.GetAuthor()
|
|
|
|
entry.Hash = i.GetHash()
|
|
|
|
entry.Content = processor.ItemContentProcessor(entry.URL, i.GetContent())
|
|
|
|
entry.Title = sanitizer.StripTags(strings.Trim(i.Title, " \n\t"))
|
|
|
|
entry.Enclosures = i.GetEnclosures()
|
|
|
|
|
|
|
|
if entry.Title == "" {
|
|
|
|
entry.Title = entry.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry
|
|
|
|
}
|