From 57321cf425fc782116bc1f6309c1063c70b1989c Mon Sep 17 00:00:00 2001 From: Paul Petring Date: Tue, 19 May 2015 23:09:43 +0200 Subject: [PATCH 1/5] added atom enclosure --- atom.go | 13 +++++++++++-- feed.go | 4 +++- rss.go | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/atom.go b/atom.go index 1f2115f..f401a3f 100644 --- a/atom.go +++ b/atom.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "time" + "strconv" ) // Generates Atom feed as XML @@ -57,9 +58,12 @@ type AtomEntry struct { } type AtomLink struct { + //Atom 1.0 XMLName xml.Name `xml:"link"` Href string `xml:"href,attr"` Rel string `xml:"rel,attr,omitempty"` + Type string `xml:"type,attr,omitempty"` + Length string `xml:"length,attr,omitempty"` } type AtomFeed struct { @@ -74,7 +78,7 @@ type AtomFeed struct { Rights string `xml:"rights,omitempty"` // copyright used Subtitle string `xml:"subtitle,omitempty"` Link *AtomLink - Author *AtomAuthor // required + Author *AtomAuthor // required Contributor *AtomContributor Entries []*AtomEntry } @@ -106,9 +110,14 @@ func newAtomEntry(i *Item) *AtomEntry { name, email = i.Author.Name, i.Author.Email } + + if(i.Link.Length>0) { + i.Link.Rel = "enclosure" + } + x := &AtomEntry{ Title: i.Title, - Link: &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel}, + Link: &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type, Length: strconv.FormatInt(i.Link.Length,10)}, Content: c, Id: id, Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created), diff --git a/feed.go b/feed.go index 7a6d133..bef34bd 100644 --- a/feed.go +++ b/feed.go @@ -7,7 +7,8 @@ import ( ) type Link struct { - Href, Rel string + Href, Rel,Type string + Length int64 } type Author struct { @@ -24,6 +25,7 @@ type Item struct { Created time.Time } + type Feed struct { Title string Link *Link diff --git a/rss.go b/rss.go index b1afcff..202278f 100644 --- a/rss.go +++ b/rss.go @@ -73,6 +73,7 @@ type RssItem struct { } type RssEnclosure struct { + //RSS 2.0 XMLName xml.Name `xml:"enclosure"` Url string `xml:"url,attr"` Length string `xml:"length,attr"` @@ -90,6 +91,7 @@ func newRssItem(i *Item) *RssItem { Link: i.Link.Href, Description: i.Description, Guid: i.Id, + Enclosure: &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: string(i.Link.Length)}, PubDate: anyTimeFormat(time.RFC822, i.Created, i.Updated), } if i.Author != nil { From 76198fdfb02ac4608f4679cf17dd32642cc14822 Mon Sep 17 00:00:00 2001 From: Paul Petring Date: Sun, 12 Jul 2015 02:49:30 +0200 Subject: [PATCH 2/5] fixing string encoding issue in rss enclosure length attribute --- rss.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rss.go b/rss.go index 202278f..79364bd 100644 --- a/rss.go +++ b/rss.go @@ -76,7 +76,7 @@ type RssEnclosure struct { //RSS 2.0 XMLName xml.Name `xml:"enclosure"` Url string `xml:"url,attr"` - Length string `xml:"length,attr"` + Length int64 `xml:"length,attr"` Type string `xml:"type,attr"` } @@ -91,7 +91,7 @@ func newRssItem(i *Item) *RssItem { Link: i.Link.Href, Description: i.Description, Guid: i.Id, - Enclosure: &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: string(i.Link.Length)}, + Enclosure: &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: i.Link.Length}, PubDate: anyTimeFormat(time.RFC822, i.Created, i.Updated), } if i.Author != nil { From a79072d462f7e669e7d1d18a33d647b314502bf8 Mon Sep 17 00:00:00 2001 From: Paul Petring Date: Sat, 1 Aug 2015 15:48:43 +0200 Subject: [PATCH 3/5] fixed tests for cases without enclosure --- atom.go | 13 ++++++++----- rss.go | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/atom.go b/atom.go index f401a3f..3273d1a 100644 --- a/atom.go +++ b/atom.go @@ -111,17 +111,20 @@ func newAtomEntry(i *Item) *AtomEntry { } - if(i.Link.Length>0) { - i.Link.Rel = "enclosure" - } - x := &AtomEntry{ Title: i.Title, - Link: &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type, Length: strconv.FormatInt(i.Link.Length,10)}, + Link: &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type}, Content: c, Id: id, Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created), } + + if(i.Link.Length>0) { + i.Link.Rel = "enclosure" + x.Link = &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type, Length: strconv.FormatInt(i.Link.Length,10)} + } + + if len(name) > 0 || len(email) > 0 { x.Author = &AtomAuthor{AtomPerson: AtomPerson{Name: name, Email: email}} } diff --git a/rss.go b/rss.go index 79364bd..b37b9cf 100644 --- a/rss.go +++ b/rss.go @@ -91,9 +91,11 @@ func newRssItem(i *Item) *RssItem { Link: i.Link.Href, Description: i.Description, Guid: i.Id, - Enclosure: &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: i.Link.Length}, PubDate: anyTimeFormat(time.RFC822, i.Created, i.Updated), } + if(i.Link.Length > 0 || i.Link.Type != ""){ + item.Enclosure = &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: i.Link.Length} + } if i.Author != nil { item.Author = i.Author.Name } From 83d205214a66ad87848907c54410c3b4707a1742 Mon Sep 17 00:00:00 2001 From: Paul Petring Date: Sat, 1 Aug 2015 16:09:32 +0200 Subject: [PATCH 4/5] added test case for podcast enclosures for atom and rss --- feed_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/feed_test.go b/feed_test.go index 9b05d65..74f7a71 100644 --- a/feed_test.go +++ b/feed_test.go @@ -42,6 +42,13 @@ var atomOutput = `How to use interfaces <em>effectively</em> + + Never Gonna Give You Up Mp3 + 2013-01-16T21:52:35-05:00 + tag:example.com,2013-01-16:/RickRoll.mp3 + Never gonna give you up - Never gonna let you down. + + ` var rssOutput = ` @@ -71,6 +78,13 @@ var rssOutput = ` How to use interfaces <em>effectively</em> 16 Jan 13 21:52 EST + + Never Gonna Give You Up Mp3 + http://example.com/RickRoll.mp3 + Never gonna give you up - Never gonna let you down. + + 16 Jan 13 21:52 EST + ` @@ -111,6 +125,12 @@ func TestFeed(t *testing.T) { Description: "How to use interfaces effectively", Created: now, }, + &Item{ + Title: "Never Gonna Give You Up Mp3", + Link: &Link{Href: "http://example.com/RickRoll.mp3",Length: 123456, Type: "audio/mpeg"}, + Description: "Never gonna give you up - Never gonna let you down.", + Created: now, + }, } atom, err := feed.ToAtom() From e2bd4557b00a49263fb0aa3e68fa9a0cb1b2732a Mon Sep 17 00:00:00 2001 From: Paul Petring Date: Sat, 8 Aug 2015 17:50:08 +0200 Subject: [PATCH 5/5] RssEnclosure.Length remains string to not break the API code now gofmt formatted --- atom.go | 12 ++++++------ feed.go | 4 +--- feed_test.go | 2 +- rss.go | 8 ++++++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/atom.go b/atom.go index 3273d1a..d0eb33c 100644 --- a/atom.go +++ b/atom.go @@ -4,8 +4,8 @@ import ( "encoding/xml" "fmt" "net/url" - "time" "strconv" + "time" ) // Generates Atom feed as XML @@ -110,7 +110,6 @@ func newAtomEntry(i *Item) *AtomEntry { name, email = i.Author.Name, i.Author.Email } - x := &AtomEntry{ Title: i.Title, Link: &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type}, @@ -119,11 +118,12 @@ func newAtomEntry(i *Item) *AtomEntry { Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created), } - if(i.Link.Length>0) { - i.Link.Rel = "enclosure" - x.Link = &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type, Length: strconv.FormatInt(i.Link.Length,10)} - } + int_Length, err := strconv.ParseInt(i.Link.Length, 10, 64) + if err == nil && (int_Length > 0 || i.Link.Type != "") { + i.Link.Rel = "enclosure" + x.Link = &AtomLink{Href: i.Link.Href, Rel: i.Link.Rel, Type: i.Link.Type, Length: i.Link.Length} + } if len(name) > 0 || len(email) > 0 { x.Author = &AtomAuthor{AtomPerson: AtomPerson{Name: name, Email: email}} diff --git a/feed.go b/feed.go index bef34bd..fe4833e 100644 --- a/feed.go +++ b/feed.go @@ -7,8 +7,7 @@ import ( ) type Link struct { - Href, Rel,Type string - Length int64 + Href, Rel, Type, Length string } type Author struct { @@ -25,7 +24,6 @@ type Item struct { Created time.Time } - type Feed struct { Title string Link *Link diff --git a/feed_test.go b/feed_test.go index 74f7a71..21a171a 100644 --- a/feed_test.go +++ b/feed_test.go @@ -127,7 +127,7 @@ func TestFeed(t *testing.T) { }, &Item{ Title: "Never Gonna Give You Up Mp3", - Link: &Link{Href: "http://example.com/RickRoll.mp3",Length: 123456, Type: "audio/mpeg"}, + Link: &Link{Href: "http://example.com/RickRoll.mp3", Length: "123456", Type: "audio/mpeg"}, Description: "Never gonna give you up - Never gonna let you down.", Created: now, }, diff --git a/rss.go b/rss.go index b37b9cf..d1dfa6d 100644 --- a/rss.go +++ b/rss.go @@ -7,6 +7,7 @@ package feeds import ( "encoding/xml" "fmt" + "strconv" "time" ) @@ -76,7 +77,7 @@ type RssEnclosure struct { //RSS 2.0 XMLName xml.Name `xml:"enclosure"` Url string `xml:"url,attr"` - Length int64 `xml:"length,attr"` + Length string `xml:"length,attr"` Type string `xml:"type,attr"` } @@ -93,7 +94,10 @@ func newRssItem(i *Item) *RssItem { Guid: i.Id, PubDate: anyTimeFormat(time.RFC822, i.Created, i.Updated), } - if(i.Link.Length > 0 || i.Link.Type != ""){ + + int_Length, err := strconv.ParseInt(i.Link.Length, 10, 64) + + if err == nil && (int_Length > 0 || i.Link.Type != "") { item.Enclosure = &RssEnclosure{Url: i.Link.Href, Type: i.Link.Type, Length: i.Link.Length} } if i.Author != nil {