forked from podpalinc/rss-feed-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itunes.go
186 lines (165 loc) · 5.66 KB
/
itunes.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package ext
import (
"fmt"
"strconv"
"time"
)
// ITunesFeedExtension is a set of extension
// fields for RSS feeds.
type ITunesFeedExtension struct {
Author string `json:"author,omitempty"`
Block string `json:"block,omitempty"`
Categories []*ITunesCategory `json:"categories,omitempty"`
Explicit string `json:"explicit,omitempty"`
Keywords string `json:"keywords,omitempty"`
Owner *ITunesOwner `json:"owner,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Summary string `json:"summary,omitempty"`
Image string `json:"image,omitempty"`
Complete string `json:"complete,omitempty"`
NewFeedURL string `json:"newFeedUrl,omitempty"`
Type string `json:"type,omitempty"`
}
// ITunesItemExtension is a set of extension
// fields for RSS items.
type ITunesItemExtension struct {
Author string `json:"author,omitempty"`
Block string `json:"block,omitempty"`
Duration string `json:"duration,omitempty"`
Explicit string `json:"explicit,omitempty"`
Keywords string `json:"keywords,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Summary string `json:"summary,omitempty"`
Image string `json:"image,omitempty"`
IsClosedCaptioned string `json:"isClosedCaptioned,omitempty"`
Episode string `json:"episode,omitempty"`
Season string `json:"season,omitempty"`
Order string `json:"order,omitempty"`
EpisodeType string `json:"episodeType,omitempty"`
}
// ITunesCategory is a category element for itunes feeds.
type ITunesCategory struct {
Text string `json:"text,omitempty"`
Subcategory *ITunesCategory `json:"subcategory,omitempty"`
}
// ITunesOwner is the owner of a particular itunes feed.
type ITunesOwner struct {
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
}
// NewITunesFeedExtension creates an ITunesFeedExtension given an
// extension map for the "itunes" key.
func NewITunesFeedExtension(extensions map[string][]Extension) *ITunesFeedExtension {
feed := &ITunesFeedExtension{}
feed.Author = parseTextExtension("author", extensions)
feed.Block = parseTextExtension("block", extensions)
feed.Explicit = parseTextExtension("explicit", extensions)
feed.Keywords = parseTextExtension("keywords", extensions)
feed.Subtitle = parseTextExtension("subtitle", extensions)
feed.Summary = parseTextExtension("summary", extensions)
feed.Image = parseImage(extensions)
feed.Complete = parseTextExtension("complete", extensions)
feed.NewFeedURL = parseTextExtension("new-feed-url", extensions)
feed.Categories = parseCategories(extensions)
feed.Owner = parseOwner(extensions)
feed.Type = parseTextExtension("type", extensions)
return feed
}
// NewITunesItemExtension creates an ITunesItemExtension given an
// extension map for the "itunes" key.
func NewITunesItemExtension(extensions map[string][]Extension) *ITunesItemExtension {
entry := &ITunesItemExtension{}
entry.Author = parseTextExtension("author", extensions)
entry.Block = parseTextExtension("block", extensions)
// entry.Duration = parseTextExtension("duration", extensions)
entry.Duration = parseDuration(extensions)
entry.Explicit = parseTextExtension("explicit", extensions)
entry.Subtitle = parseTextExtension("subtitle", extensions)
entry.Summary = parseTextExtension("summary", extensions)
entry.Keywords = parseTextExtension("keywords", extensions)
entry.Image = parseImage(extensions)
entry.IsClosedCaptioned = parseTextExtension("isClosedCaptioned", extensions)
entry.Episode = parseTextExtension("episode", extensions)
entry.Season = parseTextExtension("season", extensions)
entry.Order = parseTextExtension("order", extensions)
entry.EpisodeType = parseTextExtension("episodeType", extensions)
return entry
}
func parseImage(extensions map[string][]Extension) (image string) {
if extensions == nil {
return
}
matches, ok := extensions["image"]
if !ok || len(matches) == 0 {
return
}
image = matches[0].Attrs["href"]
return
}
func parseDuration(extensions map[string][]Extension) (duration string) {
if extensions == nil {
return
}
matches, ok := extensions["duration"]
if !ok || len(matches) == 0 {
return
}
// Convert hh:mm:ss to a format that parseDuration accepts.
duration = matches[0].Value
units := []string{"m", "h"}
counter := 0
for i := len(duration) - 1; i >= 0; i-- {
if string(duration[i]) == ":" {
duration = duration[:i] + units[counter] + duration[i+1:]
counter++
}
}
duration = duration + "s"
t1, err := time.ParseDuration(duration)
if err != nil {
fmt.Println(err)
}
return strconv.Itoa(int(t1.Seconds()))
}
func parseOwner(extensions map[string][]Extension) (owner *ITunesOwner) {
if extensions == nil {
return
}
matches, ok := extensions["owner"]
if !ok || len(matches) == 0 {
return
}
owner = &ITunesOwner{}
if name, ok := matches[0].Children["name"]; ok {
owner.Name = name[0].Value
}
if email, ok := matches[0].Children["email"]; ok {
owner.Email = email[0].Value
}
return
}
func parseCategories(extensions map[string][]Extension) (categories []*ITunesCategory) {
if extensions == nil {
return
}
matches, ok := extensions["category"]
if !ok || len(matches) == 0 {
return
}
categories = []*ITunesCategory{}
for _, cat := range matches {
c := &ITunesCategory{}
if text, ok := cat.Attrs["text"]; ok {
c.Text = text
}
if subs, ok := cat.Children["category"]; ok {
s := &ITunesCategory{}
if text, ok := subs[0].Attrs["text"]; ok {
s.Text = text
}
c.Subcategory = s
}
categories = append(categories, c)
}
return
}