Files
server/main.go
T

207 lines
4.8 KiB
Go

package main
import (
"database/sql"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
toml "github.com/pelletier/go-toml/v2"
)
type Config struct {
Address string
Port int
ScanDirectory string
//Age int
//Cats []string
//Pi float64
//Perfection []int
//DOB time.Time // requires `import time`
}
func get_config_dir() string {
// Find config directory
// Linux (XDG base directory specification compliant)
xdg_config_home := os.Getenv("XDG_CONFIG_HOME")
if xdg_config_home != "" {
return filepath.Join(xdg_config_home, "ikinuki")
} else {
home := os.Getenv("HOME")
return filepath.Join(home, ".config", "ikinuki")
}
}
func get_config(config_dir string) (*Config, error) {
config := Config{
Address: "127.0.0.1",
Port: 32520,
ScanDirectory: "FILL IN",
}
err := os.MkdirAll(config_dir, 0755)
if err != nil {
return &config, fmt.Errorf("Could not create config directory: %v", err)
}
config_path := filepath.Join(config_dir, "server.toml")
fmt.Printf("Config File Dir: %v\n", config_path)
// open file, creating it if empty
config_file, err := os.OpenFile(config_path, os.O_RDWR|os.O_CREATE, 0755)
defer config_file.Close()
if err != nil {
return &config, fmt.Errorf("Could not open config file: %v", err)
}
fi, err := config_file.Stat()
if err != nil {
return &config, fmt.Errorf("Could not get config file size: %v", err)
}
if fi.Size() == 0 {
// if file is empty, write default config
data, err := toml.Marshal(config)
if err != nil {
return &config, fmt.Errorf("Could not write default config to file: %v", err)
}
config_file.Write(data)
// return nil as the user must edit the config file
return nil, nil
} else {
// try to parse config file
data, err := ioutil.ReadAll(config_file)
if err != nil {
return &config, fmt.Errorf("Could not read data from config file: %v", err)
}
err = toml.Unmarshal(data, &config)
if err != nil {
return &config, fmt.Errorf("Could not parse config file contents: %v", err)
}
return &config, nil
}
}
func get_data_dir() string {
// Find data directory
// Linux (XDG base directory specification compliant)
xdg_data_home := os.Getenv("XDG_DATA_HOME")
if xdg_data_home != "" {
return filepath.Join(xdg_data_home, "ikinuki")
} else {
home := os.Getenv("HOME")
return filepath.Join(home, ".local", "share", "ikinuki")
}
}
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("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)
if err != nil {
return nil, fmt.Errorf("Could not open database: %v", err)
}
}
return db, nil
}
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 {
return fmt.Errorf("Could not get files in directory \"%v\": %v", directory, err)
}
}
for _, file := range files {
if file.IsDir() {
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() {
config_dir := get_config_dir()
config, err := get_config(config_dir)
if err != nil {
panic(err)
}
if config == nil {
fmt.Printf("Created config file at %v, please edit and launch again\n", config_dir)
os.Exit(0)
}
fmt.Printf("Config: %v\n", config)
data_dir := get_data_dir()
db, err := open_database(data_dir)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: Could not open database: %v", err)
os.Exit(1)
}
defer db.Close()
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)
}
}