diff --git a/.gitignore b/.gitignore index b236644..aad8d40 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build release +*.swp + diff --git a/bin/build b/bin/build index 2a065a3..e9d4e1f 100755 --- a/bin/build +++ b/bin/build @@ -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 diff --git a/cmd/spotctl/ctl.go b/cmd/spotctl/ctl.go index 3bef522..2df187d 100644 --- a/cmd/spotctl/ctl.go +++ b/cmd/spotctl/ctl.go @@ -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 { @@ -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) } @@ -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 { @@ -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) } diff --git a/cmd/spotctl/main.go b/cmd/spotctl/main.go index 1a30d5b..98aae37 100644 --- a/cmd/spotctl/main.go +++ b/cmd/spotctl/main.go @@ -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) @@ -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)