This commit is contained in:
abs3nt 2024-02-18 10:50:43 -08:00
parent 162574aa62
commit ba2b6f9952
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
2 changed files with 53 additions and 1 deletions

View File

@ -32,6 +32,16 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
return c.Play() 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", Name: "pause",
Aliases: []string{"pa"}, Aliases: []string{"pa"},

View File

@ -1,6 +1,11 @@
package commands package commands
import "github.com/zmb3/spotify/v2" import (
"net/url"
"strings"
"github.com/zmb3/spotify/v2"
)
func (c *Commander) Play() error { func (c *Commander) Play() error {
err := c.Client.Play(c.Context) err := c.Client.Play(c.Context)
@ -22,3 +27,40 @@ func (c *Commander) Play() error {
} }
return nil 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
}