Reorganized scanning code

This commit is contained in:
2022-04-13 20:33:44 -06:00
parent 5742665843
commit b4740fdad7
+41 -38
View File
@@ -152,10 +152,10 @@ type TVShow struct {
Year int `xml:"year"`
}
func scan_directory(directory string, db *sql.DB) error {
files, err := ioutil.ReadDir(directory)
func scan_tvshow_root(root string, db *sql.DB) error {
files, err := ioutil.ReadDir(root)
if err != nil {
return fmt.Errorf("Could not get files in directory \"%v\": %v", directory, err)
return fmt.Errorf("Could not get files in directory \"%v\": %v", root, err)
}
tx, err := db.Begin()
@@ -170,47 +170,50 @@ func scan_directory(directory string, db *sql.DB) error {
}
defer stmt.Close()
for _, file := range files {
if file.IsDir() {
tvshow_nfo_path := filepath.Join(directory, file.Name(), "tvshow.nfo")
tvshow_nfo_file, err := os.OpenFile(tvshow_nfo_path, os.O_RDONLY, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "No tvshow.nfo file found in \"%v\"\n", filepath.Join(directory, file.Name()))
continue
}
defer tvshow_nfo_file.Close()
// try to parse nfo file
data, err := ioutil.ReadAll(tvshow_nfo_file)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not read data from nfo file: %v\n", err)
continue
}
var tvshow TVShow
err = xml.Unmarshal(data, &tvshow)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not parse nfo file contents: %v\n", err)
continue
}
fmt.Printf("%v (%v):\n", tvshow.Title, tvshow.Year)
fmt.Printf(" Original Title: %v\n", tvshow.OriginalTile)
fmt.Printf(" Show Title: %v\n", tvshow.ShowTitle)
_, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not insert tvshow into database: %v\n", err)
continue
}
insert_tvshow_nfo(filepath.Join(root, file.Name()), stmt)
}
}
return nil
}
func insert_tvshow_nfo(show_dir string, stmt *sql.Stmt) {
tvshow_nfo_path := filepath.Join(show_dir, "tvshow.nfo")
tvshow_nfo_file, err := os.OpenFile(tvshow_nfo_path, os.O_RDONLY, 0755)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open tvshow.nfo file in \"%v\"\n", show_dir)
return
}
defer tvshow_nfo_file.Close()
// try to parse nfo file
data, err := ioutil.ReadAll(tvshow_nfo_file)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not read data from tvshow.nfo file: %v\n", err)
return
}
var tvshow TVShow
err = xml.Unmarshal(data, &tvshow)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not parse tvshow.nfo file contents: %v\n", err)
return
}
// TODO: Replace with print formatting logic
fmt.Printf("%v (%v):\n", tvshow.Title, tvshow.Year)
fmt.Printf(" Original Title: %v\n", tvshow.OriginalTile)
fmt.Printf(" Show Title: %v\n", tvshow.ShowTitle)
_, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not insert tvshow into database: %v\n", err)
return
}
}
func main() {
@@ -235,7 +238,7 @@ func main() {
}
defer db.Close()
err = scan_directory(config.ScanDirectory, db)
err = scan_tvshow_root(config.ScanDirectory, db)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not scan directory \"%v\": %v\n", config.ScanDirectory, err)
os.Exit(1)