Files
fix-subs/fix-subs.go
2022-11-21 01:50:10 -06:00

94 lines
1.9 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
}
fmt.Println(seasonDir)
// 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
// Move the srt files within the directory into the season's directory
fmt.Println(filename)
}
}
func main() {
topDir := "/mnt/z/From Scratch (2022) [tvdb-400691]/"
// 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)
ensureSeason, _ := regexp.MatchString(`.[sS][0-9]{1,2}.`, seasonsDir.Name())
// Or if the folder is just called Season xx
if !ensureSeason {
ensureSeason, _ = regexp.MatchString(`[sS]eason\s[0-9]{1,2}`, seasonsDir.Name())
}
// Process the season if it is a season folder
if ensureSeason {
processSeason(topDir + seasonsDir.Name())
}
fmt.Println("Complete")
}
}