fix(regex): Fix show regex to work better with downloaded anime

This commit is contained in:
Andreas Mieke 2024-02-21 10:01:12 -05:00
parent 7c10c8c90a
commit e0d52625f2
2 changed files with 30 additions and 6 deletions

2
Cargo.lock generated
View file

@ -841,7 +841,7 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "plex-media-ingest"
version = "0.1.0"
version = "1.0.0"
dependencies = [
"clap",
"home",

View file

@ -8,7 +8,7 @@ use serde::Deserialize;
use urlencoding::encode;
use walkdir::WalkDir;
use inline_colorization::*;
use regex::RegexBuilder;
use regex::{Captures, RegexBuilder};
use crate::{config::Config, media::{Move, self, get_file_header}, directory::search_path};
@ -157,12 +157,36 @@ fn check_show_file(file: PathBuf, primary_media: &mut Option<TMDBEntry>, cfg: &C
trace!("Checking {:#?}", file);
match get_file_header(file.clone()) {
Ok(header) => {
let season :i32;
let episode :i32;
let caps :Captures;
// Try to parse Season/Episode from filename
let re = RegexBuilder::new(r"(?:S(?<season0>[0-9]+)\.?E(?<episode0>[0-9]+)|(?<season1>[0-9]+)x(?<episode1>[0-9]+))")
let re = RegexBuilder::new(r"(?:S(?<season0>[0-9]+).?E(?<episode0>[0-9]+)|(?<season1>[0-9]+)x(?<episode1>[0-9]+))")
.case_insensitive(true).build().unwrap();
let Some(caps) = re.captures(file.to_str().unwrap_or_default()) else { warn!("Regex doesn't match {:#?}, skipping", file); return; };
let season: i32 = caps.name("season0").map_or_else(||caps.name("season1").map_or("", |m| m.as_str()), |m| m.as_str()).parse().unwrap();
let episode: i32 = caps.name("episode0").map_or_else(||caps.name("episode1").map_or("", |m| m.as_str()), |m| m.as_str()).parse().unwrap();
if re.is_match(file.to_str().unwrap_or_default()) {
caps = re.captures(file.to_str().unwrap_or_default()).unwrap()
} else {
// Try alternate regex that includes folder name
let re = RegexBuilder::new(r"(?:Season (?<season0>[0-9]+)[\/\\][\D]+?(?<episode0>[0-9]+))")
.case_insensitive(true).build().unwrap();
if re.is_match(file.to_str().unwrap_or_default()) {
caps = re.captures(file.to_str().unwrap_or_default()).unwrap()
} else {
warn!("Regex doesn't match {:#?}, skipping", file);
return;
}
}
season = caps.name("season0").map_or_else(
||caps.name("season1").map_or_else(
||caps.name("season2").map_or(
"", |m| m.as_str()), |m| m.as_str()), |m| m.as_str()).parse().unwrap();
episode = caps.name("episode0").map_or_else(
||caps.name("episode1").map_or_else(
||caps.name("episode2").map_or(
"", |m| m.as_str()), |m| m.as_str()), |m| m.as_str()).parse().unwrap();
trace!("Found Season {0:02}, Episode {1:02}", season, episode);
// Handle video files