about summary refs log tree commit diff stats
path: root/yt/src/comments
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--crates/yt/src/comments/description.rs (renamed from yt/src/comments/description.rs)8
-rw-r--r--crates/yt/src/comments/display.rs (renamed from yt/src/comments/display.rs)0
-rw-r--r--crates/yt/src/comments/mod.rs (renamed from yt/src/comments/mod.rs)26
-rw-r--r--crates/yt/src/comments/output.rs (renamed from yt/src/comments/output.rs)0
-rw-r--r--yt/src/comments/comment.rs65
5 files changed, 19 insertions, 80 deletions
diff --git a/yt/src/comments/description.rs b/crates/yt/src/comments/description.rs
index d22a40f..e8cb29d 100644
--- a/yt/src/comments/description.rs
+++ b/crates/yt/src/comments/description.rs
@@ -17,7 +17,7 @@ use crate::{
 };
 
 use anyhow::{Result, bail};
-use yt_dlp::wrapper::info_json::InfoJson;
+use yt_dlp::{InfoJson, json_cast};
 
 pub async fn description(app: &App) -> Result<()> {
     let description = get(app).await?;
@@ -39,6 +39,8 @@ pub async fn get(app: &App) -> Result<String> {
     );
 
     Ok(info_json
-        .description
-        .unwrap_or("<No description>".to_owned()))
+        .get("description")
+        .map(|val| json_cast!(val, as_str))
+        .unwrap_or("<No description>")
+        .to_owned())
 }
diff --git a/yt/src/comments/display.rs b/crates/yt/src/comments/display.rs
index 6166b2b..6166b2b 100644
--- a/yt/src/comments/display.rs
+++ b/crates/yt/src/comments/display.rs
diff --git a/yt/src/comments/mod.rs b/crates/yt/src/comments/mod.rs
index daecf8d..876146d 100644
--- a/yt/src/comments/mod.rs
+++ b/crates/yt/src/comments/mod.rs
@@ -11,11 +11,11 @@
 
 use std::mem;
 
-use anyhow::{Context, Result, bail};
-use comment::{CommentExt, Comments};
+use anyhow::{Result, bail};
+use comment::{Comment, CommentExt, Comments, Parent};
 use output::display_fmt_and_less;
 use regex::Regex;
-use yt_dlp::wrapper::info_json::{Comment, InfoJson, Parent};
+use yt_dlp::{InfoJson, json_cast};
 
 use crate::{
     app::App,
@@ -39,23 +39,25 @@ pub async fn get(app: &App) -> Result<Comments> {
             bail!("Could not find a currently playing video!");
         };
 
-    let mut info_json: InfoJson = get::video_info_json(&currently_playing_video)?.unreachable(
-        "A currently *playing* must be cached. And thus the info.json should be available",
+    let info_json: InfoJson = get::video_info_json(&currently_playing_video)?.unreachable(
+        "A currently *playing* video must be cached. And thus the info.json should be available",
     );
 
-    let base_comments = mem::take(&mut info_json.comments).with_context(|| {
-        format!(
+    let base_comments = if let Some(comments) = info_json.get("comments") {
+        json_cast!(comments, as_array)
+    } else {
+        bail!(
             "The video ('{}') does not have comments!",
             info_json
-                .title
-                .as_ref()
-                .unwrap_or(&("<No Title>".to_owned()))
+                .get("title")
+                .map(|val| json_cast!(val, as_str))
+                .unwrap_or("<No Title>")
         )
-    })?;
-    drop(info_json);
+    };
 
     let mut comments = Comments::new();
     for c in base_comments {
+        let c: Comment = serde_json::from_value(c.to_owned())?;
         if let Parent::Id(id) = &c.parent {
             comments.insert(&(id.clone()), CommentExt::from(c));
         } else {
diff --git a/yt/src/comments/output.rs b/crates/yt/src/comments/output.rs
index cb3a9c4..cb3a9c4 100644
--- a/yt/src/comments/output.rs
+++ b/crates/yt/src/comments/output.rs
diff --git a/yt/src/comments/comment.rs b/yt/src/comments/comment.rs
deleted file mode 100644
index 6b8cf73..0000000
--- a/yt/src/comments/comment.rs
+++ /dev/null
@@ -1,65 +0,0 @@
-// yt - A fully featured command line YouTube client
-//
-// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
-// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
-// SPDX-License-Identifier: GPL-3.0-or-later
-//
-// This file is part of Yt.
-//
-// 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 yt_dlp::wrapper::info_json::Comment;
-
-#[derive(Debug, Clone)]
-#[allow(clippy::module_name_repetitions)]
-pub struct CommentExt {
-    pub value: Comment,
-    pub replies: Vec<CommentExt>,
-}
-
-#[derive(Debug, Default)]
-pub struct Comments {
-    pub(super) vec: Vec<CommentExt>,
-}
-
-impl Comments {
-    pub fn new() -> Self {
-        Self::default()
-    }
-    pub fn push(&mut self, value: CommentExt) {
-        self.vec.push(value);
-    }
-    pub fn get_mut(&mut self, key: &str) -> Option<&mut CommentExt> {
-        self.vec.iter_mut().filter(|c| c.value.id.id == key).last()
-    }
-    pub fn insert(&mut self, key: &str, value: CommentExt) {
-        let parent = self
-            .vec
-            .iter_mut()
-            .filter(|c| c.value.id.id == key)
-            .last()
-            .expect("One of these should exist");
-        parent.push_reply(value);
-    }
-}
-impl CommentExt {
-    pub fn push_reply(&mut self, value: CommentExt) {
-        self.replies.push(value);
-    }
-    pub fn get_mut_reply(&mut self, key: &str) -> Option<&mut CommentExt> {
-        self.replies
-            .iter_mut()
-            .filter(|c| c.value.id.id == key)
-            .last()
-    }
-}
-
-impl From<Comment> for CommentExt {
-    fn from(value: Comment) -> Self {
-        Self {
-            replies: vec![],
-            value,
-        }
-    }
-}