diff --git a/examples/channel.rs b/examples/channel.rs index 91c75eb..0919422 100644 --- a/examples/channel.rs +++ b/examples/channel.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -13,9 +13,9 @@ async fn main() { let client = PipedClient::new(httpclient, instance); let channel = client - .get_channel_from_id("UCXuqSBlHAE6Xw-yeJA0Tunw".to_string()) + .channel_from_id("UCXuqSBlHAE6Xw-yeJA0Tunw".to_string()) .await .unwrap(); - println!("{:?}", channel); + println!("{:#?}", channel); } diff --git a/examples/comments.rs b/examples/comments.rs index 143ce07..98d869f 100644 --- a/examples/comments.rs +++ b/examples/comments.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -13,9 +13,9 @@ async fn main() { let client = PipedClient::new(httpclient, instance); let comments = client - .get_comments_from_id("__hYx6ZzFbQ".to_string()) + .comments_from_id("__hYx6ZzFbQ".to_string()) .await .unwrap(); - println!("{:?}", comments); + println!("{:#?}", comments); } diff --git a/examples/playlist.rs b/examples/playlist.rs index 1eb5a29..45a2887 100644 --- a/examples/playlist.rs +++ b/examples/playlist.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -13,9 +13,9 @@ async fn main() { let client = PipedClient::new(httpclient, instance); let playlist = client - .get_playlist_from_id("PLQSoWXSpjA38FIQCvwnVNPlGPVA63WTD8".to_string()) + .playlist_from_id("PLQSoWXSpjA38FIQCvwnVNPlGPVA63WTD8".to_string()) .await .unwrap(); - println!("{:?}", playlist); + println!("{:#?}", playlist); } diff --git a/examples/search_suggestions.rs b/examples/search_suggestions.rs index c0f5458..e3f237a 100644 --- a/examples/search_suggestions.rs +++ b/examples/search_suggestions.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -13,9 +13,9 @@ async fn main() { let client = PipedClient::new(httpclient, instance); let suggestions = client - .get_search_suggestions("techlore".to_string()) + .search_suggestions("techlore".to_string()) .await .unwrap(); - println!("{:?}", suggestions); + println!("{:#?}", suggestions); } diff --git a/examples/trending.rs b/examples/trending.rs index 34f05e1..a7fb0f1 100644 --- a/examples/trending.rs +++ b/examples/trending.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -12,7 +12,7 @@ async fn main() { let client = PipedClient::new(httpclient, instance); - let streams = client.get_trending("US".to_string()).await.unwrap(); + let streams = client.trending("US".to_string()).await.unwrap(); - println!("{:?}", streams); + println!("{:#?}", streams); } diff --git a/examples/video.rs b/examples/video.rs index d71ad97..72a0e17 100644 --- a/examples/video.rs +++ b/examples/video.rs @@ -1,4 +1,4 @@ -use piped::piped::PipedClient; +use piped::PipedClient; use reqwest::ClientBuilder; #[tokio::main] @@ -13,9 +13,9 @@ async fn main() { let client = PipedClient::new(httpclient, instance); let video = client - .get_video_from_id("__hYx6ZzFbQ".to_string()) + .video_from_id("__hYx6ZzFbQ".to_string()) .await .unwrap(); - println!("{:?}", video); + println!("{:#?}", video); } diff --git a/piped/Cargo.toml b/piped/Cargo.toml index ecc9ee9..95046d4 100644 --- a/piped/Cargo.toml +++ b/piped/Cargo.toml @@ -11,6 +11,6 @@ version = "0.0.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -reqwest = "0.11.4" -serde = {version = "1.0.130", features = ["derive"]} -serde_json = "1.0.68" +reqwest = "^0.11" +serde = {version = "^1.0", features = ["derive"]} +serde_json = "^1.0" diff --git a/piped/src/client.rs b/piped/src/client.rs new file mode 100644 index 0000000..ddd096e --- /dev/null +++ b/piped/src/client.rs @@ -0,0 +1,158 @@ +use reqwest::{Client, Url}; + +use crate::{Channel, CommentsInfo, Playlist, RelatedStream, StreamsPage, VideoInfo}; + +pub struct PipedClient { + pub httpclient: Client, + pub instance: String, +} + +impl PipedClient { + pub fn new(httpclient: Client, instance: String) -> PipedClient { + PipedClient { + httpclient, + instance, + } + } + + pub async fn trending( + &self, + region: String, + ) -> Result, Box> { + let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?; + url.query_pairs_mut().append_pair("region", region.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let streams: Vec = serde_json::from_str(resp.as_str())?; + + Ok(streams) + } + + pub async fn channel_from_id(&self, id: String) -> Result> { + let resp = &self + .httpclient + .get(format!("{}/channel/{}", &self.instance, id)) + .send() + .await? + .text() + .await?; + + let channel: Channel = serde_json::from_str(resp.as_str())?; + + Ok(channel) + } + + pub async fn channel_continuation( + &self, + id: String, + nexturl: String, + nextbody: String, + ) -> Result> { + let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id).as_str())?; + url.query_pairs_mut() + .append_pair("url", nexturl.as_str()) + .append_pair("id", nextbody.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let streams: StreamsPage = serde_json::from_str(resp.as_str())?; + + Ok(streams) + } + + pub async fn playlist_from_id( + &self, + id: String, + ) -> Result> { + let resp = &self + .httpclient + .get(format!("{}/playlists/{}", &self.instance, id)) + .send() + .await? + .text() + .await?; + + let playlist: Playlist = serde_json::from_str(resp.as_str())?; + + Ok(playlist) + } + + pub async fn playlist_continuation( + &self, + id: String, + nexturl: String, + nextbody: String, + ) -> Result> { + let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id).as_str())?; + url.query_pairs_mut() + .append_pair("url", nexturl.as_str()) + .append_pair("id", nextbody.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let streams: StreamsPage = serde_json::from_str(resp.as_str())?; + + Ok(streams) + } + + pub async fn video_from_id(&self, id: String) -> Result> { + let resp = &self + .httpclient + .get(format!("{}/streams/{}", &self.instance, id)) + .send() + .await? + .text() + .await?; + + let video: VideoInfo = serde_json::from_str(resp.as_str())?; + + Ok(video) + } + + pub async fn search_suggestions( + &self, + q: String, + ) -> Result, Box> { + let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; + url.query_pairs_mut().append_pair("query", q.as_str()); + + let resp = &self.httpclient.get(url).send().await?.text().await?; + + let suggestions: Vec = serde_json::from_str(resp.as_str())?; + + Ok(suggestions) + } + + pub async fn 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 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) + } +} diff --git a/piped/src/lib.rs b/piped/src/lib.rs index 19669de..ea36297 100644 --- a/piped/src/lib.rs +++ b/piped/src/lib.rs @@ -1,294 +1,5 @@ -pub mod piped { - use reqwest::{Client, Url}; - use serde::Deserialize; +mod client; +mod structure; - pub struct PipedClient { - pub httpclient: Client, - pub instance: String, - } - - impl PipedClient { - pub fn new(httpclient: Client, instance: String) -> PipedClient { - PipedClient { - httpclient: httpclient, - instance: instance, - } - } - - pub async fn get_trending( - &self, - region: String, - ) -> Result, Box> { - let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?; - url.query_pairs_mut().append_pair("region", region.as_str()); - - let resp = &self.httpclient.get(url).send().await?.text().await?; - - let streams: Vec = serde_json::from_str(resp.as_str())?; - - Ok(streams) - } - - pub async fn get_channel_from_id( - &self, - id: String, - ) -> Result> { - let resp = &self - .httpclient - .get(format!("{}/channel/{}", &self.instance, id)) - .send() - .await? - .text() - .await?; - - let channel: Channel = serde_json::from_str(resp.as_str())?; - - Ok(channel) - } - - pub async fn get_channel_continuation( - &self, - id: String, - nexturl: String, - nextbody: String, - ) -> Result> { - let mut url = - Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id).as_str())?; - url.query_pairs_mut() - .append_pair("url", nexturl.as_str()) - .append_pair("id", nextbody.as_str()); - - let resp = &self.httpclient.get(url).send().await?.text().await?; - - let streams: StreamsPage = serde_json::from_str(resp.as_str())?; - - Ok(streams) - } - - pub async fn get_playlist_from_id( - &self, - id: String, - ) -> Result> { - let resp = &self - .httpclient - .get(format!("{}/playlists/{}", &self.instance, id)) - .send() - .await? - .text() - .await?; - - let playlist: Playlist = serde_json::from_str(resp.as_str())?; - - Ok(playlist) - } - - pub async fn get_playlist_continuation( - &self, - id: String, - nexturl: String, - nextbody: String, - ) -> Result> { - let mut url = - Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id).as_str())?; - url.query_pairs_mut() - .append_pair("url", nexturl.as_str()) - .append_pair("id", nextbody.as_str()); - - let resp = &self.httpclient.get(url).send().await?.text().await?; - - let streams: StreamsPage = serde_json::from_str(resp.as_str())?; - - Ok(streams) - } - - pub async fn get_video_from_id( - &self, - id: String, - ) -> Result> { - let resp = &self - .httpclient - .get(format!("{}/streams/{}", &self.instance, id)) - .send() - .await? - .text() - .await?; - - let video: VideoInfo = serde_json::from_str(resp.as_str())?; - - Ok(video) - } - - pub async fn get_search_suggestions( - &self, - q: String, - ) -> Result, Box> { - let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; - url.query_pairs_mut().append_pair("query", q.as_str()); - - let resp = &self.httpclient.get(url).send().await?.text().await?; - - let suggestions: Vec = serde_json::from_str(resp.as_str())?; - - 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)] - #[serde(rename_all = "camelCase")] - pub struct Channel { - pub id: String, - pub name: String, - pub avatar_url: String, - pub banner_url: String, - pub description: String, - pub nextpage: Option, - pub nextbody: Option, - pub related_streams: Vec, - } - - #[derive(Deserialize, Debug)] - #[serde(rename_all = "camelCase")] - pub struct Playlist { - pub name: String, - pub thumbnail_url: String, - pub banner_url: Option, - pub uploader: String, - pub uploader_url: String, - pub uploader_avatar: String, - pub videos: i32, - pub nextpage: Option, - pub nextbody: Option, - pub related_streams: Vec, - } - - #[derive(Deserialize, Debug)] - #[serde(rename_all = "camelCase")] - pub struct StreamsPage { - pub nextpage: Option, - pub nextbody: Option, - pub related_streams: Vec, - } - - #[derive(Deserialize, Debug)] - #[serde(rename_all = "camelCase")] - pub struct RelatedStream { - pub url: String, - pub title: String, - pub thumbnail: String, - pub uploader_avatar: Option, - pub uploader_name: String, - pub uploader_url: String, - pub uploaded_date: Option, - pub uploader_verified: bool, - pub duration: i32, - pub views: i64, - } - - #[derive(Debug, Deserialize)] - #[serde(rename_all = "camelCase")] - pub struct VideoInfo { - pub title: String, - pub description: String, - pub dash: Option, - pub upload_date: String, - pub uploader: String, - pub uploader_url: String, - pub uploader_avatar: String, - pub thumbnail_url: String, - pub hls: String, - pub duration: i32, - pub views: i64, - pub likes: i64, - pub lbry_id: Option, - pub dislikes: i64, - pub audio_streams: Vec, - pub video_streams: Vec, - pub related_streams: Vec, - pub subtitles: Vec, - pub livestream: bool, - } - - #[derive(Debug, Deserialize)] - #[serde(rename_all = "camelCase")] - pub struct Stream { - pub url: String, - pub format: String, - pub quality: String, - pub mime_type: String, - pub codec: Option, - pub video_only: bool, - pub bitrate: i32, - pub init_start: i32, - pub init_end: i32, - pub index_start: i32, - pub index_end: i32, - pub width: i32, - pub height: i32, - pub fps: i32, - } - - #[derive(Debug, Deserialize)] - #[serde(rename_all = "camelCase")] - pub struct Subtitle { - pub url: String, - pub mime_type: String, - pub name: String, - 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, - } -} +pub use client::PipedClient; +pub use structure::*; diff --git a/piped/src/structure.rs b/piped/src/structure.rs new file mode 100644 index 0000000..9b8ab48 --- /dev/null +++ b/piped/src/structure.rs @@ -0,0 +1,127 @@ +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Channel { + pub id: String, + pub name: String, + pub avatar_url: String, + pub banner_url: String, + pub description: String, + pub nextpage: Option, + pub nextbody: Option, + pub related_streams: Vec, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Playlist { + pub name: String, + pub thumbnail_url: String, + pub banner_url: Option, + pub uploader: String, + pub uploader_url: String, + pub uploader_avatar: String, + pub videos: i32, + pub nextpage: Option, + pub nextbody: Option, + pub related_streams: Vec, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct StreamsPage { + pub nextpage: Option, + pub nextbody: Option, + pub related_streams: Vec, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct RelatedStream { + pub url: String, + pub title: String, + pub thumbnail: String, + pub uploader_avatar: Option, + pub uploader_name: String, + pub uploader_url: String, + pub uploaded_date: Option, + pub uploader_verified: bool, + pub duration: i32, + pub views: i64, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct VideoInfo { + pub title: String, + pub description: String, + pub dash: Option, + pub upload_date: String, + pub uploader: String, + pub uploader_url: String, + pub uploader_avatar: String, + pub thumbnail_url: String, + pub hls: String, + pub duration: i32, + pub views: i64, + pub likes: i64, + pub lbry_id: Option, + pub dislikes: i64, + pub audio_streams: Vec, + pub video_streams: Vec, + pub related_streams: Vec, + pub subtitles: Vec, + pub livestream: bool, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Stream { + pub url: String, + pub format: String, + pub quality: String, + pub mime_type: String, + pub codec: Option, + pub video_only: bool, + pub bitrate: i32, + pub init_start: i32, + pub init_end: i32, + pub index_start: i32, + pub index_end: i32, + pub width: i32, + pub height: i32, + pub fps: i32, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Subtitle { + pub url: String, + pub mime_type: String, + pub name: String, + 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, +}