Enumerate show episodes lazily and don't cache the results
This commit is contained in:
		
							parent
							
								
									3113a50169
								
							
						
					
					
						commit
						4081056224
					
				
					 2 changed files with 51 additions and 65 deletions
				
			
		|  | @ -27,7 +27,14 @@ async fn main() { | |||
| 
 | ||||
|     loop { | ||||
|         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!( | ||||
|             "{}{}", | ||||
|  |  | |||
|  | @ -6,7 +6,7 @@ use serde::Deserialize; | |||
| mod enumeration; | ||||
| 
 | ||||
| #[derive(Deserialize)] | ||||
| pub struct ShowSpec { | ||||
| pub struct Show { | ||||
|     pub path: PathBuf, | ||||
|     pub tags: Vec<String>, | ||||
| } | ||||
|  | @ -20,46 +20,24 @@ pub enum EpisodeNumber { | |||
| 
 | ||||
| type Episodes = HashMap<EpisodeNumber, PathBuf>; | ||||
| 
 | ||||
| pub struct Show { | ||||
|     pub episodes: Episodes, | ||||
|     pub tags: Vec<String>, | ||||
| } | ||||
| 
 | ||||
| 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")) | ||||
|             .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() | ||||
|         .expect("Failed to parse YAML from shows file") | ||||
| } | ||||
| 
 | ||||
| fn load_path(path: PathBuf) -> std::io::Result<Episodes> { | ||||
|     let metadata = fs::metadata(&path)?; | ||||
| impl Show { | ||||
|     pub fn episodes(&self) -> std::io::Result<Episodes> { | ||||
|         let path = &self.path; | ||||
|         let metadata = fs::metadata(path)?; | ||||
|         if metadata.is_file() { | ||||
|             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() { | ||||
|             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| { | ||||
|                     let entry = entry?; | ||||
|                     if !entry.file_type()?.is_file() { | ||||
|  | @ -67,8 +45,8 @@ fn load_path(path: PathBuf) -> std::io::Result<Episodes> { | |||
|                         return Ok(None); | ||||
|                     } | ||||
|                     if entry.file_name().into_string().is_err() { | ||||
|                     debug!( | ||||
|                         "Skipping {}, contains invalid unicode", | ||||
|                         error!( | ||||
|                             "Path {} contains invalid unicode, skipping", | ||||
|                             entry.path().display() | ||||
|                         ); | ||||
|                         return Ok(None); | ||||
|  | @ -88,3 +66,4 @@ fn load_path(path: PathBuf) -> std::io::Result<Episodes> { | |||
|             )) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue