Skip to content

Commit

Permalink
Merge pull request #21 from mateoradman/12-enhancement-implement-new-…
Browse files Browse the repository at this point in the history
…sync-subtitles-options

implement new sync options
  • Loading branch information
mateoradman authored May 3, 2024
2 parents 77df2f4 + e74c4d4 commit 497c792
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
27 changes: 12 additions & 15 deletions src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, process::exit};
use std::{borrow::Borrow, fmt::Debug, process::exit};

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use reqwest::Url;
Expand Down Expand Up @@ -28,7 +28,7 @@ impl Action {
Self {
client,
base_url,
action: ActionCommands::Sync,
action: ActionCommands::OCRFixes,
offset: 0,
limit: None,
pb,
Expand Down Expand Up @@ -61,12 +61,18 @@ impl Action {

async fn perform(
&self,
payload: ActionPayload,
mut payload: ActionPayload,
) -> Result<reqwest::Response, reqwest_middleware::Error> {
let mut url = self.base_url.clone();
url.path_segments_mut().unwrap().push("subtitles");
let action_string: String = self.action.to_string();
url.query_pairs_mut().append_pair("action", &action_string);
if let ActionCommands::Sync(sync_options) = &self.action.borrow() {
payload.reference = sync_options.reference.clone();
payload.max_offset_seconds = sync_options.max_offset_seconds;
payload.no_fix_framerate = Some(sync_options.no_fix_framerate);
payload.gss = Some(sync_options.gss);
}
self.client.patch(url).json(&payload).send().await
}

Expand All @@ -82,12 +88,8 @@ impl Action {
subtitle.audio_language_item.name,
episode.title,
));
let payload = ActionPayload {
id: episode.sonarr_episode_id,
media_type: String::from("episode"),
language: subtitle.audio_language_item.code2.unwrap(),
path: subtitle.path.unwrap(),
};

let payload = ActionPayload::new(episode.sonarr_episode_id, "episode", &subtitle);
match self.perform(payload).await {
Ok(res) => match res.error_for_status() {
Ok(_) => {
Expand Down Expand Up @@ -128,12 +130,7 @@ impl Action {
subtitle.audio_language_item.name,
movie.title,
));
let payload = ActionPayload {
id: movie.radarr_id,
media_type: String::from("movie"),
language: subtitle.audio_language_item.code2.unwrap(),
path: subtitle.path.unwrap(),
};
let payload = ActionPayload::new(movie.radarr_id, "movie", &subtitle);
match self.perform(payload).await {
Ok(res) => match res.error_for_status() {
Ok(_) => {
Expand Down
23 changes: 19 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::{Parser, Subcommand, ValueEnum};
use clap::{Parser, Subcommand};
use reqwest::{header, Client, Url};
use reqwest_middleware::ClientBuilder;
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
Expand Down Expand Up @@ -101,10 +101,10 @@ impl Commands {
}
}

#[derive(Subcommand, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, ValueEnum)]
#[derive(Subcommand, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ActionCommands {
/// Sync all
Sync,
Sync(SyncOptions),
/// Perform OCR fixes
OCRFixes,
/// Perform common fixes
Expand All @@ -122,7 +122,7 @@ pub enum ActionCommands {
impl ToString for ActionCommands {
fn to_string(&self) -> String {
match self {
ActionCommands::Sync => "sync".to_string(),
ActionCommands::Sync(_) => "sync".to_string(),
ActionCommands::OCRFixes => "OCR_fixes".to_string(),
ActionCommands::CommonFixes => "common".to_string(),
ActionCommands::RemoveHearingImpaired => "remove_HI".to_string(),
Expand All @@ -132,3 +132,18 @@ impl ToString for ActionCommands {
}
}
}
#[derive(clap::Args, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SyncOptions {
/// Reference for sync from video file track number (a:0), subtitle (s:0), or some subtitles file path
#[arg(short)]
pub reference: Option<String>,
/// Seconds of offset allowed when syncing [default: null]
#[arg(short, value_name = "MAX OFFSET")]
pub max_offset_seconds: Option<u32>,
/// Do not attempt to fix framerate [default: false]
#[arg(short, default_value_t = false)]
pub no_fix_framerate: bool,
/// Use Golden-Section search [default: false]
#[arg(short, default_value_t = false)]
pub gss: bool,
}
27 changes: 27 additions & 0 deletions src/data_types/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use super::response::Subtitle;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, ValueEnum)]
pub enum MediaType {
Movie,
Expand All @@ -27,4 +29,29 @@ pub struct ActionPayload {
pub media_type: String,
pub language: String,
pub path: String,

// used only for sync action
#[serde(skip_serializing_if = "Option::is_none")]
pub reference: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_offset_seconds: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub no_fix_framerate: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gss: Option<bool>,
}

impl ActionPayload {
pub fn new(id: u32, media_type: &str, subtitle: &Subtitle) -> Self {
ActionPayload {
id,
media_type: String::from(media_type),
language: subtitle.audio_language_item.code2.clone().unwrap(),
path: subtitle.path.clone().unwrap(),
reference: None,
max_offset_seconds: None,
no_fix_framerate: None,
gss: None,
}
}
}

0 comments on commit 497c792

Please sign in to comment.