Skip to content
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

implement new sync options #21

Merged
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
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,
}
}
}
Loading