-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
293 lines (259 loc) · 7.5 KB
/
show.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
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
package main
import (
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Show is the main type. It holds information about the podcast and its episodes.
type Show struct {
URL *url.URL
Dir string // show's directory on disk
Title string `xml:"channel>title"`
Author string `xml:"channel>author"`
Image string `xml:"channel>image,href"`
Episodes []Episode `xml:"channel>item"`
}
// Sync gets the current list of available episodes, determines which of them need to be downloaded, and then gets them.
func (s *Show) Sync(mainDir string, specificEp string) (int, int, error) {
resp, err := http.Get(s.URL.String())
if err != nil {
return 0, 0, fmt.Errorf("error getting RSS feed: %v", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, 0, fmt.Errorf("error reading RSS feed: %v", err)
}
if err := xml.Unmarshal(data, s); err != nil {
return 0, 0, fmt.Errorf("error reading RSS feed: %v", err)
}
if s.Title == "" {
return 0, 0, fmt.Errorf("error parsing RSS feed: no show information found")
} else if len(s.Episodes) == 0 {
return 0, 0, fmt.Errorf("error parsing RSS feed: no episodes found")
}
Log("Found show:", s.Title)
// The feed will list episodes newest to oldest. We'll reverse that here to make error handling easier later on.
length := len(s.Episodes)
for i := 0; i < length/2; i++ {
s.Episodes[i], s.Episodes[length-1-i] = s.Episodes[length-1-i], s.Episodes[i]
}
// Make sure we can create directories and files with the names that were parsed earlier from the RSS feed.
s.Title = SanitizeTitle(s.Title)
Debug("Setting show title to", s.Title)
Debug("Setting show artist to", s.Author)
for i := range s.Episodes {
s.Episodes[i].SetShowTitle(s.Title)
s.Episodes[i].SetShowArtist(s.Author)
s.Episodes[i].SetShowImage(s.Image)
}
// Validate (or create) this show's directory.
s.Dir = filepath.Join(mainDir, s.Title)
if err := ValidateDir(s.Dir); err != nil {
return 0, 0, fmt.Errorf("invalid show directory: %v", err)
}
// Choose which episodes we want to download.
if err := s.filter(specificEp); err != nil {
return 0, 0, fmt.Errorf("error selecting episodes: %v", err)
}
switch len(s.Episodes) {
case 0:
if specificEp != "" {
return 0, 0, fmt.Errorf("episode %v not found", specificEp)
}
Log("No new episodes")
return 0, 0, nil
case 1:
Log("Downloading 1 episode")
default:
Log("Downloading", len(s.Episodes), "episodes")
}
success := 0
failures := 0
for _, episode := range s.Episodes {
message := fmt.Sprintf("\n--- Downloading %s", episode.Title)
if num := episode.NumberFormatted(); num != "" {
message += fmt.Sprintf(" (%s)", num)
}
message += " ---"
Log(message)
// Try up to 3 times to download the episode properly.
for j := 1; j <= 3; j++ {
if err := episode.Download(s.Dir); err == errDownload {
if j < 3 {
Log("Download attempt", j, "of 3 failed, trying again")
} else {
Log("ERROR: All 3 download attempts failed")
failures++
break
}
} else if err != nil {
Log("Error downloading episode:", err)
failures++
if errors.Is(err, syscall.ENOSPC) {
// If there's no space left for writing, then we'll stop the entire process.
return success, failures, fmt.Errorf("no space left on disk, stopping process")
}
break
} else {
success++
break
}
}
}
return success, failures, nil
}
// filter filters out the episodes we don't want to download.
func (s *Show) filter(specificEp string) error {
have := make(map[string]bool)
// We're going to use this function to inspect all the episodes we currently have in the show's directory.
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
filename := info.Name()
if strings.HasPrefix(filename, ".") {
Debug("Skipping hidden file:", filename)
return nil
} else if !isAudio(filename) {
Debug("Skipping non-audio file:", filename)
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Build the metadata object so we can inspect the tag contents.
// (We're temporarily turning off Debug Mode so we don't spam print all the metadata frames. They'll still get
// written to the log.)
tmpDebug := DebugMode
DebugMode = false
meta := NewMeta(nil)
if _, err := io.Copy(meta, file); err != nil && err != io.EOF {
Debug("Stopping walk check early")
return err
}
DebugMode = tmpDebug
titleID := "TIT2"
if meta.Version() == 2 {
titleID = "TT2"
}
title := getFirstValue(meta, titleID)
have[title] = true
return nil
}
if specificEp != "" {
Log("\nLooking for specified episode")
if ep, found := findSpecific(s.Episodes, specificEp); found {
s.Episodes = []Episode{ep}
} else {
s.Episodes = nil
}
} else {
Log("Building list of unsynced episodes")
// Get all the metadata titles of the episodes we already have.
if err := filepath.Walk(s.Dir, walkFunc); err != nil {
return err
}
// Compare that list to what's available to find the episodes we need to download.
want := []Episode{}
for _, episode := range s.Episodes {
if _, ok := have[episode.Title]; !ok {
Debug("Need", episode.Title)
want = append(want, episode)
}
}
s.Episodes = want
}
return nil
}
// findSpecific finds the specified episode among the episodes available for download. A season can also be specified by
// separating the season and episode numbers with a "-".
func findSpecific(episodes []Episode, specified string) (Episode, bool) {
if specified == "" {
return Episode{}, false
}
specificSeason := 0
specificEpisode := 0
parts := strings.Split(specified, "-")
switch len(parts) {
case 1:
// Only an episode was specified.
num, err := strconv.Atoi(parts[0])
if err != nil {
Log("Error parsing specified episode:", err)
return Episode{}, false
}
specificEpisode = num
case 2:
// An episode and a season were specified.
num, err := strconv.Atoi(parts[0])
if err != nil {
Log("Error parsing specified season:", err)
return Episode{}, false
}
specificSeason = num
num, err = strconv.Atoi(parts[1])
if err != nil {
Log("Error parsing specified episode:", err)
return Episode{}, false
}
specificEpisode = num
default:
Log("Error parsing specified episode/season")
return Episode{}, false
}
for _, episode := range episodes {
season, _ := strconv.Atoi(episode.Season)
number, _ := strconv.Atoi(episode.Number)
if season == specificSeason && number == specificEpisode {
if specificSeason > 0 {
Log("Found episode", specificEpisode, "of season", specificSeason)
} else {
Log("Found episode", specificEpisode)
}
return episode, true
}
}
// If we're here, then we didn't find anything.
return Episode{}, false
}
// getFirstValue gets the first value for the given frame ID. This is a convenience function for dealing with frame IDs
// that should have only one occurrence.
func getFirstValue(meta *Meta, id string) string {
values := meta.GetValues(id)
if values == nil || len(values) == 0 {
return ""
}
return string(values[0])
}
// isAudio determines if the provided file is an audio file or not.
func isAudio(filename string) bool {
switch filepath.Ext(filename) {
case ".aac":
return true
case ".midi":
return true
case ".mp3":
return true
case ".oga":
return true
case ".opus":
return true
case ".wav":
return true
case ".weba":
return true
}
return false
}