181 lines
3.4 KiB
Go
181 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
)
|
|
|
|
type Song struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
Duration int `json:"duration"`
|
|
TrackNumber int `json:"track"`
|
|
DiscNumber int `json:"discNumber"`
|
|
}
|
|
|
|
type Album struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Year int `json:"year"`
|
|
Songs []Song `json:"songs,omitempty"`
|
|
}
|
|
|
|
type Artist struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Albums []Album `json:"albums,omitempty"`
|
|
}
|
|
|
|
type Index struct {
|
|
Name string `json:"name"`
|
|
Artist []Artist `json:"artist,omitempty"`
|
|
}
|
|
|
|
type Database struct {
|
|
LibraryDB []Index
|
|
LibraryPath string
|
|
// PlaylistDB []Index
|
|
// PlaylistPath string
|
|
}
|
|
|
|
func (a *Album) requestSongs(client *NavidromeClient) {
|
|
params := url.Values{}
|
|
params.Set("id", a.ID)
|
|
|
|
url, err := client.newURLRequest("getAlbum", params)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
resp, err := client.HTTP.Get(url)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var env struct {
|
|
SubsonicResponse struct {
|
|
Album struct {
|
|
Songs []Song `json:"song"`
|
|
} `json:"album"`
|
|
} `json:"subsonic-response"`
|
|
}
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
if err := decoder.Decode(&env); err != nil {
|
|
panic(err)
|
|
}
|
|
a.Songs = env.SubsonicResponse.Album.Songs
|
|
}
|
|
|
|
func (a *Artist) requestAlbums(client *NavidromeClient) error {
|
|
params := url.Values{}
|
|
params.Set("id", a.ID)
|
|
|
|
url, err := client.newURLRequest("getArtist", params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := client.HTTP.Get(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var env struct {
|
|
SubsonicResponse struct {
|
|
Artist struct {
|
|
Albums []Album `json:"album"`
|
|
} `json:"artist"`
|
|
} `json:"subsonic-response"`
|
|
}
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
if err := decoder.Decode(&env); err != nil {
|
|
return err
|
|
}
|
|
a.Albums = env.SubsonicResponse.Artist.Albums
|
|
for i := range a.Albums {
|
|
a.Albums[i].requestSongs(client)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *Database) requestDatabase(c *NavidromeClient) error {
|
|
url, err := c.newURLRequest("getArtists", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := c.HTTP.Get(url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var env struct {
|
|
SubsonicResponse struct {
|
|
Artists struct {
|
|
Index []Index `json:"index"`
|
|
} `json:"artists"`
|
|
} `json:"subsonic-response"`
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
|
return err
|
|
}
|
|
|
|
index := env.SubsonicResponse.Artists.Index
|
|
for i := range index {
|
|
for j := range index[i].Artist {
|
|
if err := index[i].Artist[j].requestAlbums(c); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
d.LibraryDB = index
|
|
return nil
|
|
}
|
|
|
|
func (d *Database) checkForDatabase(c *NavidromeClient) error {
|
|
if _, err := os.Stat(d.LibraryPath); errors.Is(err, os.ErrNotExist) {
|
|
if err := d.requestDatabase(c); err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := os.Create(d.LibraryPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", " ")
|
|
encoder.Encode(d.LibraryDB)
|
|
return nil
|
|
}
|
|
fmt.Println("Database already exists")
|
|
return nil
|
|
}
|
|
|
|
func (d *Database) loadDatabase(c *NavidromeClient) error {
|
|
err := d.checkForDatabase(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := os.Open(d.LibraryPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
decoder := json.NewDecoder(file)
|
|
decoder.DisallowUnknownFields()
|
|
decoder.Decode(&d.LibraryDB)
|
|
return nil
|
|
}
|