sync playlists between spotify and navidrome
This commit is contained in:
@@ -28,6 +28,7 @@ type NavidromePlaylist struct {
|
||||
}
|
||||
|
||||
type NavidromeTrack struct {
|
||||
ID string `json:"id"` // position ID within the playlist or id of the song. See MediaFileID
|
||||
Title string `json:"title"`
|
||||
Artist string `json:"artist"`
|
||||
ArtistID string `json:"artistId"`
|
||||
@@ -65,7 +66,6 @@ type NavidromeTrack struct {
|
||||
type NavidromePlaylistTrack struct {
|
||||
NavidromeTrack
|
||||
|
||||
ID string `json:"id"` // position ID within the playlist
|
||||
MediaFileID string `json:"mediaFileId"` // the actual song ID
|
||||
}
|
||||
|
||||
@@ -168,6 +168,25 @@ func (c *NavidromeClient) getPlaylistTracks(playlistID string) ([]NavidromePlayl
|
||||
return tracks, nil
|
||||
}
|
||||
|
||||
func (c *NavidromeClient) GetSongBySpotifyID(spotifyID string) ([]NavidromeTrack, error) {
|
||||
// https://navi.kapelle.org/api/song?_end=100&_order=ASC&_sort=title&_start=0&spotifyid=0NhcaHA0cyJyhk3g8tUsZP&missing=false
|
||||
params := url.Values{
|
||||
"spotifyid": {spotifyID},
|
||||
}
|
||||
var songs []NavidromeTrack
|
||||
err := c.getList("/api/song", params, &songs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return songs, nil
|
||||
}
|
||||
|
||||
func (c *NavidromeClient) AddTracks(playlistID string, songIDs []string) error {
|
||||
body := map[string]interface{}{"ids": songIDs}
|
||||
return c.request(http.MethodPost, "/api/playlist/"+playlistID+"/tracks", body, nil)
|
||||
}
|
||||
|
||||
func (c *NavidromeClient) getList(path string, extra url.Values, dest any) error {
|
||||
const pageSize = 500
|
||||
var all []json.RawMessage
|
||||
@@ -207,7 +226,7 @@ func (c *NavidromeClient) getList(path string, extra url.Values, dest any) error
|
||||
return json.Unmarshal(merged, dest)
|
||||
}
|
||||
|
||||
func (c *NavidromeClient) request(method, path string, body interface{}, dest interface{}) error {
|
||||
func (c *NavidromeClient) request(method, path string, body any, dest any) error {
|
||||
httpResp, err := c.do(method, path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -8,16 +8,12 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const spotifyAPIBase = "https://api.spotify.com/v1"
|
||||
|
||||
type SpotifyTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
type SpotifyPlaylist struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
@@ -51,50 +47,46 @@ type SpotifyTrack struct {
|
||||
} `json:"external_ids"`
|
||||
}
|
||||
|
||||
func GetSpotifyAccessToken(clientID, clientSecret string) (string, error) {
|
||||
creds := base64.StdEncoding.EncodeToString([]byte(clientID + ":" + clientSecret))
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("grant_type", "client_credentials")
|
||||
|
||||
req, err := http.NewRequest("POST", "https://accounts.spotify.com/api/token",
|
||||
strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Basic "+creds)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("auth failed (%d): %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
var token SpotifyTokenResponse
|
||||
if err := json.Unmarshal(body, &token); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token.AccessToken, nil
|
||||
type SpotifyClient struct {
|
||||
clientID string
|
||||
clientSecret string
|
||||
token string
|
||||
mu sync.Mutex
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func GetPlaylist(token, playlistID string) (*SpotifyPlaylist, error) {
|
||||
type spotifyTokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
|
||||
func NewSpotifyClient(clientID, clientSecret string) (*SpotifyClient, error) {
|
||||
c := &SpotifyClient{
|
||||
clientID: clientID,
|
||||
clientSecret: clientSecret,
|
||||
httpClient: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
|
||||
if err := c.renewToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *SpotifyClient) GetPlaylist(playlistID string) (*SpotifyPlaylist, error) {
|
||||
// TODO: handle rate limit
|
||||
// TODO: handle request limits
|
||||
|
||||
reqURL := fmt.Sprintf("%s/playlists/%s", spotifyAPIBase, playlistID)
|
||||
|
||||
req, err := http.NewRequest("GET", reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
resp, err := c.do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -111,3 +103,60 @@ func GetPlaylist(token, playlistID string) (*SpotifyPlaylist, error) {
|
||||
}
|
||||
return &playlist, nil
|
||||
}
|
||||
|
||||
func (c *SpotifyClient) renewToken() error {
|
||||
creds := base64.StdEncoding.EncodeToString([]byte(c.clientID + ":" + c.clientSecret))
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("grant_type", "client_credentials")
|
||||
|
||||
req, err := http.NewRequest("POST", "https://accounts.spotify.com/api/token",
|
||||
strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Authorization", "Basic "+creds)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("auth failed (%d): %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
var token spotifyTokenResponse
|
||||
if err := json.Unmarshal(body, &token); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.token = token.AccessToken
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *SpotifyClient) do(req *http.Request) (*http.Response, error) {
|
||||
resp, err := c.doOnce(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode == http.StatusUnauthorized {
|
||||
resp.Body.Close()
|
||||
if err := c.renewToken(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.doOnce(req)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *SpotifyClient) doOnce(req *http.Request) (*http.Response, error) {
|
||||
c.mu.Lock()
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
c.mu.Unlock()
|
||||
return c.httpClient.Do(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user