General Cleanup (#1)

- Split large lib-file into smaller files
- Renamed getters to follow rust naming convention
- Pretty debug print in examples
- No hardcoded dependency versions in lib
This commit is contained in:
Schmiddiii 2021-10-04 17:32:57 +02:00 committed by GitHub
parent 125fdee6c2
commit 412a8ffa52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 310 additions and 314 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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"

158
piped/src/client.rs Normal file
View file

@ -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<Vec<RelatedStream>, Box<dyn std::error::Error>> {
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<RelatedStream> = serde_json::from_str(resp.as_str())?;
Ok(streams)
}
pub async fn channel_from_id(&self, id: String) -> Result<Channel, Box<dyn std::error::Error>> {
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<StreamsPage, Box<dyn std::error::Error>> {
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<Playlist, Box<dyn std::error::Error>> {
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<StreamsPage, Box<dyn std::error::Error>> {
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<VideoInfo, Box<dyn std::error::Error>> {
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<Vec<String>, Box<dyn std::error::Error>> {
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<String> = serde_json::from_str(resp.as_str())?;
Ok(suggestions)
}
pub async fn comments_from_id(
&self,
id: String,
) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
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<CommentsInfo, Box<dyn std::error::Error>> {
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)
}
}

View file

@ -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<Vec<RelatedStream>, Box<dyn std::error::Error>> {
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<RelatedStream> = serde_json::from_str(resp.as_str())?;
Ok(streams)
}
pub async fn get_channel_from_id(
&self,
id: String,
) -> Result<Channel, Box<dyn std::error::Error>> {
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<StreamsPage, Box<dyn std::error::Error>> {
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<Playlist, Box<dyn std::error::Error>> {
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<StreamsPage, Box<dyn std::error::Error>> {
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<VideoInfo, Box<dyn std::error::Error>> {
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<Vec<String>, Box<dyn std::error::Error>> {
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<String> = serde_json::from_str(resp.as_str())?;
Ok(suggestions)
}
pub async fn get_comments_from_id(
&self,
id: String,
) -> Result<CommentsInfo, Box<dyn std::error::Error>> {
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<CommentsInfo, Box<dyn std::error::Error>> {
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<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Playlist {
pub name: String,
pub thumbnail_url: String,
pub banner_url: Option<String>,
pub uploader: String,
pub uploader_url: String,
pub uploader_avatar: String,
pub videos: i32,
pub nextpage: Option<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StreamsPage {
pub nextpage: Option<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RelatedStream {
pub url: String,
pub title: String,
pub thumbnail: String,
pub uploader_avatar: Option<String>,
pub uploader_name: String,
pub uploader_url: String,
pub uploaded_date: Option<String>,
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<String>,
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<String>,
pub dislikes: i64,
pub audio_streams: Vec<Stream>,
pub video_streams: Vec<Stream>,
pub related_streams: Vec<RelatedStream>,
pub subtitles: Vec<Subtitle>,
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<String>,
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<Comment>,
pub nextpage: Option<String>,
}
#[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::*;

127
piped/src/structure.rs Normal file
View file

@ -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<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Playlist {
pub name: String,
pub thumbnail_url: String,
pub banner_url: Option<String>,
pub uploader: String,
pub uploader_url: String,
pub uploader_avatar: String,
pub videos: i32,
pub nextpage: Option<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StreamsPage {
pub nextpage: Option<String>,
pub nextbody: Option<String>,
pub related_streams: Vec<RelatedStream>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RelatedStream {
pub url: String,
pub title: String,
pub thumbnail: String,
pub uploader_avatar: Option<String>,
pub uploader_name: String,
pub uploader_url: String,
pub uploaded_date: Option<String>,
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<String>,
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<String>,
pub dislikes: i64,
pub audio_streams: Vec<Stream>,
pub video_streams: Vec<Stream>,
pub related_streams: Vec<RelatedStream>,
pub subtitles: Vec<Subtitle>,
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<String>,
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<Comment>,
pub nextpage: Option<String>,
}
#[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,
}