chore: cargo fmt

This commit is contained in:
Ashhhleyyy 2024-01-20 13:56:00 +00:00
parent 9f1e46dbee
commit a683398ea2
Signed by: ash
GPG key ID: 83B789081A0878FB
2 changed files with 57 additions and 18 deletions

View file

@ -172,7 +172,10 @@ pub struct SubtitleTrack {
impl SubtitleTrack { impl SubtitleTrack {
pub fn from_nfo(subtitle: Subtitle, video_url: &str) -> Self { pub fn from_nfo(subtitle: Subtitle, video_url: &str) -> Self {
Self { Self {
url: video_url.replace(".mp4", &format!(".{}.{}", subtitle.language, subtitle.ext().unwrap())), url: video_url.replace(
".mp4",
&format!(".{}.{}", subtitle.language, subtitle.ext().unwrap()),
),
label: Self::label_from_lang(&subtitle.language).to_string(), label: Self::label_from_lang(&subtitle.language).to_string(),
lang: subtitle.language, lang: subtitle.language,
} }
@ -184,6 +187,7 @@ impl SubtitleTrack {
"eng" => "English", "eng" => "English",
"spa" => "Spanish", "spa" => "Spanish",
lang => lang, lang => lang,
}.to_string() }
.to_string()
} }
} }

View file

