Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Albums, Tracks, Artists; and set Playback Device #13

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build
release
*.swp

3 changes: 3 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
go build -o build/spotctl \
-ldflags "-X main.spotifyClientID=$SPOTIFY_CLIENT_ID -X main.spotifyClientSecret=$SPOTIFY_CLIENT_SECRET" \
./cmd/spotctl/...

rm -f /home/baddecisionsalex/bin/spotctl
ln -s /home/baddecisionsalex/go/src/github.com/baddecisionsalex/spotctl/build/spotctl /home/baddecisionsalex/bin/spotctl
164 changes: 141 additions & 23 deletions cmd/spotctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,141 @@ var devicesCmd = &cobra.Command{
RunE: devices,
}

var setDeviceCmd = &cobra.Command{
Use: "setdevice [name]",
Short: "Set playback device",
RunE: setDevice,
}

var getAlbumsCmd = &cobra.Command{
Use: "albums",
Short: "Show list of saved albums.",
RunE: getAlbums,
}

var getSongsCmd = &cobra.Command{
Use: "tracks",
Short: "Show list of saved tracks.",
RunE: getSongs,
}

var getArtistsCmd = &cobra.Command{
Use: "artists",
Short: "Show list of saved artists.",
RunE: getArtists,
}

func devices(cmd *cobra.Command, args []string) error {
devices, err := client.PlayerDevices()
if err != nil {
return err
}

for _, device := range devices {
active := ""
if device.Active {
active = "* "
}
fmt.Printf("%s%s - %s (volume %d%%)\n", active, device.Name, device.Type, device.Volume)
}

return nil
}

func setDevice(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return nil
}
devices, err := client.PlayerDevices()
if err != nil {
return err
}
var wanted string
wanted = strings.Join(args, " ")
for _, device := range devices {
if wanted == device.Name {
fmt.Printf ("Transfering playback to \"%s\"\n", device.Name);
err = client.TransferPlayback(device.ID, true)
if err != nil {
return err
} else {
return nil
}
}
}

return nil
}

func getAlbums(cmd *cobra.Command, args []string) error {
limit := int(50)
start := int(0)
var opt *spotify.Options
opt = &spotify.Options{
Limit: &limit,
Offset: &start,
}
for {
albums, err := client.CurrentUsersAlbumsOpt(opt)
if err != nil {
return err
}
for _, album := range albums.Albums {
fmt.Printf("%s\n", album.Name)
}
if len(albums.Albums) < limit {
return nil
}
start += limit
}
return nil
}

func getSongs(cmd *cobra.Command, args []string) error {
limit := int(50)
start := int(0)
var opt *spotify.Options
opt = &spotify.Options{
Limit: &limit,
Offset: &start,
}
for {
songs, err := client.CurrentUsersTracksOpt(opt)
if err != nil {
return err
}
for _, song := range songs.Tracks {
fmt.Printf("%s\n", song.Name)
}
if len(songs.Tracks) < limit {
return nil
}
start += limit
}
return nil
}

func getArtists(cmd *cobra.Command, args []string) error {
start := ""
for {
artists, err := client.CurrentUsersFollowedArtistsOpt(50, start)
if err != nil {
return err
}
for _, artist := range artists.Artists {
fmt.Printf("%s\n", artist.Name)
}
if len(artists.Artists) == 50 {
start = string(artists.Artists[49].URI)
start = strings.Split(start,":")[2]
fmt.Printf("%s\n", start)
} else {
return nil
}
}
return nil
}

func shuffle(cmd *cobra.Command, args []string) error {
state, err := client.PlayerState()
if err != nil {
Expand Down Expand Up @@ -97,7 +232,7 @@ func repeat(cmd *cobra.Command, args []string) error {
}

opt := &spotify.PlayOptions{
DeviceID: findDeviceByName(deviceNameFlag),
// DeviceID: findDeviceByName(deviceNameFlag),
}
return client.RepeatOpt(repeat, opt)
}
Expand All @@ -120,28 +255,11 @@ func play(cmd *cobra.Command, args []string) error {
}
}

opt.DeviceID = findDeviceByName(deviceNameFlag)
//opt.DeviceID = findDeviceByName(deviceNameFlag)

return client.PlayOpt(opt)
}

func devices(cmd *cobra.Command, args []string) error {
devices, err := client.PlayerDevices()
if err != nil {
return err
}

for _, device := range devices {
active := ""
if device.Active {
active = "* "
}
fmt.Printf("%s%s - %s (volume %d%%)\n", active, device.Name, device.Type, device.Volume)
}

return nil
}

func vol(cmd *cobra.Command, args []string) error {
state, err := client.PlayerState()
if err != nil {
Expand Down Expand Up @@ -175,28 +293,28 @@ func vol(cmd *cobra.Command, args []string) error {
}

opt := &spotify.PlayOptions{
DeviceID: findDeviceByName(deviceNameFlag),
//DeviceID: findDeviceByName(deviceNameFlag),
}
return client.VolumeOpt(currVolume, opt)
}

func pause(cmd *cobra.Command, args []string) error {
opt := &spotify.PlayOptions{
DeviceID: findDeviceByName(deviceNameFlag),
//DeviceID: findDeviceByName(deviceNameFlag),
}
return client.PauseOpt(opt)
}

func next(cmd *cobra.Command, args []string) error {
opt := &spotify.PlayOptions{
DeviceID: findDeviceByName(deviceNameFlag),
//DeviceID: findDeviceByName(deviceNameFlag),
}
return client.NextOpt(opt)
}

func prev(cmd *cobra.Command, args []string) error {
opt := &spotify.PlayOptions{
DeviceID: findDeviceByName(deviceNameFlag),
//DeviceID: findDeviceByName(deviceNameFlag),
}
return client.PreviousOpt(opt)
}
Expand Down
23 changes: 15 additions & 8 deletions cmd/spotctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ func main() {
rootCmd.AddCommand(statusCmd)
rootCmd.AddCommand(playerCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(getAlbumsCmd)
rootCmd.AddCommand(getSongsCmd)
rootCmd.AddCommand(getArtistsCmd)
rootCmd.AddCommand(setDeviceCmd)

playCmd.PersistentFlags().StringVarP(&playCmdFlagType, "type", "t", "track", "the type of [name] to play: track, album, artist or playlist.")
playCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
pauseCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
nextCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
prevCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
volCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
shuffleCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
repeatCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
playerCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//playCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//pauseCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//nextCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//prevCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//volCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//shuffleCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//repeatCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")
//playerCmd.PersistentFlags().StringVarP(&deviceNameFlag, "device", "d", "", "the name of device")

if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
Expand All @@ -89,6 +93,9 @@ func preRootCmd(cmd *cobra.Command, args []string) {
spotify.ScopeUserReadCurrentlyPlaying,
spotify.ScopeUserReadPlaybackState,
spotify.ScopeUserModifyPlaybackState,
spotify.ScopeUserLibraryRead,
spotify.ScopeUserReadPrivate,
spotify.ScopeUserFollowRead,
)
auth.SetAuthInfo(spotifyClientID, spotifyClientSecret)

Expand Down