Enumerate show episodes lazily and don't cache the results
This commit is contained in:
parent
3113a50169
commit
4081056224
|
@ -27,7 +27,14 @@ async fn main() {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let (title, show) = shows.iter().choose(&mut rng).expect("No shows found!");
|
let (title, show) = shows.iter().choose(&mut rng).expect("No shows found!");
|
||||||
let (num, file) = show.episodes.iter().choose(&mut rng).unwrap();
|
let episodes = match show.episodes() {
|
||||||
|
Ok(eps) => eps,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to get episodes for {}: {}", title, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (num, file) = episodes.iter().choose(&mut rng).unwrap();
|
||||||
|
|
||||||
let descriptor = format!(
|
let descriptor = format!(
|
||||||
"{}{}",
|
"{}{}",
|
||||||
|
|
|
@ -6,7 +6,7 @@ use serde::Deserialize;
|
||||||
mod enumeration;
|
mod enumeration;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct ShowSpec {
|
pub struct Show {
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub tags: Vec<String>,
|
pub tags: Vec<String>,
|
||||||
}
|
}
|
||||||
|
@ -20,46 +20,24 @@ pub enum EpisodeNumber {
|
||||||
|
|
||||||
type Episodes = HashMap<EpisodeNumber, PathBuf>;
|
type Episodes = HashMap<EpisodeNumber, PathBuf>;
|
||||||
|
|
||||||
pub struct Show {
|
|
||||||
pub episodes: Episodes,
|
|
||||||
pub tags: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load(shows_file: PathBuf) -> HashMap<String, Show> {
|
pub fn load(shows_file: PathBuf) -> HashMap<String, Show> {
|
||||||
let show_specs: HashMap<String, ShowSpec> =
|
|
||||||
serde_yaml::from_reader(fs::File::open(shows_file).expect("Failed to open shows file"))
|
serde_yaml::from_reader(fs::File::open(shows_file).expect("Failed to open shows file"))
|
||||||
.expect("Failed to parse YAML from shows file");
|
.expect("Failed to parse YAML from shows file")
|
||||||
|
|
||||||
show_specs
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|(name, show)| {
|
|
||||||
debug!("Enumerating show {}: {}", name, show.path.display());
|
|
||||||
load_path(show.path)
|
|
||||||
.map_err(|e| {
|
|
||||||
error!("Error processing {}: {}", name, e);
|
|
||||||
})
|
|
||||||
.ok()
|
|
||||||
.map(|eps| {
|
|
||||||
(
|
|
||||||
name,
|
|
||||||
Show {
|
|
||||||
episodes: eps,
|
|
||||||
tags: show.tags,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_path(path: PathBuf) -> std::io::Result<Episodes> {
|
impl Show {
|
||||||
let metadata = fs::metadata(&path)?;
|
pub fn episodes(&self) -> std::io::Result<Episodes> {
|
||||||
|
let path = &self.path;
|
||||||
|
let metadata = fs::metadata(path)?;
|
||||||
if metadata.is_file() {
|
if metadata.is_file() {
|
||||||
debug!("{} is a file, standalone", path.display());
|
debug!("{} is a file, standalone", path.display());
|
||||||
Ok(HashMap::from([(EpisodeNumber::Standalone, path)]))
|
Ok(HashMap::from([(
|
||||||
|
EpisodeNumber::Standalone,
|
||||||
|
path.to_path_buf(),
|
||||||
|
)]))
|
||||||
} else if metadata.is_dir() {
|
} else if metadata.is_dir() {
|
||||||
debug!("{} is a directory, enumerating episodes", path.display());
|
debug!("{} is a directory, enumerating episodes", path.display());
|
||||||
let files: Vec<PathBuf> = fs::read_dir(&path)?
|
let files: Vec<PathBuf> = fs::read_dir(path)?
|
||||||
.map(|entry| {
|
.map(|entry| {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
if !entry.file_type()?.is_file() {
|
if !entry.file_type()?.is_file() {
|
||||||
|
@ -67,8 +45,8 @@ fn load_path(path: PathBuf) -> std::io::Result<Episodes> {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
if entry.file_name().into_string().is_err() {
|
if entry.file_name().into_string().is_err() {
|
||||||
debug!(
|
error!(
|
||||||
"Skipping {}, contains invalid unicode",
|
"Path {} contains invalid unicode, skipping",
|
||||||
entry.path().display()
|
entry.path().display()
|
||||||
);
|
);
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
|
@ -87,4 +65,5 @@ fn load_path(path: PathBuf) -> std::io::Result<Episodes> {
|
||||||
format!("Invalid file type for {}", path.display()),
|
format!("Invalid file type for {}", path.display()),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue