Reorganized scanning code
This commit is contained in:
@@ -152,10 +152,10 @@ type TVShow struct {
|
|||||||
Year int `xml:"year"`
|
Year int `xml:"year"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func scan_directory(directory string, db *sql.DB) error {
|
func scan_tvshow_root(root string, db *sql.DB) error {
|
||||||
files, err := ioutil.ReadDir(directory)
|
files, err := ioutil.ReadDir(root)
|
||||||
if err != nil {
|
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()
|
tx, err := db.Begin()
|
||||||
@@ -170,33 +170,39 @@ func scan_directory(directory string, db *sql.DB) error {
|
|||||||
}
|
}
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
tvshow_nfo_path := filepath.Join(directory, file.Name(), "tvshow.nfo")
|
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)
|
tvshow_nfo_file, err := os.OpenFile(tvshow_nfo_path, os.O_RDONLY, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "No tvshow.nfo file found in \"%v\"\n", filepath.Join(directory, file.Name()))
|
fmt.Fprintf(os.Stderr, "Could not open tvshow.nfo file in \"%v\"\n", show_dir)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
defer tvshow_nfo_file.Close()
|
defer tvshow_nfo_file.Close()
|
||||||
|
|
||||||
// try to parse nfo file
|
// try to parse nfo file
|
||||||
data, err := ioutil.ReadAll(tvshow_nfo_file)
|
data, err := ioutil.ReadAll(tvshow_nfo_file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "ERROR: Could not read data from nfo file: %v\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: Could not read data from tvshow.nfo file: %v\n", err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var tvshow TVShow
|
var tvshow TVShow
|
||||||
|
|
||||||
err = xml.Unmarshal(data, &tvshow)
|
err = xml.Unmarshal(data, &tvshow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "ERROR: Could not parse nfo file contents: %v\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: Could not parse tvshow.nfo file contents: %v\n", err)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Replace with print formatting logic
|
||||||
fmt.Printf("%v (%v):\n", tvshow.Title, tvshow.Year)
|
fmt.Printf("%v (%v):\n", tvshow.Title, tvshow.Year)
|
||||||
fmt.Printf(" Original Title: %v\n", tvshow.OriginalTile)
|
fmt.Printf(" Original Title: %v\n", tvshow.OriginalTile)
|
||||||
fmt.Printf(" Show Title: %v\n", tvshow.ShowTitle)
|
fmt.Printf(" Show Title: %v\n", tvshow.ShowTitle)
|
||||||
@@ -204,13 +210,10 @@ func scan_directory(directory string, db *sql.DB) error {
|
|||||||
_, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year)
|
_, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "ERROR: Could not insert tvshow into database: %v\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: Could not insert tvshow into database: %v\n", err)
|
||||||
continue
|
return
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
@@ -235,7 +238,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
err = scan_directory(config.ScanDirectory, db)
|
err = scan_tvshow_root(config.ScanDirectory, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "ERROR: Could not scan directory \"%v\": %v\n", config.ScanDirectory, err)
|
fmt.Fprintf(os.Stderr, "ERROR: Could not scan directory \"%v\": %v\n", config.ScanDirectory, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user