-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from twexler/latest-api
Adds playlist command, with list and detail subcommands. Upgrades API version
- Loading branch information
Showing
7 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package playlist | ||
|
||
import ( | ||
"fmt" | ||
"spotify/internal" | ||
"strings" | ||
|
||
"github.com/brianstrauch/spotify" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewListCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Short: "List playlists.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
api, err := internal.Authenticate() | ||
if err != nil { | ||
return err | ||
} | ||
output, err := List(api) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Print(output) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func List(api *spotify.API) (string, error) { | ||
playlists, err := api.GetPlaylists() | ||
if err != nil { | ||
return "", err | ||
} | ||
var builder strings.Builder | ||
for _, pl := range playlists { | ||
builder.WriteString(fmt.Sprintln(pl.Name)) | ||
} | ||
return builder.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package playlist | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "playlist", | ||
Short: "do things with playlists", | ||
} | ||
cmd.AddCommand(NewListCommand()) | ||
cmd.AddCommand(NewShowCommand()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package playlist | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"spotify/internal" | ||
"strings" | ||
|
||
"github.com/brianstrauch/spotify" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewShowCommand() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "show", | ||
Short: "Show playlist artist and songs.", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
api, err := internal.Authenticate() | ||
if err != nil { | ||
return err | ||
} | ||
return Show(api, args) | ||
}, | ||
} | ||
} | ||
|
||
func Show(api *spotify.API, args []string) error { | ||
playlists, err := api.GetPlaylists() | ||
if err != nil { | ||
return err | ||
} | ||
id := "" | ||
for _, playlist := range playlists { | ||
if strings.EqualFold(strings.ToLower(playlist.Name), strings.ToLower(args[0])) { | ||
id = playlist.ID | ||
} | ||
} | ||
if id == "" { | ||
return errors.New("no such playlist") | ||
} | ||
playlist, err := api.GetPlaylist(id) | ||
if err != nil { | ||
return err | ||
} | ||
output, err := formatPlaylist(api, playlist) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Print(output) | ||
return nil | ||
} | ||
|
||
func formatPlaylist(api *spotify.API, playlist *spotify.Playlist) (string, error) { | ||
var builder strings.Builder | ||
builder.WriteString(fmt.Sprintf("📝: %s\n", playlist.Name)) | ||
builder.WriteString(fmt.Sprintln("💿 Tracks:")) | ||
for i, track := range playlist.Tracks.Items { | ||
artistNames := make([]string, len(track.Track.Artists)) | ||
for i, artist := range track.Track.Artists { | ||
if err := artist.Get(api, &artist); err != nil { | ||
return "", err | ||
} | ||
artistNames[i] = artist.Name | ||
} | ||
builder.WriteString( | ||
fmt.Sprintf( | ||
"%d. %s - %s\n", | ||
i+1, | ||
strings.Join(artistNames, ". "), | ||
track.Track.Name, | ||
), | ||
) | ||
} | ||
return builder.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters