This commit is contained in:
abs3nt 2024-02-18 10:46:59 -08:00
parent 8fccb0886f
commit 162574aa62
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09
4 changed files with 161 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package cli
import ( import (
"log/slog" "log/slog"
"os" "os"
"strconv"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.uber.org/fx" "go.uber.org/fx"
@ -55,6 +56,14 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
return c.PrintLink() return c.PrintLink()
}, },
}, },
{
Name: "linkcontext",
Aliases: []string{"lc"},
Usage: "Prints the current album or playlist",
Action: func(cCtx *cli.Context) error {
return c.PrintLinkContext()
},
},
{ {
Name: "next", Name: "next",
Aliases: []string{"n", "skip"}, Aliases: []string{"n", "skip"},
@ -96,19 +105,76 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
}, },
}, },
{ {
Name: "download_cover", Name: "volume",
Usage: "Downloads the cover of the current song", Aliases: []string{"v"},
Aliases: []string{"dl"}, Usage: "Control the volume",
Flags: []cli.Flag{ Subcommands: []*cli.Command{
&cli.PathFlag{ {
Name: "path", Name: "up",
Aliases: []string{"p"}, Usage: "Increase the volume",
Usage: "Path to save the cover", Args: true,
DefaultText: "./cover.png", ArgsUsage: "percent",
Action: func(cCtx *cli.Context) error {
amt, err := strconv.Atoi(cCtx.Args().First())
if err != nil {
return err
}
return c.ChangeVolume(amt)
},
},
{
Name: "down",
Aliases: []string{"dn"},
Usage: "Decrease the volume",
Args: true,
ArgsUsage: "percent",
Action: func(cCtx *cli.Context) error {
amt, err := strconv.Atoi(cCtx.Args().First())
if err != nil {
return err
}
return c.ChangeVolume(-amt)
},
},
{
Name: "mute",
Aliases: []string{"m"},
Usage: "Mute",
Action: func(cCtx *cli.Context) error {
return c.Mute()
},
},
{
Name: "unmute",
Aliases: []string{"um"},
Usage: "Unmute",
Action: func(cCtx *cli.Context) error {
return c.UnMute()
},
},
{
Name: "togglemute",
Aliases: []string{"tm"},
Usage: "Toggle mute",
Action: func(cCtx *cli.Context) error {
return c.ToggleMute()
},
}, },
}, },
},
{
Name: "download_cover",
Usage: "Downloads the cover of the current song",
Aliases: []string{"dl"},
Args: true,
ArgsUsage: "path",
BashComplete: func(cCtx *cli.Context) {
if cCtx.NArg() > 0 {
return
}
},
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
return c.DownloadCover(cCtx.Path("path")) return c.DownloadCover(cCtx.Args().First())
}, },
}, },
{ {
@ -119,9 +185,24 @@ func Run(c *commands.Commander, s fx.Shutdowner) {
return c.Radio() return c.Radio()
}, },
}, },
{
Name: "clearradio",
Usage: "Clears the radio queue",
Aliases: []string{"cr"},
Action: func(ctx *cli.Context) error {
return c.ClearRadio()
},
},
{
Name: "devices",
Usage: "Lists available devices",
Aliases: []string{"d"},
Action: func(ctx *cli.Context) error {
return c.ListDevices()
},
},
}, },
} }
app.Suggest = true
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
slog.Error("COMMANDER", "run error", err) slog.Error("COMMANDER", "run error", err)
os.Exit(1) os.Exit(1)

View File

@ -0,0 +1,25 @@
package commands
import (
"encoding/json"
"fmt"
"github.com/zmb3/spotify/v2"
)
func (c *Commander) ListDevices() error {
devices, err := c.Client.PlayerDevices(c.Context)
if err != nil {
return err
}
return PrintDevices(devices)
}
func PrintDevices(devices []spotify.PlayerDevice) error {
out, err := json.MarshalIndent(devices, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
}

View File

@ -10,3 +10,12 @@ func (c *Commander) PrintLink() error {
fmt.Println(state.Item.ExternalURLs["spotify"]) fmt.Println(state.Item.ExternalURLs["spotify"])
return nil return nil
} }
func (c *Commander) PrintLinkContext() error {
state, err := c.Client.PlayerState(c.Context)
if err != nil {
return err
}
fmt.Println(state.PlaybackContext.ExternalURLs["spotify"])
return nil
}

View File

@ -0,0 +1,35 @@
package commands
func (c *Commander) ChangeVolume(amount int) error {
state, err := c.Client.PlayerState(c.Context)
if err != nil {
return err
}
newVolume := state.Device.Volume + amount
if newVolume > 100 {
newVolume = 100
}
if newVolume < 0 {
newVolume = 0
}
return c.Client.Volume(c.Context, newVolume)
}
func (c *Commander) Mute() error {
return c.ChangeVolume(-100)
}
func (c *Commander) UnMute() error {
return c.ChangeVolume(100)
}
func (c *Commander) ToggleMute() error {
state, err := c.Client.PlayerState(c.Context)
if err != nil {
return err
}
if state.Device.Volume == 0 {
return c.ChangeVolume(100)
}
return c.ChangeVolume(-100)
}