about summary refs log tree commit diff stats
path: root/pkgs/by-name/yt/yt/src/cache/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkgs/by-name/yt/yt/src/cache/mod.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/pkgs/by-name/yt/yt/src/cache/mod.rs b/pkgs/by-name/yt/yt/src/cache/mod.rs
new file mode 100644
index 00000000..b60cda89
--- /dev/null
+++ b/pkgs/by-name/yt/yt/src/cache/mod.rs
@@ -0,0 +1,63 @@
+use std::path::Path;
+
+use anyhow::Result;
+use log::info;
+
+use crate::{
+    app::App,
+    storage::video_database::{
+        downloader::set_video_cache_path, getters::get_videos, setters::set_video_status, Video,
+        VideoStatus,
+    },
+};
+
+async fn invalidate_video(app: &App, video: &Video) -> Result<()> {
+    info!("Invalidating cache of video: '{}'", video.title);
+
+    set_video_status(app, &video.extractor_hash, VideoStatus::Watch, None).await?;
+    set_video_cache_path(app, &video, None).await?;
+
+    Ok(())
+}
+
+pub async fn invalidate(app: &App) -> Result<()> {
+    let all_cached_things = get_videos(app, &[VideoStatus::Cached]).await?;
+
+    info!("Got videos to invalidate: '{}'", all_cached_things.len());
+
+    for video in all_cached_things {
+        invalidate_video(app, &video).await?
+    }
+
+    Ok(())
+}
+
+pub async fn maintain(app: &App, all: bool) -> Result<()> {
+    let domain = if all {
+        vec![
+            VideoStatus::Pick,
+            //
+            VideoStatus::Watch,
+            VideoStatus::Cached,
+            VideoStatus::Watched,
+            //
+            VideoStatus::Drop,
+            VideoStatus::Dropped,
+        ]
+    } else {
+        vec![VideoStatus::Watch, VideoStatus::Cached]
+    };
+
+    let cached_videos = get_videos(app, domain.as_slice()).await?;
+
+    for vid in cached_videos {
+        let path: &Path = vid.cache_path.as_ref().expect("This is some");
+
+        info!("Checking if path ('{}') exists", path.display());
+        if !path.exists() {
+            invalidate_video(app, &vid).await?;
+        }
+    }
+
+    Ok(())
+}