From 96c5ed082906126b61df4af933a5d67bb3380abe Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 19 Nov 2026 23:52:58 -0500 Subject: [PATCH] feat(api-calling): pulls information from server using REST API from Subsonic --- .env-example | 6 +++ go.mod | 2 + go.sum | 4 ++ main.go | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 .env-example diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..446174f --- /dev/null +++ b/.env-example @@ -0,0 +1,6 @@ +SUBSONIC_API_VERSION=1.16.1 +CLIENT_NAME=Navidrome-tui + +URL=https://demo.navidrome.org +USERNAME=demo +PASSWORD=demo diff --git a/go.mod b/go.mod index 5492885..a18ef51 100644 --- a/go.mod +++ b/go.mod @@ -14,8 +14,10 @@ require ( 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/joho/godotenv v1.5.1 // indirect github.com/lucasb-eyer/go-colorful v1.4.1 // indirect github.com/mattn/go-runewidth v0.0.30 // indirect + github.com/mattn/go-sqlite3 v1.14.53 // 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 diff --git a/go.sum b/go.sum index d7a4b41..4209647 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE 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/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.24 h1:cpokDiIn0MGnhdHwuWnJBITySJ20QyNGnY2kR/ay2DU= @@ -32,6 +34,8 @@ github.com/mattn/go-runewidth v0.0.27 h1:Feg/Oou5zI/wnpgDF6omIU0OokC9GxLC/WRknhV github.com/mattn/go-runewidth v0.0.27/go.mod h1:3qAiGCV4Koz/yuveO58qUefmUTRm8r0IGEXZ9jeHp/8= 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/mattn/go-sqlite3 v1.14.53 h1:mnTM0owzUMrKuLo+0w51NUiB0ir2Fo7kFXlPt/l3BGs= +github.com/mattn/go-sqlite3 v1.14.53/go.mod h1:6JTjA44L93a0QCyJef5YvlPoKXntQPjzWv5gtm9sB6w= 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= diff --git a/main.go b/main.go index 8cd8b97..034d335 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,146 @@ package main -import ("fmt") +import ( + "crypto/md5" + "crypto/rand" + "encoding/hex" + "fmt" + "io" + "log" + "math/big" + "net/http" + "net/url" + "os" + "strconv" + "time" + + "github.com/joho/godotenv" +) + +type NavidromeClient struct { + BaseURL string + Username string + Password string + APIVersion string + ClientName string + HTTP *http.Client +} + +func 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 newTokenKey(salt string, password string) string { + sum := md5.Sum([]byte(password + salt)) + + return hex.EncodeToString(sum[:]) +} + +func (c *NavidromeClient) newRequestURL(endpoint string, extras url.Values) (string, error) { + salt, err := newSaltKey() + if err != nil { + return "", err + } + token := 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 +} func main() { - fmt.Println("Hello World") + // 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 + + 1. Mpv for audio backend + 2. sqlite for database to store all information from server locally once and can resync + 3. tui + + block 4 and 5 in the ui update from the mpv and server constantly where 1, 2, 3 are static from db + */ + _ = 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}, + } + + // pingURL, err := client.newRequestURL("getUsers", nil) + // if err != nil { + // log.Fatalf("err: request failed %s", err.Error()) + // } + // fmt.Printf("PingURL: %s\n", pingURL) + + playlistsURL, err := client.newRequestURL("getPlaylists", nil) + if err != nil { + log.Fatalf("err: request failed %s", err.Error()) + } + fmt.Printf("playlistsURL: %s\n", playlistsURL) + + resp, err := client.HTTP.Get(playlistsURL) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + panic(err) + } + + fmt.Println(string(body)) }