Insert shows (from tvshow.nfo) into db
This commit is contained in:
@@ -119,6 +119,26 @@ func open_database(data_dir string) (*sql.DB, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not open database: %v", err)
|
||||
}
|
||||
|
||||
// initalize database
|
||||
sqlStmt := `
|
||||
CREATE TABLE tvshows(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT,
|
||||
original_title TEXT,
|
||||
show_title TEXT,
|
||||
year INTEGER
|
||||
);
|
||||
CREATE TABLE episodes(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
number INTEGER,
|
||||
season INTEGER
|
||||
);
|
||||
`
|
||||
|
||||
_, err = db.Exec(sqlStmt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not initalize database: %v", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
@@ -127,8 +147,8 @@ func open_database(data_dir string) (*sql.DB, error) {
|
||||
type TVShow struct {
|
||||
XMLName xml.Name `xml:"tvshow"`
|
||||
Title string `xml:"title"`
|
||||
Originaltile string `xml:"originaltitle"`
|
||||
Showtitle string `xml:"showtitle"`
|
||||
OriginalTile string `xml:"originaltitle"`
|
||||
ShowTitle string `xml:"showtitle"`
|
||||
Year int `xml:"year"`
|
||||
}
|
||||
|
||||
@@ -137,7 +157,19 @@ func scan_directory(directory string, db *sql.DB) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not get files in directory \"%v\": %v", directory, err)
|
||||
}
|
||||
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not begin db transaction: %v", err)
|
||||
}
|
||||
defer tx.Commit()
|
||||
|
||||
stmt, err := tx.Prepare("insert into tvshows(title, original_title, show_title, year) values(?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not prepare statement: %v", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
@@ -153,27 +185,33 @@ func scan_directory(directory string, db *sql.DB) error {
|
||||
// try to parse nfo file
|
||||
data, err := ioutil.ReadAll(tvshow_nfo_file)
|
||||
if err != nil {
|
||||
fmt.Errorf("ERROR: Could not read data from nfo file: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "ERROR: Could not read data from nfo file: %v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
var tvshowdata TVShow
|
||||
var tvshow TVShow
|
||||
|
||||
err = xml.Unmarshal(data, &tvshowdata)
|
||||
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", tvshowdata.Title, tvshowdata.Year)
|
||||
fmt.Printf(" Original Title: %v\n", tvshowdata.Originaltile)
|
||||
fmt.Printf(" Show Title: %v\n", tvshowdata.Showtitle)
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
config_dir := get_config_dir()
|
||||
|
||||
Reference in New Issue
Block a user