-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast.go
127 lines (112 loc) · 3.15 KB
/
podcast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"encoding/xml"
"time"
)
const (
contentNs = "http://purl.org/rss/1.0/modules/content/"
wfwNs = "http://wellformedweb.org/CommentAPI/"
dcNs = "http://purl.org/dc/elements/1.1/"
atomNs = "http://www.w3.org/2005/Atom"
syNs = "http://purl.org/rss/1.0/modules/syndication/"
slashNs = "http://purl.org/rss/1.0/modules/slash/"
itunesNs = "http://www.itunes.com/dtds/podcast-1.0.dtd"
rawvoiceNs = "http://www.rawvoice.com/rawvoiceRssModule/"
version = "2.0"
)
// Rss wraps the given RSS channel.
type Rss struct {
XMLName xml.Name `xml:"rss"`
ContentXmlns string `xml:"xmlns:content,attr,omitempty"`
WfwXmlns string `xml:"xmlns:wfw,attr,omitempty"`
DcXmlns string `xml:"xmlns:dc,attr,omitempty"`
AtomXmlns string `xml:"xmlns:atom,attr,omitempty"`
SyXmlns string `xml:"xmlns:sy,attr,omitempty"`
SlashXmlns string `xml:"xmlns:slash,attr,omitempty"`
ItunesXmlns string `xml:"xmlns:itunes,attr,omitempty"`
RawVoiceXmlns string `xml:"xmlns:rawvoice,attr,omitempty"`
Version string `xml:"version,attr"`
Channel *Channel `xml:"channel"`
}
func defaultRss() *Rss {
return &Rss{
ContentXmlns: contentNs,
WfwXmlns: wfwNs,
DcXmlns: dcNs,
AtomXmlns: atomNs,
SyXmlns: syNs,
SlashXmlns: slashNs,
ItunesXmlns: itunesNs,
RawVoiceXmlns: rawvoiceNs,
Version: version,
}
}
func (r *Rss) Xml() ([]byte, error) {
var buf bytes.Buffer
if _, err := buf.Write([]byte(xml.Header)); err != nil {
return nil, err
}
enc := xml.NewEncoder(&buf)
enc.Indent("", " ")
if err := enc.Encode(r); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
type Channel struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Language string `xml:"language"`
Copyright string `xml:"copyright"`
Image *Image `xml:"image"`
ItunesChannel `xml:",omitempty"`
Item []*Item `xml:"item"`
}
func (c *Channel) Itunes(category string, explicit bool) {
c.ItunesChannel = ItunesChannel{
Image: ItunesImage{c.Image.Url},
Category: ItunesCategory{category},
Explicit: explicit,
}
}
type Image struct {
Url string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
}
type Item struct {
Title string `xml:"title"`
Description CData `xml:"description"`
PubDate *PubDate `xml:"pubDate"`
Enclosure Enclosure `xml:"enclosure"`
Guid string `xml:"guid"`
ItunesItem
}
func (i *Item) Itunes(duration int, image string) {
i.ItunesItem = ItunesItem{
Duration: duration,
Image: ItunesImage{image},
}
}
type Enclosure struct {
Url string `xml:"url,attr"`
Length string `xml:"length,attr"`
Type string `xml:"type,attr"`
}
type PubDate struct {
time.Time
}
func (p PubDate) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {
return err
}
if err := e.EncodeToken(xml.CharData(p.Format(time.RFC1123Z))); err != nil {
return err
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
type CData struct {
Value string `xml:",cdata"`
}