sync playlists between spotify and navidrome
This commit is contained in:
@@ -1,5 +1,117 @@
|
||||
package ripsort
|
||||
|
||||
func SyncPlaylists(spotifyUrl string) {
|
||||
// TODO
|
||||
import (
|
||||
"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