Skip to content

Commit

Permalink
Merge pull request #21 from honky/master
Browse files Browse the repository at this point in the history
added enclosure and its attributes to support podcast feeds
  • Loading branch information
kisielk committed Aug 10, 2015
2 parents d92c76c + 97d1d6c commit 3d9f1e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
16 changes: 14 additions & 2 deletions atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/xml"
"fmt"
"net/url"
"strconv"
"time"
)

Expand Down Expand Up @@ -57,9 +58,12 @@ type AtomEntry struct {
}

type AtomLink struct {
//Atom 1.0 <link rel="enclosure" type="audio/mpeg" title="MP3" href="http://www.example.org/myaudiofile.mp3" length="1234" />
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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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}}
}
Expand Down
2 changes: 1 addition & 1 deletion feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type Link struct {
Href, Rel string
Href, Rel, Type, Length string
}

type Author struct {
Expand Down
20 changes: 20 additions & 0 deletions feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ var atomOutput = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.
<content type="html">How to use interfaces &lt;em&gt;effectively&lt;/em&gt;</content>
<link href="http://jmoiron.net/blog/idiomatic-code-reuse-in-go/"></link>
</entry>
<entry>
<title>Never Gonna Give You Up Mp3</title>
<updated>2013-01-16T21:52:35-05:00</updated>
<id>tag:example.com,2013-01-16:/RickRoll.mp3</id>
<content type="html">Never gonna give you up - Never gonna let you down.</content>
<link href="http://example.com/RickRoll.mp3" rel="enclosure" type="audio/mpeg" length="123456"></link>
</entry>
</feed>`

var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
Expand Down Expand Up @@ -71,6 +78,13 @@ var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
<description>How to use interfaces &lt;em&gt;effectively&lt;/em&gt;</description>
<pubDate>16 Jan 13 21:52 EST</pubDate>
</item>
<item>
<title>Never Gonna Give You Up Mp3</title>
<link>http://example.com/RickRoll.mp3</link>
<description>Never gonna give you up - Never gonna let you down.</description>
<enclosure url="http://example.com/RickRoll.mp3" length="123456" type="audio/mpeg"></enclosure>
<pubDate>16 Jan 13 21:52 EST</pubDate>
</item>
</channel>
</rss>`

Expand Down Expand Up @@ -111,6 +125,12 @@ func TestFeed(t *testing.T) {
Description: "How to use interfaces <em>effectively</em>",
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()
Expand Down
8 changes: 8 additions & 0 deletions rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package feeds
import (
"encoding/xml"
"fmt"
"strconv"
"time"
)

Expand Down Expand Up @@ -73,6 +74,7 @@ type RssItem struct {
}

type RssEnclosure struct {
//RSS 2.0 <enclosure url="http://example.com/file.mp3" length="123456789" type="audio/mpeg" />
XMLName xml.Name `xml:"enclosure"`
Url string `xml:"url,attr"`
Length string `xml:"length,attr"`
Expand All @@ -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
}
Expand Down

0 comments on commit 3d9f1e4

Please sign in to comment.