Handle RSS entries with Atom links
This commit is contained in:
parent
cf8af56a99
commit
557cf9c21d
3 changed files with 114 additions and 38 deletions
|
@ -14,7 +14,7 @@ import (
|
||||||
"golang.org/x/net/html/charset"
|
"golang.org/x/net/html/charset"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse returns a normalized feed struct.
|
// Parse returns a normalized feed struct from a RSS feed.
|
||||||
func Parse(data io.Reader) (*model.Feed, error) {
|
func Parse(data io.Reader) (*model.Feed, error) {
|
||||||
feed := new(rssFeed)
|
feed := new(rssFeed)
|
||||||
decoder := xml.NewDecoder(data)
|
decoder := xml.NewDecoder(data)
|
||||||
|
|
|
@ -161,6 +161,51 @@ func TestParseEntryWithoutLink(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseEntryWithAtomLink(t *testing.T) {
|
||||||
|
data := `<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||||
|
<channel>
|
||||||
|
<link>https://example.org/</link>
|
||||||
|
<item>
|
||||||
|
<title>Test</title>
|
||||||
|
<atom:link href="https://example.org/item" />
|
||||||
|
</item>
|
||||||
|
</channel>
|
||||||
|
</rss>`
|
||||||
|
|
||||||
|
feed, err := Parse(bytes.NewBufferString(data))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if feed.Entries[0].URL != "https://example.org/item" {
|
||||||
|
t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseEntryWithMultipleAtomLinks(t *testing.T) {
|
||||||
|
data := `<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||||
|
<channel>
|
||||||
|
<link>https://example.org/</link>
|
||||||
|
<item>
|
||||||
|
<title>Test</title>
|
||||||
|
<atom:link rel="payment" href="https://example.org/a" />
|
||||||
|
<atom:link rel="http://foobar.tld" href="https://example.org/b" />
|
||||||
|
</item>
|
||||||
|
</channel>
|
||||||
|
</rss>`
|
||||||
|
|
||||||
|
feed, err := Parse(bytes.NewBufferString(data))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if feed.Entries[0].URL != "https://example.org/b" {
|
||||||
|
t.Errorf("Incorrect entry link, got: %s", feed.Entries[0].URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseFeedURLWithAtomLink(t *testing.T) {
|
func TestParseFeedURLWithAtomLink(t *testing.T) {
|
||||||
data := `<?xml version="1.0" encoding="utf-8"?>
|
data := `<?xml version="1.0" encoding="utf-8"?>
|
||||||
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
|
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
|
||||||
|
@ -489,3 +534,11 @@ func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) {
|
||||||
t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
|
t.Errorf("Incorrect enclosure length, got: %d", feed.Entries[0].Enclosures[0].Size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseInvalidXml(t *testing.T) {
|
||||||
|
data := `garbage`
|
||||||
|
_, err := Parse(bytes.NewBufferString(data))
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Parse should returns an error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -35,12 +35,13 @@ type rssLink struct {
|
||||||
XMLName xml.Name
|
XMLName xml.Name
|
||||||
Data string `xml:",chardata"`
|
Data string `xml:",chardata"`
|
||||||
Href string `xml:"href,attr"`
|
Href string `xml:"href,attr"`
|
||||||
|
Rel string `xml:"rel,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type rssItem struct {
|
type rssItem struct {
|
||||||
GUID string `xml:"guid"`
|
GUID string `xml:"guid"`
|
||||||
Title string `xml:"title"`
|
Title string `xml:"title"`
|
||||||
Link string `xml:"link"`
|
Links []rssLink `xml:"link"`
|
||||||
OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
|
OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
|
||||||
Description string `xml:"description"`
|
Description string `xml:"description"`
|
||||||
Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
|
Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
|
||||||
|
@ -65,9 +66,9 @@ type rssEnclosure struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rssFeed) GetSiteURL() string {
|
func (r *rssFeed) GetSiteURL() string {
|
||||||
for _, elem := range r.Links {
|
for _, element := range r.Links {
|
||||||
if elem.XMLName.Space == "" {
|
if element.XMLName.Space == "" {
|
||||||
return elem.Data
|
return element.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,9 +76,9 @@ func (r *rssFeed) GetSiteURL() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *rssFeed) GetFeedURL() string {
|
func (r *rssFeed) GetFeedURL() string {
|
||||||
for _, elem := range r.Links {
|
for _, element := range r.Links {
|
||||||
if elem.XMLName.Space == "http://www.w3.org/2005/Atom" {
|
if element.XMLName.Space == "http://www.w3.org/2005/Atom" {
|
||||||
return elem.Href
|
return element.Href
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,10 +112,10 @@ func (r *rssFeed) Transform() *model.Feed {
|
||||||
|
|
||||||
return feed
|
return feed
|
||||||
}
|
}
|
||||||
func (i *rssItem) GetDate() time.Time {
|
func (r *rssItem) GetDate() time.Time {
|
||||||
value := i.PubDate
|
value := r.PubDate
|
||||||
if i.Date != "" {
|
if r.Date != "" {
|
||||||
value = i.Date
|
value = r.Date
|
||||||
}
|
}
|
||||||
|
|
||||||
if value != "" {
|
if value != "" {
|
||||||
|
@ -130,8 +131,8 @@ func (i *rssItem) GetDate() time.Time {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) GetAuthor() string {
|
func (r *rssItem) GetAuthor() string {
|
||||||
for _, element := range i.Authors {
|
for _, element := range r.Authors {
|
||||||
if element.Name != "" {
|
if element.Name != "" {
|
||||||
return element.Name
|
return element.Name
|
||||||
}
|
}
|
||||||
|
@ -141,11 +142,11 @@ func (i *rssItem) GetAuthor() string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Creator
|
return r.Creator
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) GetHash() string {
|
func (r *rssItem) GetHash() string {
|
||||||
for _, value := range []string{i.GUID, i.Link} {
|
for _, value := range []string{r.GUID, r.GetURL()} {
|
||||||
if value != "" {
|
if value != "" {
|
||||||
return helper.Hash(value)
|
return helper.Hash(value)
|
||||||
}
|
}
|
||||||
|
@ -154,33 +155,43 @@ func (i *rssItem) GetHash() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) GetContent() string {
|
func (r *rssItem) GetContent() string {
|
||||||
if i.Content != "" {
|
if r.Content != "" {
|
||||||
return i.Content
|
return r.Content
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Description
|
return r.Description
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) GetURL() string {
|
func (r *rssItem) GetURL() string {
|
||||||
if i.OriginalLink != "" {
|
if r.OriginalLink != "" {
|
||||||
return i.OriginalLink
|
return r.OriginalLink
|
||||||
}
|
}
|
||||||
|
|
||||||
return i.Link
|
for _, link := range r.Links {
|
||||||
|
if link.XMLName.Space == "http://www.w3.org/2005/Atom" && link.Href != "" && isValidLinkRelation(link.Rel) {
|
||||||
|
return link.Href
|
||||||
|
}
|
||||||
|
|
||||||
|
if link.Data != "" {
|
||||||
|
return link.Data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) GetEnclosures() model.EnclosureList {
|
func (r *rssItem) GetEnclosures() model.EnclosureList {
|
||||||
enclosures := make(model.EnclosureList, 0)
|
enclosures := make(model.EnclosureList, 0)
|
||||||
|
|
||||||
for _, enclosure := range i.Enclosures {
|
for _, enclosure := range r.Enclosures {
|
||||||
length, _ := strconv.Atoi(enclosure.Length)
|
length, _ := strconv.Atoi(enclosure.Length)
|
||||||
enclosureURL := enclosure.URL
|
enclosureURL := enclosure.URL
|
||||||
|
|
||||||
if i.OrigEnclosureLink != "" {
|
if r.OrigEnclosureLink != "" {
|
||||||
filename := path.Base(i.OrigEnclosureLink)
|
filename := path.Base(r.OrigEnclosureLink)
|
||||||
if strings.Contains(enclosureURL, filename) {
|
if strings.Contains(enclosureURL, filename) {
|
||||||
enclosureURL = i.OrigEnclosureLink
|
enclosureURL = r.OrigEnclosureLink
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,15 +205,15 @@ func (i *rssItem) GetEnclosures() model.EnclosureList {
|
||||||
return enclosures
|
return enclosures
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *rssItem) Transform() *model.Entry {
|
func (r *rssItem) Transform() *model.Entry {
|
||||||
entry := new(model.Entry)
|
entry := new(model.Entry)
|
||||||
entry.URL = i.GetURL()
|
entry.URL = r.GetURL()
|
||||||
entry.Date = i.GetDate()
|
entry.Date = r.GetDate()
|
||||||
entry.Author = i.GetAuthor()
|
entry.Author = r.GetAuthor()
|
||||||
entry.Hash = i.GetHash()
|
entry.Hash = r.GetHash()
|
||||||
entry.Content = processor.ItemContentProcessor(entry.URL, i.GetContent())
|
entry.Content = processor.ItemContentProcessor(entry.URL, r.GetContent())
|
||||||
entry.Title = sanitizer.StripTags(strings.Trim(i.Title, " \n\t"))
|
entry.Title = sanitizer.StripTags(strings.Trim(r.Title, " \n\t"))
|
||||||
entry.Enclosures = i.GetEnclosures()
|
entry.Enclosures = r.GetEnclosures()
|
||||||
|
|
||||||
if entry.Title == "" {
|
if entry.Title == "" {
|
||||||
entry.Title = entry.URL
|
entry.Title = entry.URL
|
||||||
|
@ -210,3 +221,15 @@ func (i *rssItem) Transform() *model.Entry {
|
||||||
|
|
||||||
return entry
|
return entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue