From 0ff00ef79b5addc4ae2a805d084f00f09741d270 Mon Sep 17 00:00:00 2001 From: jjohnstondev Date: Mon, 9 Jan 2023 18:03:31 -0800 Subject: [PATCH] songs play in context --- internal/commands/commands.go | 48 +++++++++++++++++++++++++++++++++- internal/tui/list.go | 7 +---- internal/tui/playlisttracks.go | 7 +---- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/internal/commands/commands.go b/internal/commands/commands.go index 10b8208..0d80e5c 100644 --- a/internal/commands/commands.go +++ b/internal/commands/commands.go @@ -88,6 +88,53 @@ func QueueSong(ctx *gctx.Context, client *spotify.Client, id spotify.ID) error { return nil } +func PlaySongInPlaylist(ctx *gctx.Context, client *spotify.Client, context *spotify.URI, offset int) error { + err := client.PlayOpt(ctx, &spotify.PlayOptions{ + PlaybackOffset: &spotify.PlaybackOffset{Position: offset}, + PlaybackContext: context, + }) + return err +} + +func PlayLikedSongs(ctx *gctx.Context, client *spotify.Client, position int) error { + err := ClearRadio(ctx, client) + if err != nil { + return err + } + playlist, err := GetRadioPlaylist(ctx, client) + if err != nil { + return err + } + songs, err := client.CurrentUsersTracks(ctx, spotify.Limit(50), spotify.Offset(position)) + if err != nil { + return err + } + to_add := []spotify.ID{} + for _, song := range songs.Tracks { + to_add = append(to_add, song.ID) + } + client.AddTracksToPlaylist(ctx, playlist.ID, to_add...) + client.PlayOpt(ctx, &spotify.PlayOptions{ + PlaybackContext: &playlist.URI, + PlaybackOffset: &spotify.PlaybackOffset{ + Position: 0, + }, + }) + for page := 2; page <= 5; page++ { + songs, err := client.CurrentUsersTracks(ctx, spotify.Limit(50), spotify.Offset((50*(page-1))+position)) + if err != nil { + return err + } + to_add := []spotify.ID{} + for _, song := range songs.Tracks { + to_add = append(to_add, song.ID) + } + client.AddTracksToPlaylist(ctx, playlist.ID, to_add...) + } + + return err +} + func RadioGivenSong(ctx *gctx.Context, client *spotify.Client, song_id spotify.ID, pos int) error { start := time.Now().UnixMilli() seed := spotify.Seeds{ @@ -491,7 +538,6 @@ func GetRadioPlaylist(ctx *gctx.Context, client *spotify.Client) (*spotify.FullP // private flag doesnt work playlist, err := client.CreatePlaylistForUser(ctx, ctx.UserId, "autoradio", "Automanaged radio playlist", false, false) if err != nil { - fmt.Println("AHHHHHHHHHHHHh", ctx.UserId) return nil, err } out, err := json.MarshalIndent(playlist, "", " ") diff --git a/internal/tui/list.go b/internal/tui/list.go index 1f50196..d066d44 100644 --- a/internal/tui/list.go +++ b/internal/tui/list.go @@ -88,13 +88,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } if msg.String() == "enter" { - track := m.list.SelectedItem() var err error - err = commands.QueueSong(m.ctx, m.client, track.(item).ID) - if err != nil { - m.ctx.Printf(err.Error()) - } - err = commands.Skip(m.ctx, m.client) + err = commands.PlayLikedSongs(m.ctx, m.client, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) if err != nil { m.ctx.Printf(err.Error()) } diff --git a/internal/tui/playlisttracks.go b/internal/tui/playlisttracks.go index 6019e60..9984e86 100644 --- a/internal/tui/playlisttracks.go +++ b/internal/tui/playlisttracks.go @@ -87,13 +87,8 @@ func (m playlistTracksModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } if msg.String() == "enter" { - track := m.list.SelectedItem() var err error - err = commands.QueueSong(m.ctx, m.client, track.(item).ID) - if err != nil { - m.ctx.Printf(err.Error()) - } - err = commands.Skip(m.ctx, m.client) + err = commands.PlaySongInPlaylist(m.ctx, m.client, &m.playlist.URI, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) if err != nil { m.ctx.Printf(err.Error()) }