-
-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathxml.go
More file actions
318 lines (265 loc) · 7.41 KB
/
xml.go
File metadata and controls
318 lines (265 loc) · 7.41 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package feed
import (
"context"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
itunes "github.com/eduncan911/podcast"
"github.com/pkg/errors"
"github.com/mxpv/podsync/pkg/model"
)
// sort.Interface implementation
type timeSlice []*model.Episode
const defaultFilenameTemplate = "{{id}}"
var (
filenameTemplateTokenPattern = regexp.MustCompile(`{{\s*([a-z_]+)\s*}}`)
filenameTemplatePlaceholderPattern = regexp.MustCompile(`{{\s*([^{}]*)\s*}}`)
filenameTemplateTokenNamePattern = regexp.MustCompile(`^[a-z_]+$`)
validExtensionPattern = regexp.MustCompile(`^[a-z0-9]+$`)
invalidFilenameCharsPattern = regexp.MustCompile(`[^A-Za-z0-9._ -]+`)
multiWhitespacePattern = regexp.MustCompile(`\s+`)
)
var filenameTemplateAllowedTokens = map[string]struct{}{
"id": {},
"title": {},
"pub_date": {},
"feed_id": {},
}
func (p timeSlice) Len() int {
return len(p)
}
// In descending order
func (p timeSlice) Less(i, j int) bool {
return p[i].PubDate.After(p[j].PubDate)
}
func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func Build(_ctx context.Context, feed *model.Feed, cfg *Config, hostname string) (*itunes.Podcast, error) {
const (
podsyncGenerator = "Podsync generator (support us at https://github.com/mxpv/podsync)"
defaultCategory = "TV & Film"
)
var (
now = time.Now().UTC()
author = feed.Author
title = feed.Title
description = feed.Description
feedLink = feed.ItemURL
)
if author == "<notfound>" {
author = feed.Title
}
if cfg.Custom.Author != "" {
author = cfg.Custom.Author
}
if cfg.Custom.Title != "" {
title = cfg.Custom.Title
}
if cfg.Custom.Description != "" {
description = cfg.Custom.Description
}
if cfg.Custom.Link != "" {
feedLink = cfg.Custom.Link
}
p := itunes.New(title, feedLink, description, &feed.PubDate, &now)
p.Generator = podsyncGenerator
p.AddSubTitle(title)
p.IAuthor = author
p.AddSummary(description)
if feed.PrivateFeed {
p.IBlock = "yes"
}
if cfg.Custom.OwnerName != "" && cfg.Custom.OwnerEmail != "" {
p.IOwner = &itunes.Author{
Name: cfg.Custom.OwnerName,
Email: cfg.Custom.OwnerEmail,
}
}
if cfg.Custom.CoverArt != "" {
p.AddImage(cfg.Custom.CoverArt)
} else {
p.AddImage(feed.CoverArt)
}
if cfg.Custom.Category != "" {
p.AddCategory(cfg.Custom.Category, cfg.Custom.Subcategories)
} else {
p.AddCategory(defaultCategory, cfg.Custom.Subcategories)
}
if cfg.Custom.Explicit {
p.IExplicit = "true"
} else {
p.IExplicit = "false"
}
if cfg.Custom.Language != "" {
p.Language = cfg.Custom.Language
}
for _, episode := range feed.Episodes {
if episode.PubDate.IsZero() {
episode.PubDate = now
}
}
// Sort all episodes in descending order
sort.Sort(timeSlice(feed.Episodes))
for i, episode := range feed.Episodes {
if episode.Status != model.EpisodeDownloaded {
// Skip episodes that are not yet downloaded or have been removed
continue
}
item := itunes.Item{
GUID: episode.ID,
Link: episode.VideoURL,
Title: episode.Title,
Description: episode.Description,
ISubtitle: episode.Title,
// Some app prefer 1-based order
IOrder: strconv.Itoa(i + 1),
}
item.AddPubDate(&episode.PubDate)
item.AddSummary(episode.Description)
item.AddImage(episode.Thumbnail)
item.AddDuration(episode.Duration)
enclosureType := itunes.MP4
if feed.Format == model.FormatAudio {
enclosureType = itunes.MP3
}
if feed.Format == model.FormatCustom {
enclosureType = EnclosureFromExtension(cfg)
}
var (
episodeName = EpisodeName(cfg, episode)
downloadURL = fmt.Sprintf("%s/%s/%s", strings.TrimRight(hostname, "/"), cfg.ID, episodeName)
)
item.AddEnclosure(downloadURL, enclosureType, episode.Size)
// p.AddItem requires description to be not empty, use workaround
if item.Description == "" {
item.Description = " "
}
if cfg.Custom.Explicit {
item.IExplicit = "true"
} else {
item.IExplicit = "false"
}
if _, err := p.AddItem(item); err != nil {
return nil, errors.Wrapf(err, "failed to add item to podcast (id %q)", episode.ID)
}
}
return &p, nil
}
func EpisodeName(feedConfig *Config, episode *model.Episode) string {
return fmt.Sprintf("%s.%s", EpisodeBaseName(feedConfig, episode), episodeExtension(feedConfig))
}
func LegacyEpisodeName(feedConfig *Config, episode *model.Episode) string {
return fmt.Sprintf("%s.%s", episode.ID, episodeExtension(feedConfig))
}
func EnclosureFromExtension(feedConfig *Config) itunes.EnclosureType {
ext := normalizeExtension(feedConfig.CustomFormat.Extension)
switch ext {
case "m4a":
return itunes.M4A
case "m4v":
return itunes.M4V
case "mp4":
return itunes.MP4
case "mp3":
return itunes.MP3
case "mov":
return itunes.MOV
case "pdf":
return itunes.PDF
case "epub":
return itunes.EPUB
default:
return -1
}
}
func EpisodeBaseName(feedConfig *Config, episode *model.Episode) string {
template := strings.TrimSpace(feedConfig.FilenameTemplate)
if template == "" {
template = defaultFilenameTemplate
}
pubDate := "0000-00-00"
if !episode.PubDate.IsZero() {
pubDate = episode.PubDate.UTC().Format("2006-01-02")
}
replacements := map[string]string{
"id": episode.ID,
"title": episode.Title,
"pub_date": pubDate,
"feed_id": feedConfig.ID,
}
rendered := filenameTemplateTokenPattern.ReplaceAllStringFunc(template, func(token string) string {
match := filenameTemplateTokenPattern.FindStringSubmatch(token)
if len(match) < 2 {
return ""
}
return replacements[match[1]]
})
name := sanitizeFilename(rendered)
if name == "" {
name = sanitizeFilename(episode.ID)
}
if name == "" {
name = "episode"
}
return name
}
func ValidateFilenameTemplate(template string) error {
template = strings.TrimSpace(template)
if template == "" {
return nil
}
matches := filenameTemplatePlaceholderPattern.FindAllStringSubmatch(template, -1)
for _, match := range matches {
if len(match) < 2 {
continue
}
token := strings.TrimSpace(match[1])
if !filenameTemplateTokenNamePattern.MatchString(token) {
return errors.Errorf("unknown filename template token %q", token)
}
if _, ok := filenameTemplateAllowedTokens[token]; !ok {
return errors.Errorf("unknown filename template token %q", token)
}
}
return nil
}
func ValidateCustomExtension(extension string) error {
normalized := normalizeExtension(extension)
if normalized == "" {
return errors.New("custom format extension cannot be empty")
}
if !validExtensionPattern.MatchString(normalized) {
return errors.Errorf("custom format extension %q must contain only letters and numbers", extension)
}
return nil
}
func episodeExtension(feedConfig *Config) string {
defaultExt := "mp4"
if feedConfig.Format == model.FormatAudio {
defaultExt = "mp3"
}
ext := defaultExt
if feedConfig.Format == model.FormatCustom {
ext = normalizeExtension(feedConfig.CustomFormat.Extension)
}
if ext == "" || !validExtensionPattern.MatchString(ext) {
ext = defaultExt
}
return ext
}
func normalizeExtension(extension string) string {
normalized := strings.TrimSpace(extension)
normalized = strings.TrimPrefix(normalized, ".")
return strings.ToLower(normalized)
}
func sanitizeFilename(value string) string {
cleaned := strings.TrimSpace(value)
cleaned = invalidFilenameCharsPattern.ReplaceAllString(cleaned, "")
cleaned = multiWhitespacePattern.ReplaceAllString(cleaned, "_")
cleaned = strings.Trim(cleaned, "._- ")
return cleaned
}