feat(db, client): split work into separate files and finished database
This commit is contained in:
1 parent
29e94a679d
commit
4bfb5bb19a
6 files changed
+383
-240
No files matched your search
@@ -1,220 +1,6 @@
|
||||
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
|
||||
"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
|
||||
}
|
||||
// Bubbles, bubbletea, lipgloss needed
|
||||
|
||||
func main() {
|
||||
/*
|
||||
@@ -258,36 +44,63 @@ func main() {
|
||||
getPlaylists - lists all playlists
|
||||
getPlaylist - lists all songs in specifc playlist
|
||||
*/
|
||||
_ = godotenv.Load()
|
||||
// sock := "/tmp/mpv.sock"
|
||||
// _, _ = godotenv.Load(), os.Remove(sock)
|
||||
|
||||
client := &NavidromeClient{
|
||||
BaseURL: os.Getenv("URL"),
|
||||
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},
|
||||
}
|
||||
// ctx, cancel := context.WithCancel(context.Background())
|
||||
// defer cancel()
|
||||
|
||||
index, err := client.requestArtists()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// client := &NavidromeClient{
|
||||
// BaseURL: os.Getenv("URL"),
|
||||
// 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 != nil {
|
||||
panic(err)
|
||||
}
|
||||
// if err := db.loadDatabase(client); err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
fileName := "db-index.json"
|
||||
file, err := os.Create(fileName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// fmt.Println(db.LibraryDB[0].Artist[0].Name)
|
||||
// params := url.Values{}
|
||||
// params.Set("id", "38CbfBE5cwLRQ5iHt3iAYN")
|
||||
// stream, err := client.newURLRequest("stream", params)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ")
|
||||
encoder.Encode(index)
|
||||
// cmd := exec.CommandContext(ctx, "mpv",
|
||||
// "--no-video",
|
||||
// "--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)
|
||||
// if err != nil {
|
||||
|
||||
Reference in new issue
Block a user