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 +}