feat(db, client): split work into separate files and finished database
This commit is contained in:
1 parent
29e94a679d
commit
4bfb5bb19a
6 files changed
+382
-239
No files matched your search
@@ -0,0 +1,69 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"math/big"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NavidromeClient struct {
|
||||||
|
BaseURL string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
APIVersion string
|
||||||
|
ClientName string
|
||||||
|
HTTP *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *NavidromeClient) newSaltKey() (string, error) {
|
||||||
|
n, err := rand.Int(rand.Reader, big.NewInt(900000))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return strconv.Itoa(int(n.Int64() + 100000)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *NavidromeClient) newTokenKey(salt string, password string) string {
|
||||||
|
sum := md5.Sum([]byte(password + salt))
|
||||||
|
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *NavidromeClient) newURLRequest(endpoint string, extras url.Values) (string, error) {
|
||||||
|
salt, err := c.newSaltKey()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
token := c.newTokenKey(salt, c.Password)
|
||||||
|
|
||||||
|
params := url.Values{}
|
||||||
|
params.Set("u", c.Username)
|
||||||
|
params.Set("t", token)
|
||||||
|
params.Set("s", salt)
|
||||||
|
params.Set("v", c.APIVersion)
|
||||||
|
params.Set("c", c.ClientName)
|
||||||
|
params.Set("f", "json")
|
||||||
|
|
||||||
|
for key, value := range extras {
|
||||||
|
for _, val := range value {
|
||||||
|
if val != "" {
|
||||||
|
params.Set(key, val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
base, err := url.Parse(c.BaseURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
base.Path = "/rest/" + endpoint
|
||||||
|
base.RawQuery = params.Encode()
|
||||||
|
|
||||||
|
return base.String(), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -3,3 +3,22 @@ module com.DarthMooMancer.navidrome-tui
|
|||||||
go 1.27.4
|
go 1.27.4
|
||||||
|
|
||||||
require github.com/joho/godotenv v1.5.1
|
require github.com/joho/godotenv v1.5.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
charm.land/bubbletea/v2 v2.1.2 // indirect
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3 // indirect
|
||||||
|
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 // indirect
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.9 // indirect
|
||||||
|
github.com/charmbracelet/x/term v0.2.2 // indirect
|
||||||
|
github.com/charmbracelet/x/termios v0.1.1 // indirect
|
||||||
|
github.com/charmbracelet/x/windows v0.2.2 // indirect
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0 // indirect
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.1 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.30 // indirect
|
||||||
|
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/xo/terminfo v1.0.0 // indirect
|
||||||
|
golang.org/x/sync v0.22.0 // indirect
|
||||||
|
golang.org/x/sys v0.47.0 // indirect
|
||||||
|
)
|
||||||
@@ -1,2 +1,34 @@
|
|||||||
|
charm.land/bubbletea/v2 v2.1.2 h1:vB3XAcHhLB2gR3HOHpwO3lHB3nbvaowIgL3e5x/DUZo=
|
||||||
|
charm.land/bubbletea/v2 v2.1.2/go.mod h1:xWHeyxIX9/wiFU2kgPW04ojOZyTzrWf+8lGjUCaftiU=
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
|
||||||
|
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||||
|
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4 h1:hISLzUbwcvJbGoNR6A3L/TpguRRnACBmZOee8LcHLJw=
|
||||||
|
github.com/charmbracelet/ultraviolet v0.0.0-20261008173134-6b8d4baf91b4/go.mod h1:aJDVk+WOVM1K04rdc/71PMPoUGfjxjhvXfP3Pjz1Zio=
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.9 h1:kIrqLBxtTVbLyyHO2Evoi93EjQo1mylWmpNEsfPxXAM=
|
||||||
|
github.com/charmbracelet/x/ansi v0.11.9/go.mod h1:DG/hpOnaI2QJ/KMMqLL2h0KVhluuJLazf8973P+Vy5w=
|
||||||
|
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
||||||
|
github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
|
||||||
|
github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY=
|
||||||
|
github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo=
|
||||||
|
github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM=
|
||||||
|
github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k=
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
|
||||||
|
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||||
|
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.1 h1:1EO+WB73+EH8EVbzlrG3KLAfEypQWVHIBqlTf+2hNss=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.4.1/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
github.com/mattn/go-runewidth v0.0.30 h1:+KUuiDA4fF0R1p5FeueHefjDm+GIM+kWfFnDjybOPgk=
|
||||||
|
github.com/mattn/go-runewidth v0.0.30/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8=
|
||||||
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/xo/terminfo v1.0.0 h1:2ZpYzqWzyyytjk3TP6aJVDhkMAkc99/1xKQdA3TDTBY=
|
||||||
|
github.com/xo/terminfo v1.0.0/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
|
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||||
|
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
|
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||||
|
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
@@ -1,220 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/md5"
|
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
// Bubbles, bubbletea, lipgloss needed
|
// Bubbles, bubbletea, lipgloss needed
|
||||||
"github.com/joho/godotenv"
|
|
||||||
)
|
|
||||||
|
|
||||||
type NavidromeClient struct {
|
|
||||||
BaseURL string
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
APIVersion string
|
|
||||||
ClientName string
|
|
||||||
HTTP *http.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NavidromeClient) newSaltKey() (string, error) {
|
|
||||||
n, err := rand.Int(rand.Reader, big.NewInt(900000))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return strconv.Itoa(int(n.Int64() + 100000)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NavidromeClient) newTokenKey(salt string, password string) string {
|
|
||||||
sum := md5.Sum([]byte(password + salt))
|
|
||||||
|
|
||||||
return hex.EncodeToString(sum[:])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NavidromeClient) newURLRequest(endpoint string, extras url.Values) (string, error) {
|
|
||||||
salt, err := c.newSaltKey()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
token := c.newTokenKey(salt, c.Password)
|
|
||||||
|
|
||||||
params := url.Values{}
|
|
||||||
params.Set("u", c.Username)
|
|
||||||
params.Set("t", token)
|
|
||||||
params.Set("s", salt)
|
|
||||||
params.Set("v", c.APIVersion)
|
|
||||||
params.Set("c", c.ClientName)
|
|
||||||
params.Set("f", "json")
|
|
||||||
|
|
||||||
for key, value := range extras {
|
|
||||||
for _, val := range value {
|
|
||||||
if val != "" {
|
|
||||||
params.Set(key, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
base, err := url.Parse(c.BaseURL)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
base.Path = "/rest/" + endpoint
|
|
||||||
base.RawQuery = params.Encode()
|
|
||||||
|
|
||||||
return base.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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"`
|
|
||||||
}
|
|
||||||
|
|
||||||
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) fillAlbums(client *NavidromeClient) error {
|
|
||||||
if a.Albums == nil {
|
|
||||||
return fmt.Errorf("err: run after requestAlbums")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range a.Albums {
|
|
||||||
a.Albums[i].requestSongs(client)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Artist) requestAlbums(client *NavidromeClient) {
|
|
||||||
params := url.Values{}
|
|
||||||
params.Set("id", a.ID)
|
|
||||||
|
|
||||||
url, err := client.newURLRequest("getArtist", 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 {
|
|
||||||
Artist struct {
|
|
||||||
Albums []Album `json:"album"`
|
|
||||||
} `json:"artist"`
|
|
||||||
} `json:"subsonic-response"`
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
|
||||||
if err := decoder.Decode(&env); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
a.Albums = env.SubsonicResponse.Artist.Albums
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NavidromeClient) requestArtists() ([]Index, error) {
|
|
||||||
url, err := c.newURLRequest("getArtists", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := c.HTTP.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
var env struct {
|
|
||||||
SubsonicResponse struct {
|
|
||||||
Artists struct {
|
|
||||||
Index []Index `json:"index"`
|
|
||||||
} `json:"artists"`
|
|
||||||
} `json:"subsonic-response"`
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
|
||||||
if err := decoder.Decode(&env); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return env.SubsonicResponse.Artists.Index, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *NavidromeClient) fillArtists(index []Index) error {
|
|
||||||
if index == nil {
|
|
||||||
return fmt.Errorf("err: run after requesting artists")
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range index {
|
|
||||||
for j := range index[i].Artist {
|
|
||||||
index[i].Artist[j].requestAlbums(c)
|
|
||||||
err := index[i].Artist[j].fillAlbums(c)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
/*
|
/*
|
||||||
@@ -258,36 +44,63 @@ func main() {
|
|||||||
getPlaylists - lists all playlists
|
getPlaylists - lists all playlists
|
||||||
getPlaylist - lists all songs in specifc playlist
|
getPlaylist - lists all songs in specifc playlist
|
||||||
*/
|
*/
|
||||||
_ = godotenv.Load()
|
// sock := "/tmp/mpv.sock"
|
||||||
|
// _, _ = godotenv.Load(), os.Remove(sock)
|
||||||
|
|
||||||
client := &NavidromeClient{
|
// ctx, cancel := context.WithCancel(context.Background())
|
||||||
BaseURL: os.Getenv("URL"),
|
// defer cancel()
|
||||||
Username: os.Getenv("USERNAME"),
|
|
||||||
Password: os.Getenv("PASSWORD"),
|
|
||||||
APIVersion: os.Getenv("SUBSONIC_API_VERSION"),
|
|
||||||
ClientName: os.Getenv("CLIENT_NAME"),
|
|
||||||
HTTP: &http.Client{Timeout: 10 * time.Second},
|
|
||||||
}
|
|
||||||
|
|
||||||
index, err := client.requestArtists()
|
// client := &NavidromeClient{
|
||||||
if err != nil {
|
// BaseURL: os.Getenv("URL"),
|
||||||
panic(err)
|
// Username: os.Getenv("USERNAME"),
|
||||||
}
|
// Password: os.Getenv("PASSWORD"),
|
||||||
|
// APIVersion: os.Getenv("SUBSONIC_API_VERSION"),
|
||||||
|
// ClientName: os.Getenv("CLIENT_NAME"),
|
||||||
|
// HTTP: &http.Client{Timeout: 10 * time.Second},
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// db := &Database{
|
||||||
|
// LibraryPath: "db-library.json",
|
||||||
|
// LibraryDB: nil,
|
||||||
|
// }
|
||||||
|
|
||||||
err = client.fillArtists(index)
|
// if err := db.loadDatabase(client); err != nil {
|
||||||
if err != nil {
|
// panic(err)
|
||||||
panic(err)
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
fileName := "db-index.json"
|
// fmt.Println(db.LibraryDB[0].Artist[0].Name)
|
||||||
file, err := os.Create(fileName)
|
// params := url.Values{}
|
||||||
if err != nil {
|
// params.Set("id", "38CbfBE5cwLRQ5iHt3iAYN")
|
||||||
panic(err)
|
// stream, err := client.newURLRequest("stream", params)
|
||||||
}
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
|
||||||
encoder := json.NewEncoder(file)
|
// cmd := exec.CommandContext(ctx, "mpv",
|
||||||
encoder.SetIndent("", " ")
|
// "--no-video",
|
||||||
encoder.Encode(index)
|
// "--idle=yes",
|
||||||
|
// "--no-terminal",
|
||||||
|
// "--ytdl=no",
|
||||||
|
// "--input-ipc-server="+sock,
|
||||||
|
// )
|
||||||
|
// if err := cmd.Start(); err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fmt.Println(stream)
|
||||||
|
|
||||||
|
// if err := db.requestDatabase(client); err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// file, err := os.Create(db.LibraryPath)
|
||||||
|
// if err != nil {
|
||||||
|
// panic(err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// encoder := json.NewEncoder(file)
|
||||||
|
// encoder.SetIndent("", " ")
|
||||||
|
// encoder.Encode(db.LibraryDB)
|
||||||
|
|
||||||
// resp, err := client.HTTP.Get(outURL)
|
// resp, err := client.HTTP.Get(outURL)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"subsonic-response": {
|
||||||
|
"artists": {
|
||||||
|
"index": [
|
||||||
|
{
|
||||||
|
"name": "#",
|
||||||
|
"artist": [
|
||||||
|
{
|
||||||
|
"id": "1ZHez3tEDSdaETpth5Q8Lm",
|
||||||
|
"name": "2Pac",
|
||||||
|
"coverArt": "ar-1ZHez3tEDSdaETpth5Q8Lm_a56f9f91971022fa",
|
||||||
|
"albumCount": 4,
|
||||||
|
"artistImageUrl": "https://music.moomancer.xyz/share/img/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFyLTFaSGV6M3RFRFNkYUVUcHRoNVE4TG1fYTU2ZjlmOTE5NzEwMjJmYSIsImlzcyI6Ik5EIn0.lkmFe-8Vu7veiCcYdPpW0o1l9dxEP0jeaGtcM1I4m28?size=600",
|
||||||
|
"musicBrainzId": "",
|
||||||
|
"sortName": "2pac",
|
||||||
|
"roles": [
|
||||||
|
"maincredit",
|
||||||
|
"albumartist",
|
||||||
|
"artist",
|
||||||
|
"composer",
|
||||||
|
"lyricist",
|
||||||
|
"producer"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user