diff --git a/main.go b/main.go index 168d4e4..3428ea7 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "os" "path/filepath" - "regexp" _ "github.com/mattn/go-sqlite3" @@ -227,29 +226,68 @@ type Episode struct { XMLName xml.Name `xml:"episodedetails"` Title string `xml:"title"` ShowTitle string `xml:"showtitle"` - Year int `xml:"year"` + Season int `xml:"season"` + Episode int `xml:"episode"` } func scan_tvshow_episodes(show_dir string) error { + fmt.Printf("Scanning episodes for show %v\n", show_dir) + files, err := ioutil.ReadDir(show_dir) if err != nil { return fmt.Errorf("Could not enumerate files in folder: %v", err) } - // Regex to see if filename contains SXXEYY string - var re = regexp.MustCompile(".*S(\\d+)E(\\d+).*") + num_episodes := 0 for _, file := range files { - parts := re.FindStringSubmatch(file.Name()) - if len(parts) != 3 { - fmt.Fprintf(os.Stderr, "Filename \"%v\" failed regex capture (%v)\n", file.Name(), len(parts)) + ext := filepath.Ext(file.Name()) + nfo_path := filepath.Join(show_dir, file.Name()) + + if file.Name() == "tvshow.nfo" { + continue + } else if ext == ".nfo" { + fmt.Printf("Found nfo file: \"%v\"\n", file.Name()) + } else { + fmt.Printf("Skipping file \"%v\", not an nfo file\n", file.Name()) continue } - fmt.Printf("%v: S%vE%v\n", file.Name(), parts[1], parts[2]) + fmt.Printf("%v\n", file.Name()) + + nfo_file, err := os.OpenFile(nfo_path, os.O_RDONLY, 0755) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not open nfo file \"%v\": %v", nfo_path, err) + continue + } + defer nfo_file.Close() + + // try to parse nfo file + data, err := ioutil.ReadAll(nfo_file) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: Could not read data from nfo file \"%v\": %v\n", nfo_path, err) + continue + } + var episode Episode + + err = xml.Unmarshal(data, &episode) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: Could not parse contents of nfo file \"%v\": %v\n", nfo_path, err) + continue + } + + fmt.Printf(" Title: %v\n", episode.Title) + fmt.Printf(" Season %v\n", episode.Season) + fmt.Printf(" Episode %v\n", episode.Episode) + fmt.Printf(" Show: %v\n", episode.ShowTitle) + + num_episodes++ } + + fmt.Printf("Found %v episodes\n", num_episodes) + return nil }