diff --git a/atom.go b/atom.go index 1f2115f..d0eb33c 100644 --- a/atom.go +++ b/atom.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "net/url" + "strconv" "time" ) @@ -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 } @@ -108,11 +112,19 @@ func newAtomEntry(i *Item) *AtomEntry { 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}, Content: c, Id: id, Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created), } + + 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 7a6d133..fe4833e 100644 --- a/feed.go +++ b/feed.go @@ -7,7 +7,7 @@ import ( ) type Link struct { - Href, Rel string + Href, Rel, Type, Length string } type Author struct { diff --git a/feed_test.go b/feed_test.go index 9b05d65..21a171a 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() diff --git a/rss.go b/rss.go index b1afcff..d1dfa6d 100644 --- a/rss.go +++ b/rss.go @@ -7,6 +7,7 @@ package feeds import ( "encoding/xml" "fmt" + "strconv" "time" ) @@ -73,6 +74,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"` @@ -92,6 +94,12 @@ func newRssItem(i *Item) *RssItem { Guid: i.Id, PubDate: anyTimeFormat(time.RFC822, i.Created, i.Updated), } + + 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 { item.Author = i.Author.Name }