From ba2b6f9952b6bd5e884dd330fc78b4f19dcf0552 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 10:50:43 -0800 Subject: [PATCH] playurl --- src/components/cli/cli.go | 10 ++++++++ src/components/commands/play.go | 44 ++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index 652c78b..2ffc616 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -32,6 +32,16 @@ func Run(c *commands.Commander, s fx.Shutdowner) { return c.Play() }, }, + { + Name: "playurl", + Aliases: []string{"plu"}, + Usage: "Plays a spotify url", + Args: true, + ArgsUsage: "url", + Action: func(ctx *cli.Context) error { + return c.PlayUrl(ctx.Args().First()) + }, + }, { Name: "pause", Aliases: []string{"pa"}, diff --git a/src/components/commands/play.go b/src/components/commands/play.go index f799036..ce4174d 100644 --- a/src/components/commands/play.go +++ b/src/components/commands/play.go @@ -1,6 +1,11 @@ package commands -import "github.com/zmb3/spotify/v2" +import ( + "net/url" + "strings" + + "github.com/zmb3/spotify/v2" +) func (c *Commander) Play() error { err := c.Client.Play(c.Context) @@ -22,3 +27,40 @@ func (c *Commander) Play() error { } return nil } + +func (c *Commander) PlayUrl(urlString string) error { + url, err := url.Parse(urlString) + if err != nil { + return err + } + track_id := strings.Split(url.Path, "/")[2] + err = c.Client.QueueSong(c.Context, spotify.ID(track_id)) + if err != nil { + if isNoActiveError(err) { + deviceID, err := c.activateDevice(c.Context) + if err != nil { + return err + } + err = c.Client.QueueSongOpt(c.Context, spotify.ID(track_id), &spotify.PlayOptions{ + DeviceID: &deviceID, + }) + if err != nil { + return err + } + err = c.Client.NextOpt(c.Context, &spotify.PlayOptions{ + DeviceID: &deviceID, + }) + if err != nil { + return err + } + return nil + } else { + return err + } + } + err = c.Client.Next(c.Context) + if err != nil { + return err + } + return nil +}