about summary refs log tree commit diff stats
path: root/src/download
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 13:11:09 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-23 13:14:13 +0200
commit94c656ad40a7aae570e5a5fb61ad32632acc6d46 (patch)
tree269614af20caf10d76643c302e0115bd36fd2378 /src/download
parentrefactor(yt_dlp): Also move the `crates` subdirectory (diff)
downloadyt-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')
-rw-r--r--src/download/download_options.rs9
-rw-r--r--src/download/mod.rs15
2 files changed, 13 insertions, 11 deletions
diff --git a/src/download/download_options.rs b/src/download/download_options.rs
index 04c1600..e93170a 100644
--- a/src/download/download_options.rs
+++ b/src/download/download_options.rs
@@ -10,7 +10,7 @@
 
 use serde_json::{json, Value};
 
-use crate::{constants, storage::video_database::YtDlpOptions};
+use crate::{app::App, storage::video_database::YtDlpOptions};
 
 // {
 //     "ratelimit": conf.ratelimit if conf.ratelimit > 0 else None,
@@ -22,7 +22,10 @@ use crate::{constants, storage::video_database::YtDlpOptions};
 //     "logger": _ytdl_logger
 // }
 
-pub fn download_opts(additional_opts: YtDlpOptions) -> serde_json::Map<String, serde_json::Value> {
+pub fn download_opts(
+    app: &App,
+    additional_opts: YtDlpOptions,
+) -> serde_json::Map<String, serde_json::Value> {
     match json!({
       "extract_flat": false,
       "extractor_args": {
@@ -50,7 +53,7 @@ pub fn download_opts(additional_opts: YtDlpOptions) -> serde_json::Map<String, s
       "writeautomaticsub": true,
 
       "outtmpl": {
-        "default": constants::download_dir(false).expect("We're not creating this dir, thus this function can't error").join("%(channel)s/%(title)s.%(ext)s"),
+        "default": app.config.paths.download_dir.join("%(channel)s/%(title)s.%(ext)s"),
         "chapter": "%(title)s - %(section_number)03d %(section_title)s [%(id)s].%(ext)s"
       },
       "compat_opts": {},
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))?;