about summary refs log tree commit diff stats
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/yt/src/commands/select/implm/standalone/add.rs29
1 files changed, 20 insertions, 9 deletions
diff --git a/crates/yt/src/commands/select/implm/standalone/add.rs b/crates/yt/src/commands/select/implm/standalone/add.rs
index dd11cb4..2a7db53 100644
--- a/crates/yt/src/commands/select/implm/standalone/add.rs
+++ b/crates/yt/src/commands/select/implm/standalone/add.rs
@@ -15,7 +15,7 @@ use crate::{
 };
 
 use anyhow::{Context, Result, bail};
-use log::{error, warn};
+use log::{debug, error, warn};
 use url::Url;
 use yt_dlp::{YoutubeDL, info_json::InfoJson, json_cast, json_get, json_try_get};
 
@@ -28,11 +28,17 @@ pub(crate) async fn add(
 ) -> Result<()> {
     for url in urls {
         async fn process_and_add(app: &App, entry: InfoJson, yt_dlp: &YoutubeDL) -> Result<()> {
-            let url = json_get!(entry, "url", as_str).parse()?;
-
-            let entry = yt_dlp
-                .extract_info(&url, false, true)
-                .with_context(|| format!("Failed to fetch entry for url: '{url}'"))?;
+            let entry = if json_try_get!(entry, "formats", as_array).is_some() {
+                // We assume, that this entry is already processed.
+                debug!("Refusing to re-process entry again");
+                entry
+            } else {
+                let url = json_get!(entry, "url", as_str).parse()?;
+
+                yt_dlp
+                    .extract_info(&url, false, true)
+                    .with_context(|| format!("Failed to fetch entry for url: '{url}'"))?
+            };
 
             add_entry(app, entry).await?;
 
@@ -72,7 +78,9 @@ pub(crate) async fn add(
             Ok(())
         }
 
-        let yt_dlp = yt_dlp_opts_updating(start.unwrap_or(0) + stop.unwrap_or(0))?;
+        let yt_dlp = yt_dlp_opts_updating(
+            start.unwrap_or(0) + stop.map_or(usize::MAX, |val| val.saturating_add(1)),
+        )?;
 
         let entry = yt_dlp
             .extract_info(&url, false, true)
@@ -92,6 +100,10 @@ pub(crate) async fn add(
                     let start = start.unwrap_or(0);
                     let stop = stop.unwrap_or(entries.len() - 1);
 
+                    if entries.is_empty() {
+                        bail!("Failed to add playlist, as it is empty (contains no entries).")
+                    }
+
                     let respected_entries =
                         take_vector(entries, start, stop).with_context(|| {
                             format!(
@@ -131,8 +143,7 @@ pub(crate) async fn add(
                 }
             }
             other => bail!(
-                "Your URL should point to a video or a playlist, but points to a '{:#?}'",
-                other
+                "Your URL should point to a video or a playlist, but points to a '{other:#?}'"
             ),
         }
     }