Skip to content

Commit

Permalink
Merge pull request #57 from nanozuki/episode_offset
Browse files Browse the repository at this point in the history
Episode offset option
  • Loading branch information
nanozuki committed Jul 31, 2023
2 parents 9314be8 + 2ade644 commit 04facbb
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 241 deletions.
603 changes: 382 additions & 221 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ vendored = ["hyper-tls/vendored"]
clap = { version = "3.1.18", features = ["derive", "cargo", "unicode", "wrap_help"] }
serde = { version = "1.0.137", features = ["derive", "rc"] }
serde_json = "1.0.81"
serde_repr = "0.1.8"
serde_repr = "0.1.16"
hyper = { version = "0.14.18", features = ["http1", "http2", "client", "runtime"] }
tokio = { version = "1.18.2", features = ["full"] }
anyhow = "1.0.57"
Expand Down
2 changes: 2 additions & 0 deletions examples/source/呪術廻戦 第2期/dantalian.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
subject_id = 369304
episode_offset = 24
Empty file.
2 changes: 2 additions & 0 deletions examples/source/無職転生Ⅱ/dantalian.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
subject_id = 373247
episode_re = "^(?P<name>無職転生Ⅱ) (?P<sp>SP)?(?P<ep>[.\\d]+)\\."
Empty file.
24 changes: 14 additions & 10 deletions src/dantalian/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use std::path::Path;
struct ConfigFile {
subject_id: u32,
episode_re: Option<String>,
episode_offset: Option<i32>,
}

#[derive(Debug)]
pub struct Config {
pub subject_id: u32,
pub episode_re: Regex,
pub episode_offset: i32,
}

const DIR_CONFIG_NAME: &str = "dantalian.toml";
Expand All @@ -37,20 +39,20 @@ impl Config {
info!(ind: 2, "Parse config file");
let file = std::fs::read_to_string(filepath)?;
let cf: ConfigFile = toml::from_str(file.as_ref())?;
match cf.episode_re {
Some(re) => Ok(Config {
subject_id: cf.subject_id,
episode_re: Regex::new(&re)?,
}),
let episode_re = match cf.episode_re {
Some(re) => Regex::new(&re)?,
None => {
let subject = get_subject(cf.subject_id).await?;
let name_qry = format!("{}|{}", subject.name, subject.name_cn);
Ok(Config {
subject_id: cf.subject_id,
episode_re: default_ep_regex(&name_qry)?,
})
default_ep_regex(&name_qry)?
}
}
};
let episode_offset = cf.episode_offset.unwrap_or(0);
Ok(Config {
subject_id: cf.subject_id,
episode_re,
episode_offset,
})
}

async fn parse_from_dirname(path: &Path) -> Result<Config> {
Expand All @@ -70,6 +72,7 @@ impl Config {
Ok(Config {
subject_id: subjects[0].id,
episode_re: default_ep_regex(&name)?,
episode_offset: 0,
})
}
None => bail!("invalid name"),
Expand All @@ -80,6 +83,7 @@ impl Config {
let file_content = toml::to_string(&ConfigFile {
subject_id: self.subject_id,
episode_re: Some(self.episode_re.to_string()),
episode_offset: Some(self.episode_offset),
})?;
let mut f = File::create(filepath)?;
f.write_all(&file_content.into_bytes())?;
Expand Down
20 changes: 11 additions & 9 deletions src/dantalian/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ impl Job {
// if this file is not video file, skip it.
return Ok(None);
}
let file_name = match file_entry.file_name().to_str() {
Some(f) => f,
None => return Ok(None),
};
let Some(file_name) = file_entry.file_name().to_str() else { return Ok(None) };
let nfo_file_path = file_entry.path().with_extension("nfo");
if (!force) && nfo_file_path.exists() {
// nfo file of current file already exists, don't need a job
return Ok(None);
}
let caps = config.episode_re.captures(file_name);
let ep: String = match caps.as_ref().and_then(|c| c.name("ep")) {
Some(ep_match) => {
String::from(ep_match.as_str().parse::<String>()?.trim_start_matches('0'))
}
None => return Ok(None),
let Some(matched_ep) = caps.as_ref().and_then(|c| c.name("ep")) else { return Ok(None) };
let ep_str = match matched_ep.as_str().trim_start_matches('0') {
"" => "0",
ep_str => ep_str,
};
let ep = match config.episode_offset {
0 => ep_str.to_string(),
offset => ep_str
.parse::<f64>()
.map_or_else(|s| s.to_string(), |e| (e + offset as f64).to_string()),
};
let sp = caps
.and_then(|c| c.name("sp"))
Expand Down

0 comments on commit 04facbb

Please sign in to comment.