/
local.go
81 lines (68 loc) · 1.83 KB
/
local.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
// Copyright (C) 2019 Evgeny Kuznetsov (evgeny@kuznetsov.md)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along tihe this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/afero"
"path"
"regexp"
"strconv"
"strings"
)
var (
errNoEpisodes = errors.New("no episodes have been found")
)
func (pod *podcast) scanDir() error {
if pod.local == nil {
return fmt.Errorf("no local location specified")
}
files, err := afero.ReadDir(pod.local, "/")
if err != nil {
return err
}
pod.ep = make(map[int]*episode)
numRe := regexp.MustCompile(`\d+`)
for _, file := range files {
if file.IsDir() {
continue
}
num, err := strconv.Atoi(numRe.FindString(strings.TrimSuffix(file.Name(), path.Ext(file.Name()))))
if err != nil {
continue
}
epi := episode{filename: file.Name()}
pod.ep[num] = &epi
}
if len(pod.ep) == 0 {
return errNoEpisodes
}
return nil
}
func (pod *podcast) renameDownloaded() error {
max := pod.mostCurrent()
d := len(strconv.Itoa(max))
for n, ep := range pod.ep {
ext := path.Ext(ep.filename)
newname := fmt.Sprintf("%0*d%s", d, n, ext)
if ep.filename != newname {
if err := pod.local.Rename(ep.filename, newname); err != nil {
return err
}
}
ep.filename = newname
}
return nil
}