129 lines
2.8 KiB
Go
129 lines
2.8 KiB
Go
package metadata
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"git.meatbag.se/varl/subsyt/internal/dl"
|
|
"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)
|
|
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)
|
|
}
|
|
}
|
|
|
|
func episodeImage(path string) {
|
|
if strings.Contains(path, "-thumb") {
|
|
log.Printf("thumbnail detected '%s'\n", path)
|
|
return
|
|
}
|
|
thumb := strings.Replace(path, ".jpg", "-thumb.jpg", 1)
|
|
log.Printf("renaming thumbnail from '%s' to '%s'\n", path, thumb)
|
|
err := os.Rename(path, thumb)
|
|
if err != nil {
|
|
log.Printf("failed to rename '%s' to '%s\n'", path, thumb)
|
|
}
|
|
}
|
|
|
|
func showPoster(path string, show_dir string) {
|
|
poster := filepath.Join(show_dir, "poster.jpg")
|
|
log.Printf("renaming show image from '%s' to '%s'\n", path, poster)
|
|
err := os.Rename(path, poster)
|
|
if err != nil {
|
|
log.Printf("failed to rename '%s' to '%s\n'", path, poster)
|
|
}
|
|
}
|
|
|
|
func showBanner(show models.Show, showDir string) {
|
|
for index, thumb := range show.Thumbnails {
|
|
log.Println(index, thumb)
|
|
if thumb.Id == "banner_uncropped" {
|
|
log.Println("found banner candidate")
|
|
dl.Fetch(dl.Download{
|
|
Url: thumb.Url,
|
|
OutDir: showDir,
|
|
Name: "banner.jpg",
|
|
})
|
|
}
|
|
}
|
|
}
|