Skip to content

Commit

Permalink
add CLI search command (#432)
Browse files Browse the repository at this point in the history
Resolves #425
  • Loading branch information
kadinsayani authored Apr 26, 2024
1 parent 049136b commit 1926026
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spotify_player/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ async fn handle_socket_request(
let resp = handle_playlist_request(client, command).await?;
Ok(resp.into_bytes())
}
Request::Search { query } => {
let resp = handle_search_request(client, query).await?;
Ok(resp)
}
}
}

Expand Down Expand Up @@ -310,6 +314,12 @@ async fn handle_get_item_request(
})
}

async fn handle_search_request(client: &Client, query: String) -> Result<Vec<u8>> {
let search_result = client.search(&query).await?;

Ok(serde_json::to_vec(&search_result)?)
}

async fn handle_playback_request(
client: &Client,
state: &Option<SharedState>,
Expand Down
6 changes: 6 additions & 0 deletions spotify_player/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ pub fn init_playback_subcommand() -> Command {
)
}

pub fn init_search_command() -> Command {
Command::new("search")
.about("Search spotify")
.arg(Arg::new("query").help("Search query").required(true))
}

pub fn init_like_command() -> Command {
Command::new("like")
.about("Like currently playing track")
Expand Down
6 changes: 6 additions & 0 deletions spotify_player/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ pub fn handle_cli_subcommand(cmd: &str, args: &ArgMatches) -> Result<()> {
"like" => Request::Like {
unlike: args.get_flag("unlike"),
},
"search" => Request::Search {
query: args
.get_one::<String>("query")
.expect("query is required")
.to_owned(),
},
_ => unreachable!(),
};

Expand Down
2 changes: 2 additions & 0 deletions spotify_player/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub enum Request {
Connect(IdOrName),
Like { unlike: bool },
Playlist(PlaylistCommand),
Search { query: String },
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -161,6 +162,7 @@ pub fn init_cli() -> anyhow::Result<clap::Command> {
.subcommand(commands::init_authenticate_command())
.subcommand(commands::init_playlist_subcommand())
.subcommand(commands::init_generate_command())
.subcommand(commands::init_search_command())
.arg(
clap::Arg::new("theme")
.short('t')
Expand Down
2 changes: 1 addition & 1 deletion spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ impl Client {
}

/// Search for items (tracks, artists, albums, playlists) matching a given query
async fn search(&self, query: &str) -> Result<SearchResults> {
pub async fn search(&self, query: &str) -> Result<SearchResults> {
let (track_result, artist_result, album_result, playlist_result) = tokio::try_join!(
self.search_specific_type(query, rspotify_model::SearchType::Track),
self.search_specific_type(query, rspotify_model::SearchType::Artist),
Expand Down
2 changes: 1 addition & 1 deletion spotify_player/src/state/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub enum Playback {
URIs(Vec<TrackId<'static>>, Option<rspotify_model::Offset>),
}

#[derive(Default, Clone, Debug)]
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
/// Data returned when searching a query using Spotify APIs.
pub struct SearchResults {
pub tracks: Vec<Track>,
Expand Down

0 comments on commit 1926026

Please sign in to comment.