about summary refs log tree commit diff stats
path: root/yt/src/watch/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'yt/src/watch/mod.rs')
-rw-r--r--yt/src/watch/mod.rs36
1 files changed, 32 insertions, 4 deletions
diff --git a/yt/src/watch/mod.rs b/yt/src/watch/mod.rs
index 5c76c12..6e7c372 100644
--- a/yt/src/watch/mod.rs
+++ b/yt/src/watch/mod.rs
@@ -8,12 +8,13 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-use std::collections::HashMap;
+use std::{collections::HashMap, time::Duration};
 
 use anyhow::{Context, Result};
-use events::MpvEventHandler;
+use events::{IdleCheckOutput, MpvEventHandler};
 use libmpv2::{events::EventContext, Mpv};
 use log::{debug, info, warn};
+use tokio::time;
 
 use crate::{
     app::App,
@@ -24,6 +25,7 @@ use crate::{
 
 pub mod events;
 
+#[allow(clippy::too_many_lines)]
 pub async fn watch(app: &App) -> Result<()> {
     maintain(app, false).await?;
 
@@ -107,8 +109,34 @@ pub async fn watch(app: &App) -> Result<()> {
     }
 
     let mut mpv_event_handler = MpvEventHandler::from_playlist(playlist_cache);
-    loop {
-        while mpv_event_handler.check_idle(app, &mpv).await? {}
+    let mut have_warned = (false, 0);
+    'watchloop: loop {
+        'waitloop: while let Ok(value) = mpv_event_handler.check_idle(app, &mpv).await {
+            match value {
+                IdleCheckOutput::NoMoreAvailable => {
+                    break 'watchloop;
+                }
+                IdleCheckOutput::NoCached { marked_watched } => {
+                    // try again next time.
+                    if have_warned.0 {
+                        if have_warned.1 != marked_watched {
+                            warn!("Now {} videos are marked as watched.", marked_watched);
+                            have_warned.1 = marked_watched;
+                        }
+                    } else {
+                        warn!("There is nothing to watch yet, but still {} videos marked as to be watched. \
+                        Will idle, until they become available", marked_watched);
+                        have_warned = (true, marked_watched);
+                    }
+                    time::sleep(Duration::from_secs(10)).await;
+                }
+                IdleCheckOutput::Available { newly_available: _ } => {
+                    have_warned.0 = false;
+                    // Something just became available!
+                    break 'waitloop;
+                }
+            }
+        }
 
         if let Some(ev) = ev_ctx.wait_event(600.) {
             match ev {