use media_analyzer::{MediaAnalyzer, MediaAnalyzerError};
use rand::prelude::IndexedRandom;
use rand::rng;
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};
fn is_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.is_some_and(|s| s.starts_with('.'))
}
pub fn list_files_walkdir_filtered(
dir: &Path,
include_hidden: bool,
) -> Result<Vec<PathBuf>, walkdir::Error> {
WalkDir::new(dir)
.into_iter()
.filter_entry(|e| if include_hidden { true } else { !is_hidden(e) })
.filter_map(|entry_result| match entry_result {
Ok(entry) => {
if entry.file_type().is_file() {
Some(Ok(entry.path().to_path_buf()))
} else {
None
}
}
Err(e) => Some(Err(e)),
})
.collect()
}
#[tokio::main]
async fn main() -> Result<(), MediaAnalyzerError> {
let analyzer = MediaAnalyzer::builder().build().await?;
let start_dir = Path::new("E:/Backup/Photos/photos/photos");
let all_files = list_files_walkdir_filtered(start_dir, false).unwrap();
let sample_size = 1;
let mut rng_machine = rng();
let sampled_files_iter = all_files.sample(&mut rng_machine, sample_size.min(all_files.len()));
for file in sampled_files_iter {
let path = &file.canonicalize()?;
opener::open(path).expect("can't open photo");
println!("\t{}", path.display());
let analyze_result = analyzer.analyze_media(path).await?;
println!("{}", serde_json::to_string_pretty(&analyze_result).unwrap());
}
Ok(())
}