feat(api-calling): pulls information from server using REST API from Subsonic

This commit is contained in:
DarthMooMancer committed 2026-11-19 23:52:58 -05:00
1 parent 1d26320883
commit 96c5ed0829
4 files changed
+153 -2

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
SUBSONIC_API_VERSION=1.16.1
CLIENT_NAME=Navidrome-tui
URL=https://demo.navidrome.org
USERNAME=demo
PASSWORD=demo
+2
View File
@@ -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
+4
View File
@@ -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=
+141 -2
View File
@@ -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))
}