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
This commit is contained in:
1 parent
96c5ed0829
commit
29e94a679d
4 files changed
+215
-124
No files matched your search
@@ -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))
|
||||
}
|
||||
Reference in new issue
Block a user