Skip to content

Commit

Permalink
Merge pull request #31 from mateoradman/28-add-option-to-run-actions-…
Browse files Browse the repository at this point in the history
…on-a-specific-list-of-items

#28: Filter items by sonarr/radarr ids
  • Loading branch information
mateoradman authored May 28, 2024
2 parents d9bb130 + 3b4188d commit 054a4d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Action {
pub client: ClientWithMiddleware,
pub base_url: Url,
pub action: ActionCommands,
pub ids: Vec<u32>,
pub offset: u32,
pub limit: Option<u32>,
pub pb: ProgressBar,
Expand All @@ -29,22 +30,31 @@ impl Action {
client,
base_url,
action: ActionCommands::OCRFixes,
ids: Vec::new(),
offset: 0,
limit: None,
pb,
}
}

async fn get_all<T>(
&self,
mut url: Url,
limit_records: bool,
) -> Result<PaginatedResponse<T>, Box<dyn std::error::Error>>
async fn get_all<T>(&self, url: Url) -> Result<PaginatedResponse<T>, Box<dyn std::error::Error>>
where
T: DeserializeOwned,
T: Debug,
{
if limit_records {
let req = self.client.get(url);
let res = req.send().await?;
let body: PaginatedResponse<T> = res.json().await?;
Ok(body)
}

async fn limit_records(&self, mut url: Url, query_param: &str) -> Url {
if !self.ids.is_empty() {
for id in &self.ids {
url.query_pairs_mut()
.append_pair(query_param, &id.to_string());
}
} else if self.limit.is_some() || self.offset > 0 {
let length = match self.limit {
Some(val) => val,
None => std::u32::MAX,
Expand All @@ -53,10 +63,7 @@ impl Action {
.append_pair("length", &length.to_string())
.append_pair("start", &self.offset.to_string());
}
let req = self.client.get(url);
let res = req.send().await?;
let body: PaginatedResponse<T> = res.json().await?;
Ok(body)
url
}

async fn perform(
Expand Down Expand Up @@ -168,7 +175,8 @@ impl Action {
);
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap().push("movies");
let response = self.get_all::<Movie>(url, true).await?;
url = self.limit_records(url, "radarrid[]").await;
let response = self.get_all::<Movie>(url).await?;
let num_movies: u64 = response.data.len() as u64;
if num_movies == 0 {
self.pb.finish_with_message("No movies found");
Expand Down Expand Up @@ -199,7 +207,8 @@ impl Action {
);
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap().push("series");
let response = self.get_all::<TVShow>(url.clone(), true).await?;
url = self.limit_records(url, "seriesid[]").await;
let response = self.get_all::<TVShow>(url.clone()).await?;
let num_series: u64 = response.data.len() as u64;
if num_series == 0 {
pb_main.finish_with_message("No tv shows found");
Expand All @@ -219,7 +228,7 @@ impl Action {
let query_param = format!("seriesid[]={}", series.sonarr_series_id);
let mut new_url = url.clone();
new_url.set_query(Some(&query_param));
let response = self.get_all::<Episode>(new_url, false).await?;
let response = self.get_all::<Episode>(new_url).await?;
let num_episodes: u64 = response.data.len() as u64;
sub_pb.set_length(num_episodes);
if num_episodes == 0 {
Expand Down
11 changes: 8 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{actions::Action, connection::check_health, data_types::app_config::A
#[command(about = "Performs bulk operations on subtitles of movies and tv shows using Bazarr's API", long_about = None)]
pub struct Cli {
/// Path to the JSON configuration file
#[arg(short, long, value_name = "FILE")]
#[arg(required = true, short, long, value_name = "FILE")]
pub config: PathBuf,

/// Number of times to retry the request in case of lost connection
Expand All @@ -39,10 +39,13 @@ impl Cli {

#[derive(clap::Args)]
pub struct CommonArgs {
/// Skip N records
/// Filter records by Sonarr/Radarr ID (comma-separated)
#[arg(long, required = false, value_delimiter = ',')]
ids: Vec<u32>,
/// Skip N records (ignored if ids are specified) [default: skip none]
#[arg(long, default_value_t = 0)]
offset: u32,
/// Limit to N records [default: unlimited]
/// Limit to N records (ignored if ids are specified) [default: unlimited]
#[arg(long)]
limit: Option<u32>,
/// List available actions
Expand Down Expand Up @@ -86,12 +89,14 @@ impl Commands {
match self {
Commands::Movies(c) => {
action.action = c.subcommand;
action.ids = c.ids;
action.limit = c.limit;
action.offset = c.offset;
action.movies().await
}
Commands::TVShows(c) => {
action.action = c.subcommand;
action.ids = c.ids;
action.limit = c.limit;
action.offset = c.offset;
action.tv_shows().await
Expand Down

0 comments on commit 054a4d1

Please sign in to comment.