diff options
Diffstat (limited to 'import/src')
-rw-r--r-- | import/src/main.rs | 149 |
1 files changed, 74 insertions, 75 deletions
diff --git a/import/src/main.rs b/import/src/main.rs index 6cf2ef3..f139d40 100644 --- a/import/src/main.rs +++ b/import/src/main.rs @@ -12,7 +12,6 @@ use jellymatroska::read::EbmlReader; use jellyremuxer::import::{import_metadata, seek_index::import_seek_index}; use log::info; use std::{ - collections::BTreeMap, fs::{remove_file, File}, io::stdin, path::PathBuf, @@ -81,12 +80,11 @@ fn main() -> anyhow::Result<()> { r#move, } => { let tmdb_kind = if series { "tv" } else { "movie" }; - let tmdb_key = std::env::var("TMDB_API_KEY").context("tmdb api key required")?; let tmdb_id = if let Some(id) = tmdb_id { Some(id.parse().unwrap()) - } else { - let title = tmdb_search.as_ref().unwrap(); - let results = crate::tmdb_search(tmdb_kind, title, &tmdb_key)?; + } else if let Some(title) = tmdb_search { + let tmdb_key = std::env::var("TMDB_API_KEY").context("tmdb api key required")?; + let results = crate::tmdb_search(tmdb_kind, &title, &tmdb_key)?; info!("results:"); for (i, r) in results.results.iter().enumerate() { info!( @@ -108,25 +106,55 @@ fn main() -> anyhow::Result<()> { 0 }; Some(results.results[res_index].id) + } else { + None }; - let tmdb_details = tmdb_id.map(|id| { - let td = tmdb_details(tmdb_kind, id, &tmdb_key) - .context("fetching details") - .unwrap(); - if td.title.is_some() { - info!("is this correct? [y/n]"); - if stdin().lines().next().unwrap().unwrap() != "y" { - exit(0) + let tmdb_details = tmdb_id + .map(|id| { + let tmdb_key = + std::env::var("TMDB_API_KEY").context("tmdb api key required")?; + let td = tmdb_details(tmdb_kind, id, &tmdb_key) + .context("fetching details") + .unwrap(); + if td.title.is_some() { + info!("is this correct? [y/n]"); + if stdin().lines().next().unwrap().unwrap() != "y" { + exit(0) + } } - } - td - }); + Ok::<_, anyhow::Error>(td) + }) + .transpose()?; + + let mut kind = NodeKind::Series; + let (mut file_meta, mut seek_index, mut source_path_e) = Default::default(); + + if let Some(input_path) = &input { + source_path_e = Some(path.join(format!("source.mkv"))); + + file_meta = Some({ + let input = File::open(&input_path).unwrap(); + let mut input = EbmlReader::new(input); + import_metadata(&mut input)? + }); + seek_index = { + let input = File::open(&input_path).unwrap(); + let mut input = EbmlReader::new(input); + import_seek_index(&mut input)? + }; - let title = tmdb_details + kind = NodeKind::Movie; + } + + let title = file_meta .as_ref() - .map(|d| d.title.clone().or(d.name.clone())) + .map(|m| m.title.clone()) .flatten() + .or(tmdb_details + .as_ref() + .map(|d| d.title.clone().or(d.name.clone())) + .flatten()) .unwrap(); let ident = make_ident(&title); let path = path.join(&ident); @@ -163,54 +191,14 @@ fn main() -> anyhow::Result<()> { .transpose()? .flatten(); - let kind; - let media; - let source; - let mut seek_index = BTreeMap::new(); - let mut source_path_e = None; - - if let Some(input) = input { - let source_path = path.join(format!("source.mkv")); - if r#move { - std::fs::rename(&input, &source_path)?; - } else if copy { - std::fs::copy(&input, &source_path)?; - } else { - if source_path.is_symlink() { - remove_file(&source_path)?; - } - std::os::unix::fs::symlink(&input, &source_path)?; - } - - let (tracks, local_tracks, duration) = { - let input = File::open(&source_path).unwrap(); - let mut input = EbmlReader::new(input); - import_metadata(&source_path.to_path_buf(), &mut input)? - }; - seek_index = { - let input = File::open(&source_path).unwrap(); - let mut input = EbmlReader::new(input); - import_seek_index(&mut input)? - }; - - kind = NodeKind::Movie; - media = Some(MediaInfo { duration, tracks }); - source = Some(MediaSource::Local { - tracks: local_tracks, - }); - source_path_e = Some(source_path) - } else { - kind = NodeKind::Series; - media = None; - source = None; - }; - let node = Node { private: NodePrivate { import: None, backdrop: backdrop.clone().map(AssetLocation::Library), poster: poster.clone().map(AssetLocation::Library), - source, + source: file_meta.as_ref().map(|m| MediaSource::Local { + tracks: m.track_sources.clone(), + }), }, public: NodePublic { parent: None, @@ -224,26 +212,37 @@ fn main() -> anyhow::Result<()> { index: None, kind, children: Vec::new(), - media, + media: file_meta.as_ref().map(|m| MediaInfo { + duration: m.duration, + tracks: m.tracks.clone(), + }), }, }; if args.dry { - println!("{node:?}") + println!("{node:#?}") } else { std::fs::create_dir_all(&path)?; - for (tn, index) in seek_index { - info!("writing index {tn} with {} blocks", index.blocks.len()); - bincode::encode_into_std_write( - index, - &mut File::create( - source_path_e - .as_ref() - .unwrap() - .with_extension(&format!("si.{tn}")), - )?, - bincode::config::standard(), - )?; + if let Some(source_path) = source_path_e { + let input = input.clone().unwrap(); + if r#move { + std::fs::rename(&input, &source_path)?; + } else if copy { + std::fs::copy(&input, &source_path)?; + } else { + if source_path.is_symlink() { + remove_file(&source_path)?; + } + std::os::unix::fs::symlink(&input, &source_path)?; + } + for (tn, index) in seek_index { + info!("writing index {tn} with {} blocks", index.blocks.len()); + bincode::encode_into_std_write( + index, + &mut File::create(source_path.with_extension(&format!("si.{tn}")))?, + bincode::config::standard(), + )?; + } } let f = File::create(path.join(if series { "directory.json".to_string() |