Skip to content

Commit

Permalink
Merge pull request #25 from twexler/latest-api
Browse files Browse the repository at this point in the history
Adds playlist command, with list and detail subcommands. Upgrades API version
  • Loading branch information
brianstrauch authored Jul 3, 2021
2 parents e9f1af4 + 9203fd0 commit 36658c0
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/blang/semver v3.5.1+incompatible
github.com/brianstrauch/spotify v0.3.1
github.com/brianstrauch/spotify v0.4.0
github.com/pkg/browser v0.0.0-20210606212950-a7b7a6107d32
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/spf13/cobra v1.1.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/brianstrauch/spotify v0.3.1 h1:IBd0vkal0bs+yilvp+6VR4aL9q94qiionwYHETcOHS4=
github.com/brianstrauch/spotify v0.3.1/go.mod h1:sImRT74obai+LNgFVxr+qNVq2yt5KEfvNxz6ZDfYWaA=
github.com/brianstrauch/spotify v0.4.0 h1:yy3CSBmM17klWsRemfqlDI9Ai8wuw7tVntVH1XQcHhc=
github.com/brianstrauch/spotify v0.4.0/go.mod h1:sImRT74obai+LNgFVxr+qNVq2yt5KEfvNxz6ZDfYWaA=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
Expand Down
41 changes: 41 additions & 0 deletions internal/playlist/list.go
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
}
15 changes: 15 additions & 0 deletions internal/playlist/playlist.go
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
}
76 changes: 76 additions & 0 deletions internal/playlist/show.go
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
}
8 changes: 4 additions & 4 deletions internal/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Show(playback *spotify.Playback) string {
isPlayingEmoji = "⏸"
}

progressBar := showProgressBar(playback.ProgressMs, playback.Item.DurationMs)
progressBar := showProgressBar(playback.ProgressMs, playback.Item.Duration)

status := prefixLineWithEmoji("🎵", playback.Item.Name)
status += prefixLineWithEmoji("🎤", artistLine)
Expand All @@ -77,9 +77,9 @@ func joinArtists(artists []spotify.Artist) string {
return list
}

func showProgressBar(progress, duration int) string {
func showProgressBar(progress int, duration *spotify.Duration) string {
const length = 16
bars := length * progress / duration
bars := length * progress / int(duration.Milliseconds())

status := fmt.Sprintf("%s [", formatTime(progress))
for i := 0; i < bars; i++ {
Expand All @@ -88,7 +88,7 @@ func showProgressBar(progress, duration int) string {
for i := bars; i < length; i++ {
status += " "
}
status += fmt.Sprintf("] %s", formatTime(duration))
status += fmt.Sprintf("] %s", formatTime(int(duration.Milliseconds())))

return status
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"spotify/internal/p"
"spotify/internal/pause"
"spotify/internal/play"
"spotify/internal/playlist"
"spotify/internal/queue"
"spotify/internal/repeat"
"spotify/internal/save"
Expand Down Expand Up @@ -50,6 +51,7 @@ func main() {
root.AddCommand(status.NewCommand())
root.AddCommand(unsave.NewCommand())
root.AddCommand(update.NewCommand())
root.AddCommand(playlist.NewCommand())

// Hide help command
root.SetHelpCommand(&cobra.Command{Hidden: true})
Expand Down

0 comments on commit 36658c0

Please sign in to comment.