Added tvinfo.nfo xml parsing logic

This commit is contained in:
2022-04-13 19:21:53 -06:00
parent 985a7bf005
commit 4895f94d81
+50 -4
View File
@@ -2,6 +2,7 @@ package main
import (
"database/sql"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
@@ -105,11 +106,13 @@ func get_data_dir() string {
}
func open_database(data_dir string) (*sql.DB, error) {
// create data directory
err := os.MkdirAll(data_dir, 0755)
if err != nil {
return nil, fmt.Errorf("ERROR: could not create data directory: %v", err)
}
// open database
db_path := filepath.Join(data_dir, "ikinuki-server.db")
db, err := sql.Open("sqlite3", db_path)
@@ -120,7 +123,15 @@ func open_database(data_dir string) (*sql.DB, error) {
return db, nil
}
func scan_directory(directory string) {
type TVShow struct {
XMLName xml.Name `xml:"tvshow"`
Title string `xml:"title"`
Originaltile string `xml:"originaltitle"`
Showtitle string `xml:"showtitle"`
Year int `xml:"year"`
}
func scan_directory(directory string, db *sql.DB) error {
files, err := ioutil.ReadDir(directory)
if err != nil {
panic(err)
@@ -128,10 +139,37 @@ func scan_directory(directory string) {
for _, file := range files {
if file.IsDir() {
fmt.Printf("%v\n", file.Name())
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.Errorf("ERROR: Could not read data from nfo file: %v\n", err)
continue
}
var tvshowdata TVShow
err = xml.Unmarshal(data, &tvshowdata)
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)
}
}
return nil
}
func main() {
@@ -150,8 +188,16 @@ func main() {
data_dir := get_data_dir()
open_database(data_dir)
db, err := open_database(data_dir)
if err != nil {
os.Exit(1)
}
defer db.Close()
scan_directory(config.ScanDirectory)
err = scan_directory(config.ScanDirectory, db)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not scan directory \"%v\": %v\n", config.ScanDirectory, err)
os.Exit(1)
}
}