8000 feat: start playback for track from cli by juliamertz · Pull Request #649 · aome510/spotify-player · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: start playback for track from cli #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
< 8000 tr data-hunk="cbb314330510c764dfd9c52e96f8af936ccce8a122558e0904ffebec4d43a7a1" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ To enable [fuzzy search](https://en.wikipedia.org/wiki/Approximate_string_matchi

- `get`: Get Spotify data (playlist/album/artist data, user's data, etc)
- `playback`: Interact with the playback (start a playback, play-pause, next, etc)
- `search`: Search spotify
- `connect`: Connect to a Spotify device
- `like`: Like currently playing track
- `authenticate`: Authenticate the application
Expand All @@ -287,6 +288,18 @@ For more details, run `spotify_player -h` or `spotify_player {command} -h`, in w
- When using the CLI for the first time, you'll need to run `spotify_player authenticate` to authenticate the application beforehand.
- Under the hood, CLI command is handled by sending requests to a `spotify_player` client socket running on port `client_port`, [a general application configuration](https://github.com/aome510/spotify-player/blob/master/docs/config.md#general) with a default value of `8080`. If there is no running application's instance, a new client will be created upon handling the CLI commands, which increases the latency of the command.

#### Scripting

The `spotify_player` command-line interface makes scripting easy.
With the `search` subcommand, you can search Spotify and retrieve data in JSON format, enabling queries with tools like [jq](https://jqlang.github.io/jq/).

Here’s an example of starting playback for the first track from a search query:

```sh
read -p "Search spotify: " query
spotify_player playback start track --id $(spotify_player search "$query" | jq '.tracks.[0].id' | xargs)
```

## Commands

To go to the shortcut help page, press `?` or `C-h` (default shortcuts for `OpenCommandHelp` command).
Expand Down
9 changes: 9 additions & 0 deletions spotify_player/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ async fn handle_playback_request(

PlayerRequest::StartPlayback(Playback::Context(context_id, None), Some(shuffle))
}
Command::StartTrack(id_or_name) => {
let ItemId::Track(id) = get_spotify_id(client, ItemType::Track, id_or_name).await?
else {
anyhow::bail!("Unable to get track id")
};

let track = client.track(id).await?;
PlayerRequest::StartPlayback(Playback::URIs(vec![track.id.into()], None), None)
}
Command::PlayPause => PlayerRequest::ResumePause,
Command::Play => PlayerRequest::Resume,
Command::Pause => PlayerRequest::Pause,
Expand Down
3 changes: 3 additions & 0 deletions spotify_player/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ fn init_playback_start_subcommand() -> Command {
.help("Shuffle tracks within the launched playback"),
),
))
.subcommand(add_id_or_name_group(
Command::new("track").about("Start playback for a track"),
))
.subcommand(
Command::new("liked")
.about("Start a liked tracks playback")
Expand Down
1 change: 1 addition & 0 deletions spotify_player/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn handle_playback_subcommand(args: &ArgMatches) -> Result<Request> {
let (cmd, args) = args.subcommand().expect("playback subcommand is required");
let command = match cmd {
"start" => match args.subcommand() {
Some(("track", args)) => Command::StartTrack(get_id_or_name(args)),
Some(("context", args)) => {
let context_type = args
.get_one::<ContextType>("context_type")
Expand Down
1 change: 1 addition & 0 deletions spotify_player/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum Command {
id_or_name: IdOrName,
shuffle: bool,
},
StartTrack(IdOrName),
StartLikedTracks {
limit: usize,
random: bool,
Expand Down
Loading
0