feat(api-calling): pulls information from server using REST API from Subsonic
This commit is contained in:
1 parent
1d26320883
commit
96c5ed0829
4 files changed
+153
-2
No files matched your search
@@ -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))
|
||||
}
|
||||
Reference in new issue
Block a user