sync playlists between spotify and navidrome
This commit is contained in:
@@ -17,7 +17,19 @@ type SortCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SyncPlaylists struct {
|
type SyncPlaylists struct {
|
||||||
Url string `arg:"positional,required"`
|
SpotifyArgs
|
||||||
|
NavidromeArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
type SpotifyArgs struct {
|
||||||
|
SpotifyClientID string `arg:"--spotify-client-id,env:SPOTIFY_CLIENT_ID,required"`
|
||||||
|
SpotifyClientSecret string `arg:"--spotify-client-secret,env:SPOTIFY_CLIENT_SECRET,required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NavidromeArgs struct {
|
||||||
|
NavidromeURL string `arg:"--navidrome-url,env:ND_URL,required"`
|
||||||
|
NavidromeUser string `arg:"--navidrome-user,env:ND_USER,required"`
|
||||||
|
NavidromePass string `arg:"--navidrome-pass,env:ND_PASS,required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FixCommentCmd struct {
|
type FixCommentCmd struct {
|
||||||
@@ -28,7 +40,7 @@ type args struct {
|
|||||||
Info *InfoCmd `arg:"subcommand:info"`
|
Info *InfoCmd `arg:"subcommand:info"`
|
||||||
Sort *SortCmd `arg:"subcommand:sort"`
|
Sort *SortCmd `arg:"subcommand:sort"`
|
||||||
FixCommentTag *FixCommentCmd `arg:"subcommand:fix-comment"`
|
FixCommentTag *FixCommentCmd `arg:"subcommand:fix-comment"`
|
||||||
SyncPlaylists *SyncPlaylists `arg:"subcommand:spotify"`
|
SyncPlaylists *SyncPlaylists `arg:"subcommand:sync-playlist"`
|
||||||
Verbose bool `arg:"-v" default:"false"`
|
Verbose bool `arg:"-v" default:"false"`
|
||||||
DryRun bool `arg:"--dry-run" default:"false"`
|
DryRun bool `arg:"--dry-run" default:"false"`
|
||||||
}
|
}
|
||||||
@@ -49,7 +61,13 @@ func Run() {
|
|||||||
case args.FixCommentTag != nil:
|
case args.FixCommentTag != nil:
|
||||||
ripsort.FixComment(args.FixCommentTag.Path, args.DryRun)
|
ripsort.FixComment(args.FixCommentTag.Path, args.DryRun)
|
||||||
case args.SyncPlaylists != nil:
|
case args.SyncPlaylists != nil:
|
||||||
ripsort.SyncPlaylists(args.SyncPlaylists.Url)
|
ripsort.SyncPlaylists(
|
||||||
|
args.SyncPlaylists.SpotifyClientID,
|
||||||
|
args.SyncPlaylists.SpotifyClientSecret,
|
||||||
|
args.SyncPlaylists.NavidromeURL,
|
||||||
|
args.SyncPlaylists.NavidromeUser,
|
||||||
|
args.SyncPlaylists.NavidromePass,
|
||||||
|
)
|
||||||
default:
|
default:
|
||||||
p.Fail("Must specify command")
|
p.Fail("Must specify command")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ type NavidromePlaylist struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NavidromeTrack struct {
|
type NavidromeTrack struct {
|
||||||
|
ID string `json:"id"` // position ID within the playlist or id of the song. See MediaFileID
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Artist string `json:"artist"`
|
Artist string `json:"artist"`
|
||||||
ArtistID string `json:"artistId"`
|
ArtistID string `json:"artistId"`
|
||||||
@@ -65,7 +66,6 @@ type NavidromeTrack struct {
|
|||||||
type NavidromePlaylistTrack struct {
|
type NavidromePlaylistTrack struct {
|
||||||
NavidromeTrack
|
NavidromeTrack
|
||||||
|
|
||||||
ID string `json:"id"` // position ID within the playlist
|
|
||||||
MediaFileID string `json:"mediaFileId"` // the actual song ID
|
MediaFileID string `json:"mediaFileId"` // the actual song ID
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,6 +168,25 @@ func (c *NavidromeClient) getPlaylistTracks(playlistID string) ([]NavidromePlayl
|
|||||||
return tracks, nil
|
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 {
|
func (c *NavidromeClient) getList(path string, extra url.Values, dest any) error {
|
||||||
const pageSize = 500
|
const pageSize = 500
|
||||||
var all []json.RawMessage
|
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)
|
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)
|
httpResp, err := c.do(method, path, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -8,16 +8,12 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const spotifyAPIBase = "https://api.spotify.com/v1"
|
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 {
|
type SpotifyPlaylist struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
@@ -51,50 +47,46 @@ type SpotifyTrack struct {
|
|||||||
} `json:"external_ids"`
|
} `json:"external_ids"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSpotifyAccessToken(clientID, clientSecret string) (string, error) {
|
type SpotifyClient struct {
|
||||||
creds := base64.StdEncoding.EncodeToString([]byte(clientID + ":" + clientSecret))
|
clientID string
|
||||||
|
clientSecret string
|
||||||
data := url.Values{}
|
token string
|
||||||
data.Set("grant_type", "client_credentials")
|
mu sync.Mutex
|
||||||
|
httpClient *http.Client
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 rate limit
|
||||||
// TODO: handle request limits
|
// TODO: handle request limits
|
||||||
|
|
||||||
reqURL := fmt.Sprintf("%s/playlists/%s", spotifyAPIBase, playlistID)
|
reqURL := fmt.Sprintf("%s/playlists/%s", spotifyAPIBase, playlistID)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", reqURL, nil)
|
req, err := http.NewRequest("GET", reqURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := c.do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -111,3 +103,60 @@ func GetPlaylist(token, playlistID string) (*SpotifyPlaylist, error) {
|
|||||||
}
|
}
|
||||||
return &playlist, nil
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,117 @@
|
|||||||
package ripsort
|
package ripsort
|
||||||
|
|
||||||
func SyncPlaylists(spotifyUrl string) {
|
import (
|
||||||
// TODO
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"git.kapelle.org/niklas/ripsort/internal/playlist"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SyncPlaylists(spotifyClientID, spotifyClientSecret, navidromeBase, navidromeUser, navidromePass string) {
|
||||||
|
spotifyClient, err := playlist.NewSpotifyClient(spotifyClientID, spotifyClientSecret)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to create spotify client", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
navidromeClient, err := playlist.NewNavidromeClient(navidromeBase, navidromeUser, navidromePass)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to create navidrome client", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
allPlaylists, err := navidromeClient.GetPlaylists()
|
||||||
|
|
||||||
|
for _, pl := range allPlaylists {
|
||||||
|
spotifyPlaylistID := spotifyIDForPlaylist(&pl)
|
||||||
|
if spotifyPlaylistID == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Syncing playlist", "name", pl.Name)
|
||||||
|
|
||||||
|
err = syncPlaylists(spotifyClient, navidromeClient, spotifyPlaylistID, pl.ID)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to sync playlist", "name", pl.Name, "err", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func spotifyIDForPlaylist(playlist *playlist.NavidromePlaylist) string {
|
||||||
|
var re = regexp.MustCompile(`(?m)https:\/\/open\.spotify\.com\/playlist\/([a-zA-Z0-9]*)`)
|
||||||
|
|
||||||
|
matches := re.FindAllStringSubmatch(playlist.Comment, -1)
|
||||||
|
if len(matches) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches[0][1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncPlaylists(sp *playlist.SpotifyClient, nd *playlist.NavidromeClient, spotifyid string, navidromeID string) error {
|
||||||
|
spotifyPlaylist, err := sp.GetPlaylist(spotifyid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
navidromeFullPlaylist, err := nd.GetPlaylist(navidromeID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
songsToAdd := []string{}
|
||||||
|
|
||||||
|
for _, spotifySong := range spotifyPlaylist.Items.Items {
|
||||||
|
if ndPlaylistContainsSong(navidromeFullPlaylist, spotifySong.Item.ID) {
|
||||||
|
slog.Debug("Track already in playlist", "id", spotifySong.Item.ID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
songs, err := nd.GetSongBySpotifyID(spotifySong.Item.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(songs) == 0 {
|
||||||
|
slog.Warn("No song found", "spotifyid", spotifySong.Item.ID, "title", spotifySong.Item.Name, "artists", spotifySong.Item.Artists)
|
||||||
|
// Missing song
|
||||||
|
// TODO: handle
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(songs) != 1 {
|
||||||
|
slog.Warn("Found multiple songs for spotifyid", "spotifyid", spotifySong.Item.ID)
|
||||||
|
}
|
||||||
|
songsToAdd = append(songsToAdd, songs[0].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(songsToAdd) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Adding songs to playlist", "count", len(songsToAdd), "playlist", navidromeFullPlaylist.Name)
|
||||||
|
err = nd.AddTracks(navidromeID, songsToAdd)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to add songs", "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ndPlaylistContainsSong(pl *playlist.NavidromePlaylist, spotifyID string) bool {
|
||||||
|
for _, song := range pl.Tracks {
|
||||||
|
tag := song.CustomTags["spotifyid"]
|
||||||
|
|
||||||
|
if tag == nil || len(tag) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if tag[0] == spotifyID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user