@ -1,18 +1,28 @@
use std::{path::{Path, PathBuf}, fs::File, collections::HashMap}; use std::{
collections::HashMap,
fs::File,
path::{Path, PathBuf},
};
use color_eyre::{Result, eyre::eyre}; use color_eyre::{eyre::eyre, Result};
use crate::{nfo::{ContentType, ItemMeta, ItemData, SubtitleTrack}, Args, templates, util::get_url, PlayerKind}; use crate::{
nfo::{ContentType, ItemData, ItemMeta, SubtitleTrack},
templates,
util::get_url,
Args, PlayerKind,
};
pub fn walk_metadata(args: &Args) -> Result<()> { pub fn walk_metadata(args: &Args) -> Result<()> {
let mut path_cache = HashMap::<PathBuf, ItemMeta>::new(); let mut path_cache = HashMap::<PathBuf, ItemMeta>::new();
let walk = walkdir::WalkDir::new(&args.dir) let walk = walkdir::WalkDir::new(&args.dir).contents_first(true);
.contents_first(true);
for entry in walk { for entry in walk {
let entry = entry?; let entry = entry?;
if !entry.file_type().is_dir() { continue; } if !entry.file_type().is_dir() {
continue;
}
let path = entry.path(); let path = entry.path();
let read = std::fs::read_dir(path)?; let read = std::fs::read_dir(path)?;
let mut entries = Vec::with_capacity(read.size_hint().0); let mut entries = Vec::with_capacity(read.size_hint().0);
@ -31,19 +41,28 @@ pub fn walk_metadata(args: &Args) -> Result<()> {
let children = find_tv_show_children(&path_cache, &entries); let children = find_tv_show_children(&path_cache, &entries);
let rendered = templates::tv_show(&info, args, &children)?; let rendered = templates::tv_show(&info, args, &children)?;
std::fs::write(output_path, rendered.0)?; std::fs::write(output_path, rendered.0)?;
}, }
ContentType::Season => { ContentType::Season => {
let mut children = find_season_children(&path_cache, &entries, &info.data, args)?; let mut children =
find_season_children(&path_cache, &entries, &info.data, args)?;
for child in &mut children { for child in &mut children {
let aspect = child.data.file_info.stream_details.video.aspect_ratio.replace(':', "/"); let aspect = child
.data
.file_info
.stream_details
.video
.aspect_ratio
.replace(':', "/");
let rendered = match args.player { let rendered = match args.player {
PlayerKind::Vidstack => templates::vidstack_player( PlayerKind::Vidstack => templates::vidstack_player(
&child.data.title, &child.data.title,
&child.url, &child.url,
&aspect, &aspect,
child.data.art.poster.as_ref().unwrap(), child.data.art.poster.as_ref().unwrap(),
&if let Some(subtitle) = &child.data.file_info.stream_details.subtitle { &if let Some(subtitle) =
&child.data.file_info.stream_details.subtitle
{
vec![SubtitleTrack::from_nfo(subtitle.clone(), &child.url)] vec![SubtitleTrack::from_nfo(subtitle.clone(), &child.url)]
} else { } else {
vec![] vec![]
@ -55,7 +74,9 @@ pub fn walk_metadata(args: &Args) -> Result<()> {
&child.url, &child.url,
&aspect, &aspect,
child.data.art.poster.as_ref().unwrap(), child.data.art.poster.as_ref().unwrap(),
&if let Some(subtitle) = &child.data.file_info.stream_details.subtitle { &if let Some(subtitle) =
&child.data.file_info.stream_details.subtitle
{
vec![SubtitleTrack::from_nfo(subtitle.clone(), &child.url)] vec![SubtitleTrack::from_nfo(subtitle.clone(), &child.url)]
} else { } else {
vec![] vec![]
@ -70,7 +91,7 @@ pub fn walk_metadata(args: &Args) -> Result<()> {
let rendered = templates::tv_show(&info, args, &children)?; let rendered = templates::tv_show(&info, args, &children)?;
std::fs::write(output_path, rendered.0)?; std::fs::write(output_path, rendered.0)?;
}, }
ContentType::Episode => unreachable!(), ContentType::Episode => unreachable!(),
} }
println!("{path:?}: {:?}", info.ty); println!("{path:?}: {:?}", info.ty);
@ -83,7 +104,10 @@ pub fn walk_metadata(args: &Args) -> Result<()> {
Ok(()) Ok(())
} }
fn find_tv_show_children(path_cache: &HashMap<PathBuf, ItemMeta>, entries: &[PathBuf]) -> Vec<ItemMeta> { fn find_tv_show_children(
path_cache: &HashMap<PathBuf, ItemMeta>,
entries: &[PathBuf],
) -> Vec<ItemMeta> {
let mut children = vec![]; let mut children = vec![];
for entry in entries { for entry in entries {
if let Some(info) = path_cache.get(entry) { if let Some(info) = path_cache.get(entry) {
@ -96,7 +120,12 @@ fn find_tv_show_children(path_cache: &HashMap<PathBuf, ItemMeta>, entries: &[Pat
children children
} }
fn find_season_children(_path_cache: &HashMap<PathBuf, ItemMeta>, entries: &[PathBuf], parent: &ItemData, args: &Args) -> Result<Vec<ItemMeta>> { fn find_season_children(
_path_cache: &HashMap<PathBuf, ItemMeta>,
entries: &[PathBuf],
parent: &ItemData,
args: &Args,
) -> Result<Vec<ItemMeta>> {
let mut children = vec![]; let mut children = vec![];
for entry in entries { for entry in entries {
if let (Some(stem), Some(ext)) = (entry.file_stem(), entry.extension()) { if let (Some(stem), Some(ext)) = (entry.file_stem(), entry.extension()) {
@ -104,7 +133,12 @@ fn find_season_children(_path_cache: &HashMap<PathBuf, ItemMeta>, entries: &[Pat
if stem.contains("Episode") && ext == "nfo" { if stem.contains("Episode") && ext == "nfo" {
let data = read_data(entry)?; let data = read_data(entry)?;
let video = entry.with_extension("mp4"); let video = entry.with_extension("mp4");
let url = get_url(&video.to_string_lossy().replace("/stuff/ash/Series", "/media/Shows"), args)?; let url = get_url(
&video
.to_string_lossy()
.replace("/stuff/ash/Series", "/media/Shows"),
args,
)?;
children.push(ItemMeta { children.push(ItemMeta {
url, url,
ty: ContentType::Episode, ty: ContentType::Episode,
@ -121,7 +155,8 @@ fn find_season_children(_path_cache: &HashMap<PathBuf, ItemMeta>, entries: &[Pat
fn read_data(path: &Path) -> Result<ItemData> { fn read_data(path: &Path) -> Result<ItemData> {
let mut f = File::open(path)?; let mut f = File::open(path)?;
let data: ItemData = yaserde::de::from_reader(&mut f).map_err(|e| eyre!("failed to parse directory meta: {e}"))?; let data: ItemData = yaserde::de::from_reader(&mut f)
.map_err(|e| eyre!("failed to parse directory meta: {e}"))?;
Ok(data) Ok(data)
} }