Add README; add cli args; finish program

This commit is contained in:
2022-11-22 00:51:28 -06:00
parent 4cf741c01e
commit 03b6a2bf4c
2 changed files with 116 additions and 9 deletions

View File

@@ -32,8 +32,6 @@ func processSeason(seasonDir string) {
return
}
fmt.Println(seasonDir)
// Go through each file
for _, seasonsDirItems := range seasonsDirList {
@@ -55,16 +53,62 @@ func processSeason(seasonDir string) {
filename := strings.TrimSuffix(seasonsDirItems.Name(), filepath.Ext(seasonsDirItems.Name()))
// Ensure the directory within subs exist matching this filename
episodeSubsDir := seasonDir + "/" + subsDir + "/" + filename
if _, err := os.Stat(episodeSubsDir); !os.IsNotExist(err) {
episodeSubsList, err := os.ReadDir(episodeSubsDir)
if err != nil {
log.Fatal(err)
}
// Move the srt files within the directory into the season's directory
// Go through sub's directory, sort for English only
for _, episodeSubsItems := range episodeSubsList {
ensureLanguage, _ := regexp.MatchString(`_[Ee]nglish.srt`, episodeSubsItems.Name())
fmt.Println(filename)
// Move the srt files within the directory into the season's directory
if ensureLanguage {
newSubsName := filename + "_" + episodeSubsItems.Name()
err := os.Rename(episodeSubsDir+"/"+episodeSubsItems.Name(), seasonDir+"/"+newSubsName)
if err != nil {
log.Fatal(err)
}
}
}
}
}
// Delete Subs folder
err = os.RemoveAll(seasonDir + "/" + subsDir)
if err != nil {
log.Fatal(err)
}
}
func formatSeason(seasonNumber string) string {
// If it starts with a '.', it is in the '.Sxx.' format
if seasonNumber[0] == '.' {
return seasonNumber[2 : len(seasonNumber)-1]
}
// Else, it is in the 'Season xx' format (do nothing)
return seasonNumber
}
func main() {
topDir := "/mnt/z/From Scratch (2022) [tvdb-400691]/"
cliArgs := os.Args
fmt.Println()
if len(cliArgs) < 2 {
fmt.Println("Missing directory!")
os.Exit(1)
}
topDir := os.Args[1]
// Confirm directory
fmt.Print("Press <ENTER> to confirm with directory: \n\t" + topDir)
_, err := fmt.Scanln()
if err != nil {
log.Fatal(err)
}
// Read in the top directory
topDirList, err := os.ReadDir(topDir)
if err != nil {
@@ -74,17 +118,27 @@ func main() {
// Each of the seasons directories
for _, seasonsDir := range topDirList {
// Ensure there's a Sxx in the name (for the season number)
ensureSeason, _ := regexp.MatchString(`.[sS][0-9]{1,2}.`, seasonsDir.Name())
// Ensure there's a 'Sxx' in the name (for the season number)
seasonRegex := regexp.MustCompile(`.[sS][0-9]{1,2}.`)
ensureSeason := seasonRegex.MatchString(seasonsDir.Name())
// Or if the folder is just called Season xx
// Or if the folder is just called 'Season xx'
if !ensureSeason {
ensureSeason, _ = regexp.MatchString(`[sS]eason\s[0-9]{1,2}`, seasonsDir.Name())
seasonRegex = regexp.MustCompile(`[sS]eason\s[0-9]{1,2}`)
ensureSeason = seasonRegex.MatchString(seasonsDir.Name())
}
// Process the season if it is a season folder
if ensureSeason {
processSeason(topDir + seasonsDir.Name())
// Rename the season folder to 'Season xx'
seasonFound := seasonRegex.FindStringSubmatch(seasonsDir.Name())
seasonNumber := formatSeason(seasonFound[0])
err := os.Rename(topDir+seasonsDir.Name(), topDir+"Season "+seasonNumber)
if err != nil {
log.Fatal(err)
}
}
fmt.Println("Complete")