mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
Use more idiomatic code for Atom parser
This commit is contained in:
parent
89307010ad
commit
6618caca81
2 changed files with 91 additions and 112 deletions
|
@ -6,87 +6,64 @@ package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/miniflux/miniflux2/helper"
|
"github.com/miniflux/miniflux2/helper"
|
||||||
"github.com/miniflux/miniflux2/model"
|
"github.com/miniflux/miniflux2/model"
|
||||||
"github.com/miniflux/miniflux2/reader/feed/date"
|
"github.com/miniflux/miniflux2/reader/feed/date"
|
||||||
"github.com/miniflux/miniflux2/reader/processor"
|
"github.com/miniflux/miniflux2/reader/processor"
|
||||||
"github.com/miniflux/miniflux2/reader/sanitizer"
|
"github.com/miniflux/miniflux2/reader/sanitizer"
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AtomFeed struct {
|
type atomFeed struct {
|
||||||
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
||||||
ID string `xml:"id"`
|
ID string `xml:"id"`
|
||||||
Title string `xml:"title"`
|
Title string `xml:"title"`
|
||||||
Author Author `xml:"author"`
|
Author atomAuthor `xml:"author"`
|
||||||
Links []Link `xml:"link"`
|
Links []atomLink `xml:"link"`
|
||||||
Entries []AtomEntry `xml:"entry"`
|
Entries []atomEntry `xml:"entry"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AtomEntry struct {
|
type atomEntry struct {
|
||||||
ID string `xml:"id"`
|
ID string `xml:"id"`
|
||||||
Title string `xml:"title"`
|
Title string `xml:"title"`
|
||||||
Updated string `xml:"updated"`
|
Updated string `xml:"updated"`
|
||||||
Links []Link `xml:"link"`
|
Links []atomLink `xml:"link"`
|
||||||
Summary string `xml:"summary"`
|
Summary string `xml:"summary"`
|
||||||
Content Content `xml:"content"`
|
Content atomContent `xml:"content"`
|
||||||
MediaGroup MediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
|
MediaGroup atomMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
|
||||||
Author Author `xml:"author"`
|
Author atomAuthor `xml:"author"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author struct {
|
type atomAuthor struct {
|
||||||
Name string `xml:"name"`
|
Name string `xml:"name"`
|
||||||
Email string `xml:"email"`
|
Email string `xml:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Link struct {
|
type atomLink struct {
|
||||||
Url string `xml:"href,attr"`
|
URL string `xml:"href,attr"`
|
||||||
Type string `xml:"type,attr"`
|
Type string `xml:"type,attr"`
|
||||||
Rel string `xml:"rel,attr"`
|
Rel string `xml:"rel,attr"`
|
||||||
Length string `xml:"length,attr"`
|
Length string `xml:"length,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Content struct {
|
type atomContent struct {
|
||||||
Type string `xml:"type,attr"`
|
Type string `xml:"type,attr"`
|
||||||
Data string `xml:",chardata"`
|
Data string `xml:",chardata"`
|
||||||
Xml string `xml:",innerxml"`
|
XML string `xml:",innerxml"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MediaGroup struct {
|
type atomMediaGroup struct {
|
||||||
Description string `xml:"http://search.yahoo.com/mrss/ description"`
|
Description string `xml:"http://search.yahoo.com/mrss/ description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AtomFeed) getSiteURL() string {
|
func (a *atomFeed) Transform() *model.Feed {
|
||||||
for _, link := range a.Links {
|
|
||||||
if strings.ToLower(link.Rel) == "alternate" {
|
|
||||||
return link.Url
|
|
||||||
}
|
|
||||||
|
|
||||||
if link.Rel == "" && link.Type == "" {
|
|
||||||
return link.Url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AtomFeed) getFeedURL() string {
|
|
||||||
for _, link := range a.Links {
|
|
||||||
if strings.ToLower(link.Rel) == "self" {
|
|
||||||
return link.Url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AtomFeed) Transform() *model.Feed {
|
|
||||||
feed := new(model.Feed)
|
feed := new(model.Feed)
|
||||||
feed.FeedURL = a.getFeedURL()
|
feed.FeedURL = getRelationURL(a.Links, "self")
|
||||||
feed.SiteURL = a.getSiteURL()
|
feed.SiteURL = getURL(a.Links)
|
||||||
feed.Title = sanitizer.StripTags(a.Title)
|
feed.Title = sanitizer.StripTags(a.Title)
|
||||||
|
|
||||||
if feed.Title == "" {
|
if feed.Title == "" {
|
||||||
|
@ -96,7 +73,7 @@ func (a *AtomFeed) Transform() *model.Feed {
|
||||||
for _, entry := range a.Entries {
|
for _, entry := range a.Entries {
|
||||||
item := entry.Transform()
|
item := entry.Transform()
|
||||||
if item.Author == "" {
|
if item.Author == "" {
|
||||||
item.Author = a.GetAuthor()
|
item.Author = getAuthor(a.Author)
|
||||||
}
|
}
|
||||||
|
|
||||||
feed.Entries = append(feed.Entries, item)
|
feed.Entries = append(feed.Entries, item)
|
||||||
|
@ -105,13 +82,50 @@ func (a *AtomFeed) Transform() *model.Feed {
|
||||||
return feed
|
return feed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AtomFeed) GetAuthor() string {
|
func (a *atomEntry) Transform() *model.Entry {
|
||||||
return getAuthor(a.Author)
|
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)
|
||||||
|
|
||||||
|
if entry.Title == "" {
|
||||||
|
entry.Title = entry.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AtomEntry) GetDate() time.Time {
|
return entry
|
||||||
if e.Updated != "" {
|
}
|
||||||
result, err := date.Parse(e.Updated)
|
|
||||||
|
func getURL(links []atomLink) string {
|
||||||
|
for _, link := range links {
|
||||||
|
if strings.ToLower(link.Rel) == "alternate" {
|
||||||
|
return link.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
if link.Rel == "" && link.Type == "" {
|
||||||
|
return link.URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRelationURL(links []atomLink, relation string) string {
|
||||||
|
for _, link := range links {
|
||||||
|
if strings.ToLower(link.Rel) == relation {
|
||||||
|
return link.URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDate(a *atomEntry) time.Time {
|
||||||
|
if a.Updated != "" {
|
||||||
|
result, err := date.Parse(a.Updated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return time.Now()
|
return time.Now()
|
||||||
|
@ -123,26 +137,28 @@ func (e *AtomEntry) GetDate() time.Time {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AtomEntry) GetURL() string {
|
func getContent(a *atomEntry) string {
|
||||||
for _, link := range e.Links {
|
if a.Content.Type == "html" || a.Content.Type == "text" {
|
||||||
if strings.ToLower(link.Rel) == "alternate" {
|
return a.Content.Data
|
||||||
return link.Url
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if link.Rel == "" && link.Type == "" {
|
if a.Content.Type == "xhtml" {
|
||||||
return link.Url
|
return a.Content.XML
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if a.Summary != "" {
|
||||||
|
return a.Summary
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.MediaGroup.Description != "" {
|
||||||
|
return a.MediaGroup.Description
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AtomEntry) GetAuthor() string {
|
func getHash(a *atomEntry) string {
|
||||||
return getAuthor(e.Author)
|
for _, value := range []string{a.ID, getURL(a.Links)} {
|
||||||
}
|
|
||||||
|
|
||||||
func (e *AtomEntry) GetHash() string {
|
|
||||||
for _, value := range []string{e.ID, e.GetURL()} {
|
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return helper.Hash(value)
|
return helper.Hash(value)
|
||||||
}
|
}
|
||||||
|
@ -151,57 +167,20 @@ func (e *AtomEntry) GetHash() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AtomEntry) GetContent() string {
|
func getEnclosures(a *atomEntry) model.EnclosureList {
|
||||||
if e.Content.Type == "html" || e.Content.Type == "text" {
|
|
||||||
return e.Content.Data
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Content.Type == "xhtml" {
|
|
||||||
return e.Content.Xml
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Summary != "" {
|
|
||||||
return e.Summary
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.MediaGroup.Description != "" {
|
|
||||||
return e.MediaGroup.Description
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *AtomEntry) GetEnclosures() model.EnclosureList {
|
|
||||||
enclosures := make(model.EnclosureList, 0)
|
enclosures := make(model.EnclosureList, 0)
|
||||||
|
|
||||||
for _, link := range e.Links {
|
for _, link := range a.Links {
|
||||||
if strings.ToLower(link.Rel) == "enclosure" {
|
if strings.ToLower(link.Rel) == "enclosure" {
|
||||||
length, _ := strconv.Atoi(link.Length)
|
length, _ := strconv.Atoi(link.Length)
|
||||||
enclosures = append(enclosures, &model.Enclosure{URL: link.Url, MimeType: link.Type, Size: length})
|
enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return enclosures
|
return enclosures
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AtomEntry) Transform() *model.Entry {
|
func getAuthor(author atomAuthor) string {
|
||||||
entry := new(model.Entry)
|
|
||||||
entry.URL = e.GetURL()
|
|
||||||
entry.Date = e.GetDate()
|
|
||||||
entry.Author = sanitizer.StripTags(e.GetAuthor())
|
|
||||||
entry.Hash = e.GetHash()
|
|
||||||
entry.Content = processor.ItemContentProcessor(entry.URL, e.GetContent())
|
|
||||||
entry.Title = sanitizer.StripTags(strings.Trim(e.Title, " \n\t"))
|
|
||||||
entry.Enclosures = e.GetEnclosures()
|
|
||||||
|
|
||||||
if entry.Title == "" {
|
|
||||||
entry.Title = entry.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
return entry
|
|
||||||
}
|
|
||||||
|
|
||||||
func getAuthor(author Author) string {
|
|
||||||
if author.Name != "" {
|
if author.Name != "" {
|
||||||
return author.Name
|
return author.Name
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
|
|
||||||
// Parse returns a normalized feed struct from a Atom feed.
|
// Parse returns a normalized feed struct from a Atom feed.
|
||||||
func Parse(data io.Reader) (*model.Feed, error) {
|
func Parse(data io.Reader) (*model.Feed, error) {
|
||||||
atomFeed := new(AtomFeed)
|
atomFeed := new(atomFeed)
|
||||||
decoder := xml.NewDecoder(data)
|
decoder := xml.NewDecoder(data)
|
||||||
decoder.CharsetReader = charset.NewReaderLabel
|
decoder.CharsetReader = charset.NewReaderLabel
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue