185 lines
5.0 KiB
Go
185 lines
5.0 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
type TVShow struct {
|
|
XMLName xml.Name `xml:"tvshow" json:"-"`
|
|
Title string `xml:"title"`
|
|
OriginalTile string `xml:"originaltitle"`
|
|
ShowTitle string `xml:"showtitle"`
|
|
Year int `xml:"year"`
|
|
Description string `xml:"plot"`
|
|
Thumb []Thumb `xml:"thumb"`
|
|
}
|
|
|
|
type Thumb struct {
|
|
Aspect string `xml:"aspect,attr"`
|
|
Season string `xml:"season,attr"`
|
|
Type string `xml:"type,attr"`
|
|
Value string `xml:",chardata"`
|
|
}
|
|
|
|
func ScanTvshowRoot(root string, db *sql.DB) error {
|
|
log.Noticef("Scanning directory root %v", root)
|
|
|
|
files, err := ioutil.ReadDir(root)
|
|
if err != nil {
|
|
return fmt.Errorf("Could not get files in directory \"%v\": %v", root, 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, description, poster) values(?, ?, ?, ?, ?, ?)")
|
|
if err != nil {
|
|
return fmt.Errorf("Could not prepare statement: %v", err)
|
|
}
|
|
defer stmt.Close()
|
|
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
show_path := filepath.Join(root, file.Name())
|
|
if insert_tvshow_nfo(show_path, stmt) {
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func insert_tvshow_nfo(show_dir string, stmt *sql.Stmt) bool {
|
|
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 {
|
|
log.Debugf("Could not open tvshow.nfo file in \"%v\"\n", show_dir)
|
|
return false
|
|
}
|
|
defer tvshow_nfo_file.Close()
|
|
|
|
// try to parse nfo file
|
|
data, err := ioutil.ReadAll(tvshow_nfo_file)
|
|
if err != nil {
|
|
log.Debugf("Could not read data from tvshow.nfo file: %v\n", err)
|
|
return false
|
|
}
|
|
var tvshow TVShow
|
|
|
|
err = xml.Unmarshal(data, &tvshow)
|
|
if err != nil {
|
|
log.Debugf("Could not parse tvshow.nfo file contents: %v\n", err)
|
|
return false
|
|
}
|
|
|
|
// TODO: Replace with print formatting logic
|
|
log.Debugf("%v (%v):\n", tvshow.Title, tvshow.Year)
|
|
log.Debugf(" Original Title: %v\n", tvshow.OriginalTile)
|
|
log.Debugf(" Show Title: %v\n", tvshow.ShowTitle)
|
|
|
|
_, err = stmt.Exec(tvshow.Title, tvshow.OriginalTile, tvshow.ShowTitle, tvshow.Year, tvshow.Description, tvshow.Thumb[0].Value)
|
|
if err != nil {
|
|
log.Debugf("Could not insert tvshow \"%v\" into database: %v\n", tvshow.Title, err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
type Episode struct {
|
|
XMLName xml.Name `xml:"episodedetails" json:"-"`
|
|
Title string `xml:"title"`
|
|
ShowTitle string `xml:"showtitle"`
|
|
Season int `xml:"season"`
|
|
Episode int `xml:"episode"`
|
|
OriginalFilename string `xml:"original_filename"`
|
|
}
|
|
|
|
func scan_tvshow_episodes(show_dir string, tx *sql.Tx) error {
|
|
|
|
log.Infof("Scanning episodes for show %v\n", show_dir)
|
|
|
|
files, err := ioutil.ReadDir(show_dir)
|
|
if err != nil {
|
|
return fmt.Errorf("Could not enumerate files in folder: %v", err)
|
|
}
|
|
|
|
num_episodes := 0
|
|
|
|
stmt, err := tx.Prepare("insert into episodes(title, show_title, number, season, original_filename) 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())
|
|
|
|
if file.Name() == "tvshow.nfo" {
|
|
continue
|
|
} else if ext == ".nfo" {
|
|
log.Debugf("Found nfo file: \"%v\"\n", file.Name())
|
|
} else {
|
|
log.Debugf("Skipping file \"%v\", not an nfo file\n", file.Name())
|
|
continue
|
|
}
|
|
|
|
log.Debugf("%v\n", file.Name())
|
|
|
|
nfo_file, err := os.OpenFile(nfo_path, os.O_RDONLY, 0755)
|
|
if err != nil {
|
|
log.Debugf("Could not open nfo file \"%v\": %v", nfo_path, err)
|
|
continue
|
|
}
|
|
defer nfo_file.Close()
|
|
|
|
// try to parse nfo file
|
|
data, err := ioutil.ReadAll(nfo_file)
|
|
if err != nil {
|
|
log.Debugf("Could not read data from nfo file \"%v\": %v\n", nfo_path, err)
|
|
continue
|
|
}
|
|
var episode Episode
|
|
|
|
err = xml.Unmarshal(data, &episode)
|
|
if err != nil {
|
|
log.Debugf("Could not parse contents of nfo file \"%v\": %v\n", nfo_path, err)
|
|
continue
|
|
}
|
|
|
|
log.Debugf(" Title: %v\n", episode.Title)
|
|
log.Debugf(" Season %v\n", episode.Season)
|
|
log.Debugf(" Episode %v\n", episode.Episode)
|
|
log.Debugf(" Show: %v\n", episode.ShowTitle)
|
|
log.Debugf(" Original Filename: %v\n", episode.OriginalFilename)
|
|
|
|
full_original_filename := filepath.Join(show_dir, episode.OriginalFilename)
|
|
|
|
_, err = stmt.Exec(episode.Title, episode.ShowTitle, episode.Episode, episode.Season, full_original_filename)
|
|
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++
|
|
|
|
}
|
|
|
|
log.Infof("Found %v episodes\n", num_episodes)
|
|
|
|
return nil
|
|
}
|