mirror of
https://codeberg.org/librarian/feeds
synced 2024-12-06 19:16:24 +01:00
Merge pull request #21 from honky/master
added enclosure and its attributes to support podcast feeds
This commit is contained in:
@@ -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 <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 {
|
||||
@@ -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}}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
Href, Rel string
|
||||
Href, Rel, Type, Length string
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
|
||||
@@ -42,6 +42,13 @@ var atomOutput = `<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.
|
||||
<content type="html">How to use interfaces <em>effectively</em></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">
|
||||
@@ -71,6 +78,13 @@ var rssOutput = `<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
|
||||
<description>How to use interfaces <em>effectively</em></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>`
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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 <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"`
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user