diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-08-23 13:11:09 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-08-23 13:14:13 +0200 |
commit | 94c656ad40a7aae570e5a5fb61ad32632acc6d46 (patch) | |
tree | 269614af20caf10d76643c302e0115bd36fd2378 /src/download/mod.rs | |
parent | refactor(yt_dlp): Also move the `crates` subdirectory (diff) | |
download | yt-94c656ad40a7aae570e5a5fb61ad32632acc6d46.tar.gz yt-94c656ad40a7aae570e5a5fb61ad32632acc6d46.zip |
feat(treewide): Use a configuration file
This allows use to avoid duplication of default values in the codebase and obviously also facilitates changing these without having to re-compile.
Diffstat (limited to 'src/download/mod.rs')
-rw-r--r-- | src/download/mod.rs | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/src/download/mod.rs b/src/download/mod.rs index c3d79b7..707f281 100644 --- a/src/download/mod.rs +++ b/src/download/mod.rs @@ -12,7 +12,6 @@ use std::{collections::HashMap, sync::Arc, time::Duration}; use crate::{ app::App, - constants::download_dir, download::download_options::download_opts, storage::video_database::{ downloader::{get_next_uncached_video, set_video_cache_path}, @@ -72,8 +71,8 @@ impl Downloader { /// This will run, until the database doesn't contain any watchable videos pub async fn consume(&mut self, app: Arc<App>, max_cache_size: u64) -> Result<()> { while let Some(next_video) = get_next_uncached_video(&app).await? { - if Self::get_current_cache_allocation().await? - + self.get_approx_video_size(&next_video).await? + if Self::get_current_cache_allocation(&app).await? + + self.get_approx_video_size(&app, &next_video).await? >= max_cache_size { warn!( @@ -134,7 +133,7 @@ impl Downloader { Ok(()) } - async fn get_current_cache_allocation() -> Result<u64> { + async fn get_current_cache_allocation(app: &App) -> Result<u64> { fn dir_size(mut dir: fs::ReadDir) -> BoxFuture<'static, Result<u64>> { async move { let mut acc = 0; @@ -155,14 +154,14 @@ impl Downloader { .boxed() } - let val = dir_size(fs::read_dir(download_dir(true)?).await?).await; + let val = dir_size(fs::read_dir(&app.config.paths.download_dir).await?).await; if let Ok(val) = val.as_ref() { info!("Cache dir has a size of '{}'", val); } val } - async fn get_approx_video_size(&mut self, video: &Video) -> Result<u64> { + async fn get_approx_video_size(&mut self, app: &App, video: &Video) -> Result<u64> { if let Some(value) = self.video_size_cache.get(&video.extractor_hash) { Ok(*value) } else { @@ -170,7 +169,7 @@ impl Downloader { let add_opts = YtDlpOptions { subtitle_langs: "".to_owned(), }; - let opts = &download_opts(add_opts); + let opts = &download_opts(&app, add_opts); let result = yt_dlp::extract_info(&opts, &video.url, false, true) .await @@ -201,7 +200,7 @@ impl Downloader { let addional_opts = get_video_yt_dlp_opts(&app, &video.extractor_hash).await?; - let result = yt_dlp::download(&[video.url.clone()], &download_opts(addional_opts)) + let result = yt_dlp::download(&[video.url.clone()], &download_opts(&app, addional_opts)) .await .with_context(|| format!("Failed to download video: '{}'", video.title))?; |