93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package metadata
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.meatbag.se/varl/subsyt/internal/models"
|
|
"git.meatbag.se/varl/subsyt/internal/nfo"
|
|
)
|
|
|
|
func findFiles(scanPath string, ext string) ([]string, error) {
|
|
var result []string
|
|
|
|
err := filepath.Walk(scanPath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !info.IsDir() && strings.HasSuffix(path, ext) {
|
|
result = append(result, path)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error walking directory: %w", err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func Generate(outDir string, title string, dryRun bool) {
|
|
showDir := filepath.Join(outDir, title)
|
|
log.Printf("Writing NFO's for %s\n", showDir)
|
|
|
|
if dryRun {
|
|
return
|
|
}
|
|
|
|
infojsons, err := findFiles(showDir, ".info.json")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
show := regexp.MustCompile("s(NA)")
|
|
season := regexp.MustCompile(`s\d\d\d\d`)
|
|
|
|
for index, path := range infojsons {
|
|
log.Println(index, path)
|
|
switch {
|
|
case show.MatchString(path):
|
|
show := models.LoadShow(path)
|
|
nfo.WriteShowInfo(show, filepath.Join(showDir, "tvshow.nfo"))
|
|
showBanner(show, showDir)
|
|
showFanart(show, showDir)
|
|
case season.MatchString(path):
|
|
ep := models.LoadEpisode(path)
|
|
nfo.WriteEpisodeNFO(ep, path)
|
|
default:
|
|
log.Printf("no match for '%s'\n", path)
|
|
}
|
|
|
|
os.Remove(path)
|
|
}
|
|
|
|
images, err := findFiles(showDir, ".jpg")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for index, path := range images {
|
|
log.Println(index, path)
|
|
switch {
|
|
case show.MatchString(path):
|
|
showPoster(path, showDir)
|
|
case season.MatchString(path):
|
|
episodeImage(path)
|
|
default:
|
|
log.Printf("no match for '%s'\n", path)
|
|
}
|
|
}
|
|
|
|
del := filepath.Join(showDir, "sNA")
|
|
log.Printf("removing '%s'\n", del)
|
|
err = os.RemoveAll(del)
|
|
if err != nil {
|
|
log.Println("failed to remove", err)
|
|
}
|
|
}
|