Files
fix-subs/fix-subs.go

148 lines
3.4 KiB
Go

package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func checkForSubsDir(dir []os.DirEntry) string {
for _, item := range dir {
if strings.ToLower(item.Name()) == "subs" {
return item.Name()
}
}
return ""
}
func processSeason(seasonDir string) {
// Get files within each directory
seasonsDirList, err := os.ReadDir(seasonDir)
if err != nil {
log.Fatal(err)
}
// Ensure there's a subs directory
subsDir := checkForSubsDir(seasonsDirList)
if len(subsDir) == 0 {
return
}
// Go through each file
for _, seasonsDirItems := range seasonsDirList {
// If ends with .txt, ignore
if filepath.Ext(seasonsDirItems.Name()) == ".txt" {
continue
}
// If this is a directory, ignore
fileInfo, err := os.Stat(seasonDir + "/" + seasonsDirItems.Name())
if err != nil {
log.Fatal(err)
}
if fileInfo.IsDir() {
continue
}
// Get the filename without extension
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)
}
// Go through sub's directory, sort for English only
for _, episodeSubsItems := range episodeSubsList {
ensureLanguage, _ := regexp.MatchString(`_[Ee]nglish.srt`, episodeSubsItems.Name())
// 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() {
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 {
log.Fatal(err)
}
// Each of the seasons directories
for _, seasonsDir := range topDirList {
// 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'
if !ensureSeason {
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")
}
}