volume and position cli

This commit is contained in:
jjohnstondev 2023-01-14 20:52:47 -08:00
parent f5caa8f08f
commit fe278a3270
5 changed files with 187 additions and 0 deletions

24
src/cmd/mute.go Normal file
View File

@ -0,0 +1,24 @@
package cmd
import (
"gospt/src/commands"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(muteCmd)
}
var muteCmd = &cobra.Command{
Use: "mute",
Short: "mutes playback",
Long: `Mutes the spotify device, playback will continue`,
RunE: func(cmd *cobra.Command, args []string) error {
err := commands.SetVolume(ctx, client, 0)
if err != nil {
return err
}
return nil
},
}

48
src/cmd/seek.go Normal file
View File

@ -0,0 +1,48 @@
package cmd
import (
"strconv"
"gospt/src/commands"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(seekCmd)
}
var seekCmd = &cobra.Command{
Use: "seek {forward/backward/songposition in seconds}",
Short: "seek forward/backward or to a given second",
Args: cobra.MinimumNArgs(1),
Long: `Seeks forward or backward, or seeks to a given position in seconds`,
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] == "forward" || args[0] == "f" {
err := commands.Seek(ctx, client, true)
if err != nil {
return err
}
return nil
}
if args[0] == "backward" || args[0] == "b" {
err := commands.Seek(ctx, client, false)
if err != nil {
return err
}
return nil
}
pos, err := strconv.Atoi(args[0])
if err != nil {
return err
}
pos = pos * 1000
err = commands.SetPosition(ctx, client, pos)
if err != nil {
return err
}
return nil
},
}

24
src/cmd/unmute.go Normal file
View File

@ -0,0 +1,24 @@
package cmd
import (
"gospt/src/commands"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(unmuteCmd)
}
var unmuteCmd = &cobra.Command{
Use: "unmute",
Short: "unmutes playback",
Long: `unmutes the spotify device, playback will continue`,
RunE: func(cmd *cobra.Command, args []string) error {
err := commands.SetVolume(ctx, client, 100)
if err != nil {
return err
}
return nil
},
}

47
src/cmd/volume.go Normal file
View File

@ -0,0 +1,47 @@
package cmd
import (
"strconv"
"gospt/src/commands"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(volumeCmd)
}
var volumeCmd = &cobra.Command{
Use: "volume",
Short: "sets the volume",
Args: cobra.MinimumNArgs(1),
Long: `Sets the volume to the given percent [0-100] or increases/decreases by 5 percent if you say up or down`,
RunE: func(cmd *cobra.Command, args []string) error {
if args[0] == "up" {
err := commands.ChangeVolume(ctx, client, 5)
if err != nil {
return err
}
return nil
}
if args[0] == "down" {
err := commands.ChangeVolume(ctx, client, -5)
if err != nil {
return err
}
return nil
}
vol, err := strconv.Atoi(args[0])
if err != nil {
return err
}
err = commands.SetVolume(ctx, client, vol)
if err != nil {
return err
}
return nil
},
}

View File

@ -16,6 +16,50 @@ import (
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
) )
func SetVolume(ctx *gctx.Context, client *spotify.Client, vol int) error {
return client.Volume(ctx, vol)
}
func SetPosition(ctx *gctx.Context, client *spotify.Client, pos int) error {
err := client.Seek(ctx, pos)
if err != nil {
fmt.Println(err.Error())
return err
}
return nil
}
func Seek(ctx *gctx.Context, client *spotify.Client, fwd bool) error {
current, err := client.PlayerCurrentlyPlaying(ctx)
if err != nil {
return err
}
newPos := current.Progress + 5000
if !fwd {
newPos = current.Progress - 5000
}
err = client.Seek(ctx, newPos)
if err != nil {
return err
}
return nil
}
func ChangeVolume(ctx *gctx.Context, client *spotify.Client, vol int) error {
state, err := client.PlayerState(ctx)
if err != nil {
return err
}
newVolume := state.Device.Volume + vol
if newVolume > 100 {
newVolume = 100
}
if newVolume < 0 {
newVolume = 0
}
return client.Volume(ctx, newVolume)
}
func Play(ctx *gctx.Context, client *spotify.Client) error { func Play(ctx *gctx.Context, client *spotify.Client) error {
err := client.Play(ctx) err := client.Play(ctx)
if err != nil { if err != nil {