-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodcast.go
90 lines (83 loc) · 1.86 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
package nhkpod
import (
"github.com/eduncan911/podcast"
"io/fs"
"io/ioutil"
"path"
"path/filepath"
"time"
)
var Pubdate = time.Date(2011, 9, 1, 0, 0, 0, 0, time.UTC)
func WritePodcastFile(s PodcastSettings, program Program) error {
p, err := GeneratePodcast(s, program)
if err != nil {
return err
}
err = ioutil.WriteFile(s.RootDir+"/feed.rss", p.Bytes(), 444)
if err != nil {
return err
}
return nil
}
func GeneratePodcast(s PodcastSettings, program Program) (*podcast.Podcast, error) {
audioFiles, err := GetAudioFiles(s.RootDir, program)
if err != nil {
return nil, err
}
eps := make([]Episode, len(audioFiles))
for i, f := range audioFiles {
e, err := NewFromFile(path.Join(s.RootDir, f.Name()), f.Size())
if err != nil || e.Title == "" {
continue
}
eps[i] = *e
}
n := time.Now()
p := podcast.New(PodcastTitle(s, program), "", "", &Pubdate, &n)
p.Language = "ja_jp"
p.AddImage(program.ImageUrl)
for _, ep := range eps {
p.AddItem(toPodcastItem(s.BaseURL, ep))
}
return &p, nil
}
func PodcastTitle(s PodcastSettings, program Program) string {
if s.CornerID == "" {
return program.Title
}
return program.Title + " " + program.Corner
}
func toPodcastItem(baseUrl string, ep Episode) podcast.Item {
url := baseUrl + ep.FilePath
r := podcast.Item{
GUID: ep.ID,
Title: ep.Title,
Description: ep.Description,
PubDate: &ep.Started,
Link: url,
Enclosure: &podcast.Enclosure{
URL: url,
Type: podcast.M4A,
Length: ep.Size,
},
}
return r
}
func GetAudioFiles(audioDir string, program Program) ([]fs.FileInfo, error) {
files, err := ioutil.ReadDir(audioDir)
if err != nil {
return nil, err
}
var r []fs.FileInfo
for _, file := range files {
if file.IsDir() {
continue
}
ex := filepath.Ext(file.Name())
if ex != Encoding {
continue
}
r = append(r, file)
}
return r, nil
}