From 1c9706485cb285cc611f474141783a01145c3744 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 26 Sep 2023 01:07:01 +0200 Subject: [PATCH] use device name instead of ID --- cmd/morningalarm.go | 4 ++-- internal/morningalarm.go | 2 +- internal/spotify.go | 25 ++++++++++++++++++++++++- internal/webserver.go | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/morningalarm.go b/cmd/morningalarm.go index 924f1b7..442609e 100644 --- a/cmd/morningalarm.go +++ b/cmd/morningalarm.go @@ -8,7 +8,7 @@ import ( type args struct { SpotifyClientID string `arg:"--spotify-client-id,required,env:SPOTIFY_CLIENT_ID" placeholder:"SPOTIFY_CLIENT_ID"` SpotifyClientSecret string `arg:"--spotify-client-secret,required,env:SPOTIFY_CLIENT_SECRET" placeholder:"SPOTIFY_CLIENT_SECRET"` - DeviceID string `arg:"--device-id,required,env:SPOTIFY_DEVICE_ID" placeholder:"SPOTIFY_DEVICE_ID" help:"Can be found later in the API"` + DeviceName string `arg:"--device-name,required,env:SPOTIFY_DEVICE_NAME" placeholder:"SPOTIFY_DEVICE_NAME" help:"Can be found later in the API"` PlaylistID string `arg:"--playlist-id,required,env:SPOTIFY_PLAYLIST_ID" help:"Just the id" placeholder:"SPOTIFY_PLAYLIST_ID"` RedirectURL string `arg:"--redirect-url,required,env:SPOTIFY_REDIRECT_URL" help:"Must be the same as in the Spotify developer dashboard." placeholder:"SPOTIFY_REDIRECT_URL"` ListenAddr string `arg:"--listen-addr,env:LISTEN_ADDR" help:"Address to listen on" placeholder:"LISTEN_ADDR" default:":3000"` @@ -23,7 +23,7 @@ func main() { morningalarm.New(morningalarm.MorningAlarmConfig{ SpotifyClientID: args.SpotifyClientID, SpotifyClientSecret: args.SpotifyClientSecret, - DeviceID: args.DeviceID, + DeviceName: args.DeviceName, PlaylistID: args.PlaylistID, RedirectURL: args.RedirectURL, ListenAddr: args.ListenAddr, diff --git a/internal/morningalarm.go b/internal/morningalarm.go index 0769c0d..cf3f3c9 100644 --- a/internal/morningalarm.go +++ b/internal/morningalarm.go @@ -15,7 +15,7 @@ type alarm struct { type MorningAlarmConfig struct { SpotifyClientID string `json:"spotifyClientId"` SpotifyClientSecret string `json:"spotifyClientSecret"` - DeviceID string `json:"deviceId"` + DeviceName string `json:"deviceName"` PlaylistID string `json:"wakeupContext"` RedirectURL string `json:"redirectUrl"` ListenAddr string `json:"listenAddr"` diff --git a/internal/spotify.go b/internal/spotify.go index 3756ba1..178fac3 100644 --- a/internal/spotify.go +++ b/internal/spotify.go @@ -114,7 +114,14 @@ func (ma *MorningAlarm) setupSpotify() error { // Can still return an error even if the playback was started successfully. func (ma *MorningAlarm) playWakeUpMusic() (bool, error) { playContextURI := spotify.URI("spotify:playlist:" + ma.config.PlaylistID) - deviceID := spotify.ID(ma.config.DeviceID) + + devID, err := ma.getDeviceIDFromName(ma.config.DeviceName) + + if err != nil { + return false, err + } + + deviceID := spotify.ID(devID) playlist, err := ma.sp.GetPlaylistItems(context.Background(), spotify.ID(ma.config.PlaylistID)) @@ -160,3 +167,19 @@ func (ma *MorningAlarm) getAvailableDevices() ([]Device, error) { return availableDevices, nil } + +func (ma *MorningAlarm) getDeviceIDFromName(name string) (string, error) { + dev, err := ma.getAvailableDevices() + + if err != nil { + return "", err + } + + for _, device := range dev { + if device.Name == name { + return device.ID, nil + } + } + + return "", fmt.Errorf("device with name %s not found", name) +} diff --git a/internal/webserver.go b/internal/webserver.go index e2736b8..fb3cb59 100644 --- a/internal/webserver.go +++ b/internal/webserver.go @@ -106,7 +106,7 @@ func (ma *MorningAlarm) setupWebserver() { "timezoneOffsetInH": offset / 60 / 60, "alarms": len(ma.cr.Entries()), "wakeupPlaylist": ma.config.PlaylistID, - "deviceId": ma.config.DeviceID, + "deviceName": ma.config.DeviceName, } if nextIn != nil {