From 29e94a679d2c549b1853270cce513e0cc895e57c Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Sat, 21 Nov 2026 00:06:56 -0500 Subject: [PATCH] feat(database): Saves entire library to a local db-index.json file for an organized list of all songs. Can be easily decoded into structs. Also updated the plan board --- .gitignore | 4 +- go.mod | 23 +---- go.sum | 48 ---------- main.go | 264 ++++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 215 insertions(+), 124 deletions(-) diff --git a/.gitignore b/.gitignore index 38f9e2e..bb609a4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ .env .env.* -bin/ \ No newline at end of file +db-* + +bin/ diff --git a/go.mod b/go.mod index a18ef51..a43211f 100644 --- a/go.mod +++ b/go.mod @@ -2,25 +2,4 @@ module com.DarthMooMancer.navidrome-tui go 1.27.4 -require ( - charm.land/bubbles/v2 v2.2.1 // indirect - charm.land/bubbletea/v2 v2.1.2 // indirect - charm.land/lipgloss/v2 v2.1.1 // 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/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 - golang.org/x/sync v0.22.0 // indirect - golang.org/x/sys v0.47.0 // indirect -) +require github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index 4209647..d61b19e 100644 --- a/go.sum +++ b/go.sum @@ -1,50 +1,2 @@ -charm.land/bubbles/v2 v2.2.1 h1:Fq1+qm5hV6GkvzLQDhCBpXXE5tLgvh1PRriCLwSvIQU= -charm.land/bubbles/v2 v2.2.1/go.mod h1:wdMgn+sje1KNXdwFizIWjbf328fIUBxqEmJ/vYPo8yc= -charm.land/bubbletea/v2 v2.1.2 h1:vB3XAcHhLB2gR3HOHpwO3lHB3nbvaowIgL3e5x/DUZo= -charm.land/bubbletea/v2 v2.1.2/go.mod h1:xWHeyxIX9/wiFU2kgPW04ojOZyTzrWf+8lGjUCaftiU= -charm.land/lipgloss/v2 v2.1.1 h1:F+vKWis9Kz3MFRsbICSDFtT1ZwVL+8f+36Dtg+OpKwc= -charm.land/lipgloss/v2 v2.1.1/go.mod h1:k79y/56yThpkJvbwyrcHr8MyZF42e84Q5fW9Ibu6LFc= -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-20260811164956-006e29f97886 h1:rdnVWKgJpTVXKuKuJyxDJ+NFJdUaUqGvyGy61OcvlbA= -github.com/charmbracelet/ultraviolet v0.0.0-20260811164956-006e29f97886/go.mod h1:nAw0d9PhFp1qdzi2xhQU5YOu5sVpDIHWlaW2Uz/bCro= -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.8 h1:JMFwp0CgDC2+jcOB162HH5k7I3FVbgFSMMYg7dSPBQQ= -github.com/charmbracelet/x/ansi v0.11.8/go.mod h1:ZNN+3mXny/516oTQPLMPIBeSINvNJJQ8uQXDgbeJxY0= -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/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= -github.com/mattn/go-runewidth v0.0.24/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= -github.com/mattn/go-runewidth v0.0.27 h1:Feg/Oou5zI/wnpgDF6omIU0OokC9GxLC/WRknhVlIR0= -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= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= -github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= -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= diff --git a/main.go b/main.go index 034d335..b2dcd09 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,8 @@ import ( "crypto/md5" "crypto/rand" "encoding/hex" + "encoding/json" "fmt" - "io" - "log" "math/big" "net/http" "net/url" @@ -14,6 +13,7 @@ import ( "strconv" "time" + // Bubbles, bubbletea, lipgloss needed "github.com/joho/godotenv" ) @@ -26,7 +26,7 @@ type NavidromeClient struct { HTTP *http.Client } -func newSaltKey() (string, error) { +func (c *NavidromeClient) newSaltKey() (string, error) { n, err := rand.Int(rand.Reader, big.NewInt(900000)) if err != nil { return "", err @@ -35,18 +35,18 @@ func newSaltKey() (string, error) { return strconv.Itoa(int(n.Int64() + 100000)), nil } -func newTokenKey(salt string, password string) string { +func (c *NavidromeClient) 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() +func (c *NavidromeClient) newURLRequest(endpoint string, extras url.Values) (string, error) { + salt, err := c.newSaltKey() if err != nil { return "", err } - token := newTokenKey(salt, c.Password) + token := c.newTokenKey(salt, c.Password) params := url.Values{} params.Set("u", c.Username) @@ -75,38 +75,188 @@ func (c *NavidromeClient) newRequestURL(endpoint string, extras url.Values) (str 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. + /* + 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 + -------------------- + 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 | - +------------------------------------------------------------------------+ + +--------------------+----------------------+----------------------------+ + | [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 - /* 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 - 1. Mpv for audio backend - 2. sqlite for database to store all information from server locally once and can resync - 3. tui + ----------------------- + Endpoints + ----------------------- - block 4 and 5 in the ui update from the mpv and server constantly where 1, 2, 3 are static from db + getPlaylists - lists all playlists + getPlaylist - lists all songs in specifc playlist */ _ = godotenv.Load() @@ -119,28 +269,36 @@ func main() { HTTP: &http.Client{Timeout: 10 * time.Second}, } - // pingURL, err := client.newRequestURL("getUsers", nil) + 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 { - // log.Fatalf("err: request failed %s", err.Error()) + // panic(err) + // } + // defer resp.Body.Close() + // + // body, err := io.ReadAll(resp.Body) + // if err != nil { + // panic(err) // } - // 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)) + // fmt.Println(string(body)) }