More cleanup (#2)

* Make functions take references, moved user agent into lib

* Improved error handling
This commit is contained in:
Schmiddiii 2021-10-06 12:19:11 +02:00 committed by GitHub
parent 412a8ffa52
commit 8a7a87824f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 152 additions and 119 deletions

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let channel = client let channel = client
.channel_from_id("UCXuqSBlHAE6Xw-yeJA0Tunw".to_string()) .channel_from_id("UCXuqSBlHAE6Xw-yeJA0Tunw".to_string())

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let comments = client let comments = client
.comments_from_id("__hYx6ZzFbQ".to_string()) .comments_from_id("__hYx6ZzFbQ".to_string())

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let playlist = client let playlist = client
.playlist_from_id("PLQSoWXSpjA38FIQCvwnVNPlGPVA63WTD8".to_string()) .playlist_from_id("PLQSoWXSpjA38FIQCvwnVNPlGPVA63WTD8".to_string())

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let suggestions = client let suggestions = client
.search_suggestions("techlore".to_string()) .search_suggestions("techlore".to_string())

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let streams = client.trending("US".to_string()).await.unwrap(); let streams = client.trending("US".to_string()).await.unwrap();

View file

@ -1,16 +1,13 @@
use piped::PipedClient; use piped::PipedClient;
use reqwest::ClientBuilder; use reqwest::Client;
const INSTANCE: &'static str = "https://pipedapi.kavin.rocks";
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let httpclient = ClientBuilder::new() let httpclient = Client::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 client = PipedClient::new(httpclient, instance);
let video = client let video = client
.video_from_id("__hYx6ZzFbQ".to_string()) .video_from_id("__hYx6ZzFbQ".to_string())

View file

@ -12,5 +12,8 @@ version = "0.0.0"
[dependencies] [dependencies]
reqwest = "^0.11" reqwest = "^0.11"
url = "^2.2"
serde = {version = "^1.0", features = ["derive"]} serde = {version = "^1.0", features = ["derive"]}
serde_json = "^1.0" serde_json = "^1.0"
thiserror = "^1.0"

View file

@ -1,38 +1,46 @@
use reqwest::{Client, Url}; use reqwest::{Client, Url};
use crate::{Channel, CommentsInfo, Playlist, RelatedStream, StreamsPage, VideoInfo}; use crate::{Channel, CommentsInfo, Playlist, RelatedStream, Result, StreamsPage, VideoInfo};
pub struct PipedClient { pub struct PipedClient {
pub httpclient: Client, pub httpclient: Client,
pub instance: String, pub instance: String,
} }
const USER_AGENT: &'static str =
"Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0";
impl PipedClient { impl PipedClient {
pub fn new(httpclient: Client, instance: String) -> PipedClient { pub fn new<S: AsRef<str>>(httpclient: &Client, instance: S) -> PipedClient {
PipedClient { PipedClient {
httpclient, httpclient: httpclient.clone(),
instance, instance: instance.as_ref().to_string(),
} }
} }
pub async fn trending( pub async fn trending<S: AsRef<str>>(&self, region: S) -> Result<Vec<RelatedStream>> {
&self,
region: String,
) -> Result<Vec<RelatedStream>, Box<dyn std::error::Error>> {
let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?; let mut url = Url::parse(format!("{}/trending", &self.instance).as_str())?;
url.query_pairs_mut().append_pair("region", region.as_str()); url.query_pairs_mut().append_pair("region", region.as_ref());
let resp = &self.httpclient.get(url).send().await?.text().await?; let resp = &self
.httpclient
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let streams: Vec<RelatedStream> = serde_json::from_str(resp.as_str())?; let streams: Vec<RelatedStream> = serde_json::from_str(resp.as_ref())?;
Ok(streams) Ok(streams)
} }
pub async fn channel_from_id(&self, id: String) -> Result<Channel, Box<dyn std::error::Error>> { pub async fn channel_from_id<S: AsRef<str>>(&self, id: S) -> Result<Channel> {
let resp = &self let resp = &self
.httpclient .httpclient
.get(format!("{}/channel/{}", &self.instance, id)) .get(format!("{}/channel/{}", &self.instance, id.as_ref()))
.header("User-Agent", USER_AGENT)
.send() .send()
.await? .await?
.text() .text()
@ -43,63 +51,78 @@ impl PipedClient {
Ok(channel) Ok(channel)
} }
pub async fn channel_continuation( pub async fn channel_continuation<S: AsRef<str>>(
&self, &self,
id: String, id: S,
nexturl: String, nexturl: S,
nextbody: String, nextbody: S,
) -> Result<StreamsPage, Box<dyn std::error::Error>> { ) -> Result<StreamsPage> {
let mut url = Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id).as_str())?; let mut url =
Url::parse(format!("{}/nextpage/channels/{}", &self.instance, id.as_ref()).as_str())?;
url.query_pairs_mut() url.query_pairs_mut()
.append_pair("url", nexturl.as_str()) .append_pair("url", nexturl.as_ref())
.append_pair("id", nextbody.as_str()); .append_pair("id", nextbody.as_ref());
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 let resp = &self
.httpclient .httpclient
.get(format!("{}/playlists/{}", &self.instance, id)) .get(url)
.header("User-Agent", USER_AGENT)
.send() .send()
.await? .await?
.text() .text()
.await?; .await?;
let playlist: Playlist = serde_json::from_str(resp.as_str())?; let streams: StreamsPage = serde_json::from_str(resp.as_ref())?;
Ok(streams)
}
pub async fn playlist_from_id<S: AsRef<str>>(&self, id: S) -> Result<Playlist> {
let resp = &self
.httpclient
.get(format!("{}/playlists/{}", &self.instance, id.as_ref()))
.header("User-Agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let playlist: Playlist = serde_json::from_str(resp.as_ref())?;
Ok(playlist) Ok(playlist)
} }
pub async fn playlist_continuation( pub async fn playlist_continuation<S: AsRef<str>>(
&self, &self,
id: String, id: S,
nexturl: String, nexturl: S,
nextbody: String, nextbody: S,
) -> Result<StreamsPage, Box<dyn std::error::Error>> { ) -> Result<StreamsPage> {
let mut url = Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id).as_str())?; let mut url =
Url::parse(format!("{}/nextpage/playlists/{}", &self.instance, id.as_ref()).as_str())?;
url.query_pairs_mut() url.query_pairs_mut()
.append_pair("url", nexturl.as_str()) .append_pair("url", nexturl.as_ref())
.append_pair("id", nextbody.as_str()); .append_pair("id", nextbody.as_ref());
let resp = &self.httpclient.get(url).send().await?.text().await?; let resp = &self
.httpclient
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let streams: StreamsPage = serde_json::from_str(resp.as_str())?; let streams: StreamsPage = serde_json::from_str(resp.as_str())?;
Ok(streams) Ok(streams)
} }
pub async fn video_from_id(&self, id: String) -> Result<VideoInfo, Box<dyn std::error::Error>> { pub async fn video_from_id<S: AsRef<str>>(&self, id: S) -> Result<VideoInfo> {
let resp = &self let resp = &self
.httpclient .httpclient
.get(format!("{}/streams/{}", &self.instance, id)) .get(format!("{}/streams/{}", &self.instance, id.as_ref()))
.header("User-Agent", USER_AGENT)
.send() .send()
.await? .await?
.text() .text()
@ -110,48 +133,58 @@ impl PipedClient {
Ok(video) Ok(video)
} }
pub async fn search_suggestions( pub async fn search_suggestions<S: AsRef<str>>(&self, q: S) -> Result<Vec<String>> {
&self,
q: String,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?; let mut url = Url::parse(format!("{}/suggestions", &self.instance).as_str())?;
url.query_pairs_mut().append_pair("query", q.as_str()); url.query_pairs_mut().append_pair("query", q.as_ref());
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 let resp = &self
.httpclient .httpclient
.get(format!("{}/comments/{}", &self.instance, id)) .get(url)
.header("User-Agent", USER_AGENT)
.send() .send()
.await? .await?
.text() .text()
.await?; .await?;
let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; let suggestions: Vec<String> = serde_json::from_str(resp.as_ref())?;
Ok(suggestions)
}
pub async fn comments_from_id<S: AsRef<str>>(&self, id: S) -> Result<CommentsInfo> {
let resp = &self
.httpclient
.get(format!("{}/comments/{}", &self.instance, id.as_ref()))
.header("User-Agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?;
Ok(comments) Ok(comments)
} }
pub async fn comments_continuation( pub async fn comments_continuation<S: AsRef<str>>(
&self, &self,
id: String, id: S,
nexturl: String, nexturl: S,
) -> Result<CommentsInfo, Box<dyn std::error::Error>> { ) -> Result<CommentsInfo> {
let mut url = Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id).as_str())?; let mut url =
url.query_pairs_mut().append_pair("url", nexturl.as_str()); Url::parse(format!("{}/nextpage/comments/{}", &self.instance, id.as_ref()).as_str())?;
url.query_pairs_mut().append_pair("url", nexturl.as_ref());
let resp = &self.httpclient.get(url).send().await?.text().await?; let resp = &self
.httpclient
.get(url)
.header("User-Agent", USER_AGENT)
.send()
.await?
.text()
.await?;
let comments: CommentsInfo = serde_json::from_str(resp.as_str())?; let comments: CommentsInfo = serde_json::from_str(resp.as_ref())?;
Ok(comments) Ok(comments)
} }

13
piped/src/error.rs Normal file
View file

@ -0,0 +1,13 @@
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("{0}")]
Network(#[from] reqwest::Error),
#[error("{0}")]
ParseResponse(#[from] serde_json::error::Error),
#[error("{0}")]
Parseurl(#[from] url::ParseError),
}

View file

@ -1,5 +1,7 @@
mod client; mod client;
mod error;
mod structure; mod structure;
pub use client::PipedClient; pub use client::PipedClient;
pub use error::{Error, Result};
pub use structure::*; pub use structure::*;