1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
extern crate json;
extern crate reqwest;
extern crate tokio;
// use chrono::{DateTime, Local};
use std::fmt;

/// Error enum
#[derive(Debug, Error)]
pub enum Error {
    UnexpectedJson,
    NoneError,
    JsonParseError(json::Error),
    RequestError(reqwest::Error),
}

/// Items related to the post
#[derive(Debug, Clone)]
pub struct RaPostItems {
    pub upvotes: u32,
    pub downvotes: u32,
    pub permalink: String,
    pub url: Option<String>,
}

impl RaPostItems {
    /// Create a new struct of items for a post  
    /// You probably don't need this fucntion.
    pub fn new(upvotes: u32, downvotes: u32, permalink: &str, url: Option<String>) -> Self {
        Self {
            upvotes,
            downvotes,
            permalink: permalink.to_string(),
            url,
        }
    }
}

/// Reddit post object
/// (more like repost object).  
/// Keeps track of post information.
#[derive(Clone)]
pub struct RaPost {
    pub id: String,
    pub title: String,
    pub text: Option<String>,
    pub items: RaPostItems,
    pub json: json::JsonValue,
}

impl fmt::Debug for RaPost {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RaPost")
            .field("id", &self.id)
            .field("title", &self.title)
            .field("text", &self.text)
            .field("permalink", &self.items.permalink)
            .field("url", &self.items.url)
            .finish()
    }
}

impl RaPost {
    /// Generate new post object (you probably don't need this).
    pub fn new(
        id: &str,
        title: &str,
        text: Option<&str>,
        items: RaPostItems,
        json: &json::JsonValue,
    ) -> Self {
        Self {
            id: id.to_string(),
            title: title.to_string(),
            text: text.map(String::from),
            items,
            json: json.clone(),
        }
    }

    /// Parse json from `json['data']['children']` array elements.
    pub fn parse(post: &json::JsonValue) -> Result<RaPost, Error> {
        let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?;
        let title = post["title"].as_str().ok_or(Error::UnexpectedJson)?;
        let upvotes = post["ups"].as_u32().ok_or(Error::UnexpectedJson)?;
        let downvotes = post["downs"].as_u32().ok_or(Error::UnexpectedJson)?;
        let permalink = post["permalink"].as_str().ok_or(Error::UnexpectedJson)?;
        let url = post["url"].as_str().map(String::from);

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

        Ok(RaPost::new(id, title, selftext, items, post))
    }
}

///# RaSub
///Subreddit object   
///Keeps track of posts
#[derive(Debug)]
pub struct RaSub {
    pub name: String,
    pub posts: Vec<RaPost>,
    after: Option<String>,
}

impl RaSub {
    /// Generate subreddit object
    pub fn new(name: &str) -> RaSub {
        RaSub {
            name: String::from(name),
            posts: Vec::new(),
            after: None,
        }
    }
}

/// Reddit api client.  
/// Uses a [reqwest::Client](https://docs.rs/reqwest/0.11.2/reqwest/struct.Client.html) internally.  
/// Currently no authentication
#[derive(Debug)]
pub struct RaprClient {
    oauth: Option<String>,
    rwclient: reqwest::Client,
}

impl Default for RaprClient {
    fn default() -> Self {
        Self::new()
    }
}

impl RaprClient {
    pub fn new() -> Self {
        Self {
            oauth: None,
            rwclient: reqwest::Client::new(),
        }
    }
    pub fn default() -> Self {
        Self::new()
    }

    /// 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<Vec<RaPost>, Error> {
        let url = match self.oauth {
            None => format!("https://reddit.com/r/{}.json", sub.name),
            Some(_) => format!("https://oauth.reddit.com/r/{}.json", sub.name),
        };

        let res = match &sub.after {
            None => {
                self.rwclient
                    .get(url)
                    .query(&[("limit", count)])
                    .send()
                    .await?
            }
            Some(after) => {
                self.rwclient
                    .get(url)
                    .query(&[("limit", count.to_string()), ("after", after.to_string())])
                    .send()
                    .await?
            }
        };

        let mut parsed = json::parse(res.text().await?.as_str())?;

        let raw_posts: Vec<json::JsonValue> = match parsed["data"]["children"].take() {
            json::JsonValue::Array(arr) => arr,
            _ => return Err(Error::UnexpectedJson),
        };

        let mut parsed_posts: Vec<RaPost> = Vec::new();

        for post in raw_posts {
            if post["kind"].as_str().ok_or(Error::NoneError)? == "t3" {
                parsed_posts.push(RaPost::parse(&post["data"])?);
            }
        }
        if parsed["data"]["after"].is_string() {
            sub.after = Some(parsed["data"]["after"].to_string());
        }
        sub.posts.append(&mut parsed_posts);
        Ok(parsed_posts)
    }
}