an organized list of all songs. Can be easily decoded into structs. Also updated the plan board
305 lines
7.1 KiB
Go
305 lines
7.1 KiB
Go
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
|
|
}
|
|
|
|
func main() {
|
|
/*
|
|
Check mpv because i think it sends urls to it from the backend and mpv gets the song and handles the song part
|
|
Also the ui should be cached to a local db file without hosting something like redis to get the entire song list by artist -> ablum -> song
|
|
The ui will format the file properly to be a column style where the songs in an album are listed under in a grid.
|
|
|
|
--------------------
|
|
Optimized Horizontal Concept
|
|
--------------------
|
|
|
|
+--------------------+----------------------+----------------------------+
|
|
| [1] Artists | [2] Albums | Now Playing |
|
|
| > Artist A | > Album 1 (2024) | Track Name |
|
|
| Artist B | Album 2 (2021) | Artist - Album |
|
|
| Artist C | | [=========>-----] 02:14 |
|
|
| Artist D |----------------------| FLAC 24-bit/96kHz |
|
|
| Artist E | [3] Songs +----------------------------+
|
|
| Artist F | > 01. Intro 01:45 | [4] Queue (Up Next) |
|
|
| Artist G | 02. Track A 03:22 | 01. Track A |
|
|
| Artist H | 03. Track B 04:10 | > 02. Track B (Playing) |
|
|
| Artist I | 04. Track C 02:58 | 03. Track C |
|
|
| Artist J | 05. Outro 05:12 | 04. Other Song |
|
|
+--------------------+----------------------+----------------------------+
|
|
| [>] Playing | Vol: 85% | [S]huffle: Off | [R]epeat: All | /: Search |
|
|
+------------------------------------------------------------------------+
|
|
|
|
-----------------------
|
|
Breaking into parts
|
|
-----------------------
|
|
block 4 and 5 in the ui update from the mpv and server constantly where 1, 2, 3 are static from db
|
|
|
|
1. Mpv for audio backend
|
|
2. sqlite for database to store all information from server locally once and can resync
|
|
3. tui
|
|
|
|
-----------------------
|
|
Endpoints
|
|
-----------------------
|
|
|
|
getPlaylists - lists all playlists
|
|
getPlaylist - lists all songs in specifc playlist
|
|
*/
|
|
_ = godotenv.Load()
|
|
|
|
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},
|
|
}
|
|
|
|
index, err := client.requestArtists()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = client.fillArtists(index)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fileName := "db-index.json"
|
|
file, err := os.Create(fileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
encoder := json.NewEncoder(file)
|
|
encoder.SetIndent("", " ")
|
|
encoder.Encode(index)
|
|
|
|
// resp, err := client.HTTP.Get(outURL)
|
|
// if err != nil {
|
|
// panic(err)
|
|
// }
|
|
// defer resp.Body.Close()
|
|
//
|
|
// body, err := io.ReadAll(resp.Body)
|
|
// if err != nil {
|
|
// panic(err)
|
|
// }
|
|
|
|
// fmt.Println(string(body))
|
|
}
|