use device name instead of ID

This commit is contained in:
Niklas Kapelle 2023-09-26 01:07:01 +02:00
parent 527200abe7
commit 1c9706485c
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
4 changed files with 28 additions and 5 deletions

View File

@ -8,7 +8,7 @@ import (
type args struct { type args struct {
SpotifyClientID string `arg:"--spotify-client-id,required,env:SPOTIFY_CLIENT_ID" placeholder:"SPOTIFY_CLIENT_ID"` 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"` 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"` 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"` 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"` 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{ morningalarm.New(morningalarm.MorningAlarmConfig{
SpotifyClientID: args.SpotifyClientID, SpotifyClientID: args.SpotifyClientID,
SpotifyClientSecret: args.SpotifyClientSecret, SpotifyClientSecret: args.SpotifyClientSecret,
DeviceID: args.DeviceID, DeviceName: args.DeviceName,
PlaylistID: args.PlaylistID, PlaylistID: args.PlaylistID,
RedirectURL: args.RedirectURL, RedirectURL: args.RedirectURL,
ListenAddr: args.ListenAddr, ListenAddr: args.ListenAddr,

View File

@ -15,7 +15,7 @@ type alarm struct {
type MorningAlarmConfig struct { type MorningAlarmConfig struct {
SpotifyClientID string `json:"spotifyClientId"` SpotifyClientID string `json:"spotifyClientId"`
SpotifyClientSecret string `json:"spotifyClientSecret"` SpotifyClientSecret string `json:"spotifyClientSecret"`
DeviceID string `json:"deviceId"` DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"` PlaylistID string `json:"wakeupContext"`
RedirectURL string `json:"redirectUrl"` RedirectURL string `json:"redirectUrl"`
ListenAddr string `json:"listenAddr"` ListenAddr string `json:"listenAddr"`

View File

@ -114,7 +114,14 @@ func (ma *MorningAlarm) setupSpotify() error {
// Can still return an error even if the playback was started successfully. // Can still return an error even if the playback was started successfully.
func (ma *MorningAlarm) playWakeUpMusic() (bool, error) { func (ma *MorningAlarm) playWakeUpMusic() (bool, error) {
playContextURI := spotify.URI("spotify:playlist:" + ma.config.PlaylistID) 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)) 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 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)
}

View File

@ -106,7 +106,7 @@ func (ma *MorningAlarm) setupWebserver() {
"timezoneOffsetInH": offset / 60 / 60, "timezoneOffsetInH": offset / 60 / 60,
"alarms": len(ma.cr.Entries()), "alarms": len(ma.cr.Entries()),
"wakeupPlaylist": ma.config.PlaylistID, "wakeupPlaylist": ma.config.PlaylistID,
"deviceId": ma.config.DeviceID, "deviceName": ma.config.DeviceName,
} }
if nextIn != nil { if nextIn != nil {