aome510 / spotify-player

A Spotify player in the terminal with full feature parity
MIT License
3.41k stars 149 forks source link

Support handling CLI commands without a running `spotify_player` instance #279

Closed chrisgrieser closed 10 months ago

chrisgrieser commented 11 months ago

Is your feature request related to a problem? Please describe. spotify_player playback … requires a running instance of spotify_player. However, when playing via Spotify Connect, you do not always really need a daemon or the TUI running, often something like spotify_player playback next is enough.

Describe the solution you'd like A way to send playback commands to a Spotify Connect device. spotify-tui allows you to do this via spt playback --next – no daemon, no running TUI, affecting the Spotify Connect Device.

Describe alternatives you've considered Running spotifiy_player in the background all the time, even if not needed.

Additional context Looking to replace the unmaintained spotify-tui. Basically, this is the only feature missing that I need before being able to switch.

Thanks for the cli, its UI is much better than of other players I tried.

aome510 commented 11 months ago

The design for CLI support between spotify_player and spotify-tui are different. spotify-tui treats CLI as a separate application, while spotify_player treats CLI as a tool to interact with the main application.

Because CLI is a separate application, each time spotify-tui want to handle a CLI command, e.g player next, it will need to initialize a Spotify client from scratch, which includes fetching credentials. The credential fetching part can make handling the CLI command slow. spotify_player CLI handles those commands instantly. Of course, this is a trade-off between flexibility and performance.

Currently, the spotify_player CLI architecture is implemented based on the assumption that it's a tool to communicate with the main application. Will need to find a good way to abstract the codes to support both modes.

catgoose commented 10 months ago

I used to use spotify_tui in neovim with the toggle term plugin. It was simple to open a floating window to control spotify. I don't really want to have an application running, I would really like to dispatch commands to a CLI when neaded.

aome510 commented 10 months ago

@catgoose @chrisgrieser the support for running CLI commands without a running instance is implemented in https://github.com/aome510/spotify-player/pull/290.

Can you guys checkout master and run the application cargo run --release -- {command} to see it works as expected?

Just a note that without a running instance, the latency of the command will increase significantly (~100x based in my local machine):

Without a running instance

  • playback next
    $ time test_spotify_player playback next
    ________________________________________________________
    Executed in    1.15 secs      fish           external
    usr time   28.03 millis    0.23 millis   27.80 millis
    sys time   18.36 millis    1.83 millis   16.54 millis
  • get key playback
> time test_spotify_player get key playback
________________________________________________________
Executed in    1.27 secs      fish           external
   usr time   29.29 millis    0.25 millis   29.04 millis
   sys time   23.85 millis    2.13 millis   21.71 millis

With a running instance

  • playback next
    > time test_spotify_player playback next
    ________________________________________________________
    Executed in   14.94 millis    fish           external
    usr time    7.58 millis    0.19 millis    7.39 millis
    sys time    5.54 millis    1.54 millis    4.00 millis
  • get key playback
> time test_spotify_player get key playback
________________________________________________________
Executed in   16.28 millis    fish           external
   usr time    7.54 millis    0.19 millis    7.35 millis
   sys time    5.90 millis    1.63 millis    4.27 millis

The bottleneck seems to come from network requests to get API token and send Spotify APIs. I will be surprised if spotify-tui CLI's latency is much better than this.

chrisgrieser commented 10 months ago

yeah, spotify-tui also has 1-2 seconds of latency for that. That's ok.

Just checked out master the common commands (playback next, playback play-pause) work as expected on my end :)

catgoose commented 10 months ago

prev, next, play-pause work for me! Could there be an argument to volume to increase/decrease by a percentage? My use case is to use neovim key bindings to do prev/nex/pause and lower/raise volume, so if I could adjust volume in that way it would be really nice!

aome510 commented 10 months ago

prev, next, play-pause work for me! Could there be an argument to volume to increase/decrease by a percentage? My use case is to use neovim key bindings to do prev/nex/pause and lower/raise volume, so if I could adjust volume in that way it would be really nice!

spotify_player supports increasing/decreasing by an offset with --offset option:

> spotify_player playback volume -h
Set the volume percentage

Usage: spotify_player playback volume [OPTIONS] <percent>

Arguments:
  <percent>

Options:
      --offset  Increase the volume percent by an offset

Decreasing just means using negative value.

catgoose commented 10 months ago

prev, next, play-pause work for me! Could there be an argument to volume to increase/decrease by a percentage? My use case is to use neovim key bindings to do prev/nex/pause and lower/raise volume, so if I could adjust volume in that way it would be really nice!

spotify_player supports increasing/decreasing by an offset with --offset option:

> spotify_player playback volume -h
Set the volume percentage

Usage: spotify_player playback volume [OPTIONS] <percent>

Arguments:
  <percent>

Options:
      --offset  Increase the volume percent by an offset

Decreasing just means using negative value.

❯ cargo run --release -- playback volume --offset 10
    Finished release [optimized + debuginfo] target(s) in 0.19s
     Running `target/release/spotify_player playback volume --offset 10`
Bad request: http error: status code 400 Bad Request

I get a similar error if I try to set volume to a percentage:

❯ cargo run --release -- playback volume 30
    Finished release [optimized + debuginfo] target(s) in 0.19s
     Running `target/release/spotify_player playback volume 30`
Bad request: http error: status code 403 Forbidden
aome510 commented 10 months ago

prev, next, play-pause work for me! Could there be an argument to volume to increase/decrease by a percentage? My use case is to use neovim key bindings to do prev/nex/pause and lower/raise volume, so if I could adjust volume in that way it would be really nice!

spotify_player supports increasing/decreasing by an offset with --offset option:

> spotify_player playback volume -h
Set the volume percentage

Usage: spotify_player playback volume [OPTIONS] <percent>

Arguments:
  <percent>

Options:
      --offset  Increase the volume percent by an offset

Decreasing just means using negative value.

❯ cargo run --release -- playback volume --offset 10
    Finished release [optimized + debuginfo] target(s) in 0.19s
     Running `target/release/spotify_player playback volume --offset 10`
Bad request: http error: status code 400 Bad Request

I get a similar error if I try to set volume to a percentage:

❯ cargo run --release -- playback volume 30
    Finished release [optimized + debuginfo] target(s) in 0.19s
     Running `target/release/spotify_player playback volume 30`
Bad request: http error: status code 403 Forbidden

weird, it works well for me (both command) to change volume of the official Spotify app. Bad request may mean that there is no active playback.