-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_rename.go
129 lines (99 loc) · 2.93 KB
/
cmd_rename.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
package main
import (
"github.com/pboehm/series/renamer"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"regexp"
)
var renameEpisodes, addToIndex bool
var renameAndIndexCmd = &cobra.Command{
Use: "rename_and_index",
Short: "Renames and indexes the supplied episodes.",
Run: renameAndIndexHandler,
}
func renameAndIndexHandler(cmd *cobra.Command, args []string) {
dir := appConfig.EpisodeDirectory
if customEpisodeDirectory != "" {
dir = customEpisodeDirectory
}
HandleError(os.Chdir(dir))
interestingEntries := GetInterestingDirEntries()
if len(interestingEntries) == 0 {
os.Exit(0)
}
callPreProcessingHook()
loadIndex()
LOG.Println("### Process all interesting files ...")
renameableEpisodes := HandleInterestingEpisodes(interestingEntries)
if len(renameableEpisodes) > 0 && renameEpisodes {
writeIndex()
LOG.Println("### Renaming episodes ...")
for _, episode := range renameableEpisodes {
LOG.Printf("> %s: %s", episode.Series, episode.CleanedFileName())
HandleError(episode.Rename("."))
LOG.Printf(" [OK]\n")
callEpisodeHook(episode.CleanedFileName(), episode.Series)
}
callPostProcessingHook()
}
}
func GetInterestingDirEntries() []string {
content, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
validRegex := regexp.MustCompile("^S\\d+E\\d+.-.\\w+.*\\.\\w+$")
var interesting []string
for _, entry := range content {
entryPath := entry.Name()
if !renamer.IsInterestingDirEntry(entryPath) {
continue
}
if validRegex.Match([]byte(entryPath)) {
continue
}
interesting = append(interesting, entryPath)
}
return interesting
}
func HandleInterestingEpisodes(entries []string) []*renamer.Episode {
var renameableEpisodes []*renamer.Episode
for _, entryPath := range entries {
episode, err := renamer.CreateEpisodeFromPath(entryPath)
if err != nil {
LOG.Printf("!!! '%s' - %s\n\n", entryPath, err)
continue
}
episode.RemoveTrashWords()
if !episode.HasValidEpisodeName() {
episode.SetDefaultEpisodeName()
}
LOG.Printf("<<< %s\n", entryPath)
LOG.Printf(">>> %s\n", episode.CleanedFileName())
if !episode.CanBeRenamed() {
LOG.Printf("!!! '%s' is currently not renameable\n\n", entryPath)
continue
}
if addToIndex {
added, addedErr := seriesIndex.AddEpisode(episode)
if !added {
LOG.Printf("!!! couldn't be added to the index: %s\n\n", addedErr)
continue
}
LOG.Printf("---> succesfully added to series index\n\n")
}
renameableEpisodes = append(renameableEpisodes, episode)
}
return renameableEpisodes
}
func init() {
renameAndIndexCmd.Flags().BoolVarP(&renameEpisodes, "rename", "r", true,
"Do actually rename the episodes.")
renameAndIndexCmd.Flags().BoolVarP(&addToIndex, "index", "i", true,
"Add the episodes to index.")
indexCmd.Flags().BoolVarP(&renameEpisodes, "rename", "r", true,
"Do actually rename the episodes.")
indexCmd.Flags().BoolVarP(&addToIndex, "index", "i", true,
"Add the episodes to index.")
}