diff --git a/examples/Cargo.toml b/examples/Cargo.toml index f26eb8e..4eeb8d2 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -28,3 +28,7 @@ path = "video.rs" [[example]] name = "search_suggestions" path = "search_suggestions.rs" + +[[example]] +name = "comments" +path = "comments.rs" diff --git a/examples/comments.rs b/examples/comments.rs new file mode 100644 index 0000000..143ce07 --- /dev/null +++ b/examples/comments.rs @@ -0,0 +1,21 @@ +use piped::piped::PipedClient; +use reqwest::ClientBuilder; + +#[tokio::main] +async fn main() { + let httpclient = ClientBuilder::new() + .user_agent("Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0") + .build() + .unwrap(); + + let instance = "https://pipedapi.kavin.rocks".to_string(); + + let client = PipedClient::new(httpclient, instance); + + let comments = client + .get_comments_from_id("__hYx6ZzFbQ".to_string()) + .await + .unwrap(); + + println!("{:?}", comments); +} diff --git a/piped/src/lib.rs b/piped/src/lib.rs index b2623ef..c8434fc 100644 --- a/piped/src/lib.rs +++ b/piped/src/lib.rs @@ -131,6 +131,39 @@ pub mod piped { Ok(suggestions) } + + pub async fn get_comments_from_id( + &self, + id: String, + ) -> Result> { + let resp = &self + .httpclient + .get(format!("{}/comments/{}", &self.instance, id)) + .send() + .await? + .text() + .await?; + + let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; + + Ok(comments) + } + + pub async fn get_comments_continuation( + &self, + id: String, + nexturl: String, + ) -> Result> { + let mut url = + Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id).as_str())?; + url.query_pairs_mut().append_pair("url", nexturl.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; + + Ok(comments) + } } #[derive(Deserialize, Debug)] @@ -232,4 +265,26 @@ pub mod piped { pub code: String, pub auto_generated: bool, } + + #[derive(Debug, Deserialize)] + #[serde(rename_all = "camelCase")] + pub struct CommentsInfo { + pub comments: Vec, + pub nextpage: Option, + } + + #[derive(Debug, Deserialize)] + #[serde(rename_all = "camelCase")] + pub struct Comment { + pub author: String, + pub thumbnail: String, + pub comment_id: String, + pub comment_text: String, + pub commented_time: String, + pub commentor_url: String, + pub like_count: i64, + pub hearted: bool, + pub pinned: bool, + pub verified: bool, + } }