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 atom
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2017-11-21 03:50:16 +01:00
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
"github.com/miniflux/miniflux2/helper"
|
|
|
|
"github.com/miniflux/miniflux2/model"
|
2017-11-21 04:17:04 +01:00
|
|
|
"github.com/miniflux/miniflux2/reader/date"
|
2017-11-20 06:10:04 +01:00
|
|
|
"github.com/miniflux/miniflux2/reader/processor"
|
|
|
|
"github.com/miniflux/miniflux2/reader/sanitizer"
|
|
|
|
)
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomFeed struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
|
|
|
ID string `xml:"id"`
|
|
|
|
Title string `xml:"title"`
|
2017-11-21 03:50:16 +01:00
|
|
|
Author atomAuthor `xml:"author"`
|
|
|
|
Links []atomLink `xml:"link"`
|
|
|
|
Entries []atomEntry `xml:"entry"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomEntry struct {
|
|
|
|
ID string `xml:"id"`
|
|
|
|
Title string `xml:"title"`
|
|
|
|
Updated string `xml:"updated"`
|
|
|
|
Links []atomLink `xml:"link"`
|
|
|
|
Summary string `xml:"summary"`
|
|
|
|
Content atomContent `xml:"content"`
|
|
|
|
MediaGroup atomMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
|
|
|
|
Author atomAuthor `xml:"author"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomAuthor struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
Name string `xml:"name"`
|
|
|
|
Email string `xml:"email"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomLink struct {
|
|
|
|
URL string `xml:"href,attr"`
|
2017-11-20 06:10:04 +01:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Rel string `xml:"rel,attr"`
|
|
|
|
Length string `xml:"length,attr"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomContent struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
Data string `xml:",chardata"`
|
2017-11-21 03:50:16 +01:00
|
|
|
XML string `xml:",innerxml"`
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
type atomMediaGroup struct {
|
2017-11-20 06:10:04 +01:00
|
|
|
Description string `xml:"http://search.yahoo.com/mrss/ description"`
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func (a *atomFeed) Transform() *model.Feed {
|
2017-11-20 06:10:04 +01:00
|
|
|
feed := new(model.Feed)
|
2017-11-21 03:50:16 +01:00
|
|
|
feed.FeedURL = getRelationURL(a.Links, "self")
|
|
|
|
feed.SiteURL = getURL(a.Links)
|
2017-11-20 06:10:04 +01:00
|
|
|
feed.Title = sanitizer.StripTags(a.Title)
|
|
|
|
|
|
|
|
if feed.Title == "" {
|
|
|
|
feed.Title = feed.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range a.Entries {
|
|
|
|
item := entry.Transform()
|
|
|
|
if item.Author == "" {
|
2017-11-21 03:50:16 +01:00
|
|
|
item.Author = getAuthor(a.Author)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
feed.Entries = append(feed.Entries, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func (a *atomEntry) Transform() *model.Entry {
|
|
|
|
entry := new(model.Entry)
|
|
|
|
entry.URL = getURL(a.Links)
|
|
|
|
entry.Date = getDate(a)
|
|
|
|
entry.Author = sanitizer.StripTags(getAuthor(a.Author))
|
|
|
|
entry.Hash = getHash(a)
|
|
|
|
entry.Content = processor.ItemContentProcessor(entry.URL, getContent(a))
|
|
|
|
entry.Title = sanitizer.StripTags(strings.Trim(a.Title, " \n\t"))
|
|
|
|
entry.Enclosures = getEnclosures(a)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
if entry.Title == "" {
|
|
|
|
entry.Title = entry.URL
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
return entry
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getURL(links []atomLink) string {
|
|
|
|
for _, link := range links {
|
2017-11-20 06:10:04 +01:00
|
|
|
if strings.ToLower(link.Rel) == "alternate" {
|
2017-11-21 03:50:16 +01:00
|
|
|
return link.URL
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if link.Rel == "" && link.Type == "" {
|
2017-11-21 03:50:16 +01:00
|
|
|
return link.URL
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getRelationURL(links []atomLink, relation string) string {
|
|
|
|
for _, link := range links {
|
|
|
|
if strings.ToLower(link.Rel) == relation {
|
|
|
|
return link.URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getDate(a *atomEntry) time.Time {
|
|
|
|
if a.Updated != "" {
|
|
|
|
result, err := date.Parse(a.Updated)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return time.Now()
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
2017-11-21 03:50:16 +01:00
|
|
|
|
|
|
|
return result
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
return time.Now()
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getContent(a *atomEntry) string {
|
|
|
|
if a.Content.Type == "html" || a.Content.Type == "text" {
|
|
|
|
return a.Content.Data
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
if a.Content.Type == "xhtml" {
|
|
|
|
return a.Content.XML
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
if a.Summary != "" {
|
|
|
|
return a.Summary
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
if a.MediaGroup.Description != "" {
|
|
|
|
return a.MediaGroup.Description
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getHash(a *atomEntry) string {
|
|
|
|
for _, value := range []string{a.ID, getURL(a.Links)} {
|
|
|
|
if value != "" {
|
|
|
|
return helper.Hash(value)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
return ""
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getEnclosures(a *atomEntry) model.EnclosureList {
|
|
|
|
enclosures := make(model.EnclosureList, 0)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
for _, link := range a.Links {
|
|
|
|
if strings.ToLower(link.Rel) == "enclosure" {
|
|
|
|
length, _ := strconv.Atoi(link.Length)
|
|
|
|
enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
|
|
|
|
}
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
return enclosures
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:50:16 +01:00
|
|
|
func getAuthor(author atomAuthor) string {
|
2017-11-20 06:10:04 +01:00
|
|
|
if author.Name != "" {
|
|
|
|
return author.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if author.Email != "" {
|
|
|
|
return author.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|