Skip to content

Commit

Permalink
queue songs by name
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstrauch committed May 13, 2021
1 parent 5410232 commit a0fffbd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ Get the latest version <a href="https://github.com/brianstrauch/spotify-cli/rele
```
$ spotify login
Success!
$ spotify play
$ spotify play back pocket
🎵 Back Pocket
🎤 Vulfpeck
▶️ 1:30 [======== ] 3:01
$ spotify pause
🎵 Back Pocket
🎤 Vulfpeck
⏸ 1:30 [======== ] 3:01
$ spotify queue blinding lights
Queued!
$ spotify next
🎵 Blinding Lights
🎤 The Weeknd
Expand All @@ -39,8 +41,6 @@ $ spotify shuffle
🔀 Shuffle on
$ spotify repeat
🔁 Repeat on
$ spotify repeat
🔂 Repeat track
```

## Aliases
Expand All @@ -61,6 +61,10 @@ $ spotify repeat
<td><code>spotify pause</code></td>
<td><code>spotify p</code></td>
</tr>
<tr>
<td><code>spotify queue</code></td>
<td><code>spotify q</code></td>
</tr>
<tr>
<td><code>spotify status</code></td>
<td><code>spotify s</code></td>
Expand Down
11 changes: 1 addition & 10 deletions internal/play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Play(api pkg.APIInterface, query string) (string, error) {
var uri string

if len(query) > 0 {
uri, err = Search(api, query)
uri, err = internal.Search(api, query)
if err != nil {
return "", err
}
Expand All @@ -71,12 +71,3 @@ func Play(api pkg.APIInterface, query string) (string, error) {

return status.Show(playback), nil
}

func Search(api pkg.APIInterface, query string) (string, error) {
page, err := api.Search(query, 1)
if err != nil {
return "", err
}

return page.Tracks.Items[0].URI, nil
}
42 changes: 42 additions & 0 deletions internal/queue/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package queue

import (
"spotify/internal"
"spotify/pkg"
"strings"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "queue song",
Aliases: []string{"q"},
Short: "Queue a specific song.",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
api, err := internal.Authenticate()
if err != nil {
return err
}

query := strings.Join(args, " ")

if err := Queue(api, query); err != nil {
return err
}

cmd.Println("Queued!")
return nil
},
}
}

func Queue(api pkg.APIInterface, query string) error {
uri, err := internal.Search(api, query)
if err != nil {
return err
}

return api.Queue(uri)
}
12 changes: 12 additions & 0 deletions internal/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package internal

import "spotify/pkg"

func Search(api pkg.APIInterface, query string) (string, error) {
page, err := api.Search(query, 1)
if err != nil {
return "", err
}

return page.Tracks.Items[0].URI, nil
}
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/queue"
"spotify/internal/repeat"
"spotify/internal/save"
"spotify/internal/shuffle"
Expand Down Expand Up @@ -46,6 +47,7 @@ func main() {
root.AddCommand(p.NewCommand())
root.AddCommand(pause.NewCommand())
root.AddCommand(play.NewCommand())
root.AddCommand(queue.NewCommand())
root.AddCommand(repeat.NewCommand())
root.AddCommand(save.NewCommand())
root.AddCommand(shuffle.NewCommand())
Expand Down
9 changes: 9 additions & 0 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type APIInterface interface {
Next() error
Pause() error
Play(uri string) error
Queue(uri string) error
Repeat(state string) error
Save(id string) error
Search(queue string, limit int) (*model.Page, error)
Expand Down Expand Up @@ -72,6 +73,14 @@ func (a *API) Play(uri string) error {
return err
}

func (a *API) Queue(uri string) error {
q := url.Values{}
q.Add("uri", uri)

_, err := a.call(http.MethodPost, "/me/player/queue?"+q.Encode(), nil)
return err
}

func (a *API) Repeat(state string) error {
q := url.Values{}
q.Add("state", state)
Expand Down
5 changes: 5 additions & 0 deletions pkg/api_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (m *MockAPI) Play(uri string) error {
return args.Error(0)
}

func (m *MockAPI) Queue(uri string) error {
args := m.Called(uri)
return args.Error(0)
}

func (m *MockAPI) Repeat(state string) error {
args := m.Called(state)
return args.Error(0)
Expand Down

0 comments on commit a0fffbd

Please sign in to comment.