-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast.go
157 lines (140 loc) · 3.42 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
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
package main
import (
"fmt"
"github.com/mmcdole/gofeed"
"gopkg.in/yaml.v3"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
type podcastDef struct {
Url string
Lasttime int64
Titleoverride string
}
type Episode struct {
Name string `json:"name"`
URL string `json:"url"`
TimeStamp int64 `json:"timestamp"`
Filepath string
}
func readConfig(filename string) ([]podcastDef, error) {
file, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("Can't get config %v", err)
}
var podcasts []podcastDef
err = yaml.Unmarshal(file, &podcasts)
if err != nil {
return nil, fmt.Errorf("Can't read configfile %v", err)
}
return podcasts, nil
}
func getEpisodes(URL string, lastTime int64, titleOverride string) (title string, episodes []*Episode) {
fp := gofeed.NewParser()
feed, _ := fp.ParseURL(URL)
reg, _ := regexp.Compile(`[\\\/:"*?<>|]`)
title = feed.Title
if titleOverride != "" {
title = titleOverride
}
for _, item := range feed.Items {
if item.PublishedParsed.Unix() < lastTime {
return
}
epName := reg.ReplaceAllString(item.Title, "")
extension := getFileExtension(item.Enclosures[0].URL, epName)
epPath := filepath.Join("/podcasts", title, epName+extension)
episodes = append(episodes, &Episode{
Name: item.Title,
URL: item.Enclosures[0].URL,
TimeStamp: item.PublishedParsed.Unix(),
Filepath: epPath,
})
}
return
}
func makeDirectory(title string) error {
err := os.Mkdir(filepath.Join("/podcasts", title), 0755)
if err != nil && !os.IsExist(err) {
return err
}
return nil
}
func getFileExtension(url, eptitle string) (extension string) {
extension = filepath.Ext(url)
if extension == "" {
log.Print("Assuming .mp3 extension for %s", eptitle)
return ".mp3"
}
return strings.Split(extension, "?")[0]
}
func downloadEpisode(url, filePath, epTitle string) error {
log.Print("Downloading ", epTitle)
resp, err := http.Get(url)
if err != nil {
return err
}
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
_, err = file.ReadFrom(resp.Body)
return nil
}
func checkEpisodesExist(episodesList []*Episode, title string) (episodes []*Episode) {
for _, e := range episodesList {
_, err := os.Stat(e.Filepath)
if !os.IsNotExist(err) {
continue
}
episodes = append(episodes, e)
}
return episodes
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func downloadPodcast(episodes []*Episode, title string, maxDownload int) error {
err := makeDirectory(title)
if err != nil {
return err
}
initialSize := len(episodes)
log.Print("Found ", initialSize, " episodes for ", title)
episodes = checkEpisodesExist(episodes, title)
log.Print("Skipping ", initialSize-len(episodes), " episodes that already exist")
for _, e := range episodes[:Min(maxDownload, len(episodes))] {
err := downloadEpisode(e.URL, e.Filepath, e.Name)
if err != nil {
log.Print("Error getting episode ", e.Name)
log.Print(err)
}
}
return nil
}
func getPodcast(podcast podcastDef, maxDownload int) error {
title, episodes := getEpisodes(podcast.Url, podcast.Lasttime, podcast.Titleoverride)
err := downloadPodcast(episodes, title, maxDownload)
return err
}
func main() {
podcasts, err := readConfig("config/config.yaml")
if err != nil {
fmt.Println(err)
return
}
for _, podcast := range podcasts {
err := getPodcast(podcast, 10)
if err != nil {
fmt.Println(err)
}
}
}