diff --git a/Cargo.toml b/Cargo.toml index 51463768..34941ba4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,7 @@ edition = "2018" [dependencies] derive-error = "0.0.5" -reqwest = "0.11.*" -chrono = "0.4.*" -json = "0.12.*" -tokio = { version = "1.3.*", features = ["full"] } +reqwest = "0.11.2" +json = "0.12.4" +tokio = { version = "1.4.0", features = ["full"] } diff --git a/src/lib.rs b/src/lib.rs index 0e2c25aa..c2fd0586 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //!use crate::rapr::{RaprClient,RaSub}; //!#[tokio::main] //!async fn main() -> Result<(), rapr::Error> { -//! let client = RaprClient::new(); +//! let client = RaprClient::default(); //! let mut sub = RaSub::new("pics"); //! client.fetch(10, &mut sub).await?; //! for post in sub.posts { @@ -27,7 +27,7 @@ mod tests { use crate::rapr::{RaSub, RaprClient}; #[tokio::test] async fn subreddit() -> Result<(), crate::rapr::Error> { - let client = RaprClient::new(); + let client = RaprClient::default(); let mut sub = RaSub::new("rust"); client.fetch(10, &mut sub).await?; for post in sub.posts { @@ -37,7 +37,7 @@ mod tests { } #[tokio::test] async fn title() { - let client = RaprClient::new(); + let client = RaprClient::default(); let mut sub = RaSub::new("rust"); client.fetch(10, &mut sub).await.unwrap(); for post in sub.posts { diff --git a/src/rapr.rs b/src/rapr.rs index 62b14fb8..a91fdc54 100644 --- a/src/rapr.rs +++ b/src/rapr.rs @@ -1,7 +1,6 @@ extern crate json; extern crate reqwest; extern crate tokio; -// use chrono::{DateTime, Local}; use std::fmt; /// Error enum @@ -78,6 +77,7 @@ impl RaPost { } /// Parse json from `json['data']['children']` array elements. + /// Note: In case of url mission reddit make url the permalink pub fn parse(post: &json::JsonValue) -> Result { let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?; let title = post["title"].as_str().ok_or(Error::UnexpectedJson)?; @@ -88,7 +88,6 @@ impl RaPost { let mut selftext = post["selftext"].as_str(); if selftext.ok_or(Error::NoneError)?.is_empty() { - // Either unwraps and sets new value or reamins "" selftext = post["selftext_html"].as_str(); } let items = RaPostItems::new(upvotes, downvotes, permalink, url); @@ -128,23 +127,33 @@ pub struct RaprClient { } impl Default for RaprClient { + /// Return the default RaprClient fn default() -> Self { Self::new() } } impl RaprClient { + /// Make a RaprClient pub fn new() -> Self { Self { oauth: None, rwclient: reqwest::Client::new(), } } + /// Make a RaprClient with a custom user_agent (since reddit ratelimits same user agent being + /// used a ton of times) + pub fn with_user_agent(user_agent: &str) -> Result { + Ok(Self { + oauth: None, + rwclient: reqwest::Client::builder().user_agent(user_agent).build()?, + }) + } pub fn default() -> Self { Self::new() } - /// Fetch posts from subreddit and store them in the subreddit object. + /// Fetch posts from subreddit and store them in the subreddit object. /// Note: First fetch always seems to pull two pinned posts which are not marked pinned in the json pub async fn fetch(&self, count: u32, sub: &mut RaSub) -> Result, Error> { let url = match self.oauth { @@ -156,14 +165,18 @@ impl RaprClient { None => { self.rwclient .get(url) - .query(&[("limit", count)]) + .query(&[("limit", count), ("raw_json", 1)]) .send() .await? } Some(after) => { self.rwclient .get(url) - .query(&[("limit", count.to_string()), ("after", after.to_string())]) + .query(&[ + ("limit", count.to_string().as_str()), + ("after", after.as_str()), + ("raw_json", "1"), + ]) .send() .await? } @@ -180,6 +193,7 @@ impl RaprClient { for post in raw_posts { if post["kind"].as_str().ok_or(Error::NoneError)? == "t3" { + // 't3' is the 'post' type in reddit api parsed_posts.push(RaPost::parse(&post["data"])?); } }