From 832892fa415704409e5a6fb85f17603e6f8fd017 Mon Sep 17 00:00:00 2001 From: restitux Date: Thu, 14 Apr 2022 16:52:27 -0600 Subject: [PATCH] Episodes are now inserted into database --- main.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 63e0d33..fb9ce5b 100644 --- a/main.go +++ b/main.go @@ -132,6 +132,8 @@ CREATE TABLE tvshows( ); CREATE TABLE episodes( id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT, + show_title TEXT, number INTEGER, season INTEGER ); @@ -177,7 +179,7 @@ func scan_tvshow_root(root string, db *sql.DB) error { if file.IsDir() { show_path := filepath.Join(root, file.Name()) if insert_tvshow_nfo(show_path, stmt) { - err = scan_tvshow_episodes(show_path) + err = scan_tvshow_episodes(show_path, tx) if err != nil { fmt.Fprintf(os.Stderr, "Could not scan for episodes in %v: %v", show_path, err) continue @@ -233,7 +235,7 @@ type Episode struct { Episode int `xml:"episode"` } -func scan_tvshow_episodes(show_dir string) error { +func scan_tvshow_episodes(show_dir string, tx *sql.Tx) error { log.Infof("Scanning episodes for show %v\n", show_dir) @@ -244,6 +246,12 @@ func scan_tvshow_episodes(show_dir string) error { num_episodes := 0 + stmt, err := tx.Prepare("insert into episodes(title, show_title, number, season) values(?, ?, ?, ?)") + if err != nil { + return fmt.Errorf("Could not prepare statement: %v", err) + } + defer stmt.Close() + for _, file := range files { ext := filepath.Ext(file.Name()) nfo_path := filepath.Join(show_dir, file.Name()) @@ -285,6 +293,12 @@ func scan_tvshow_episodes(show_dir string) error { log.Debugf(" Episode %v\n", episode.Episode) log.Debugf(" Show: %v\n", episode.ShowTitle) + _, err = stmt.Exec(episode.Title, episode.ShowTitle, episode.Episode, episode.Season) + if err != nil { + log.Debugf("Could not insert episode %v of tvshow \"%v\" into database: %v\n", episode.Episode, episode.ShowTitle, err) + continue + } + num_episodes++ } @@ -294,7 +308,6 @@ func scan_tvshow_episodes(show_dir string) error { return nil } - var log = logging.MustGetLogger("ikinuki-server") func main() {