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
use crate::{
    error::Error,
    types::Thing,
};

// Guesses for good defaults for the user agent.

// TODO: Extract from target
const DEFAULT_PLATFORM: &str = "pc";

const DEFAULT_APP_ID: &str = env!("CARGO_PKG_NAME");
const DEFAULT_APP_VERSION: &str = env!("CARGO_PKG_VERSION");

// TODO: Is there really a good default to choose here?
const DEFAULT_REDDIT_USERNAME: &str = "deleted";

/// A client to access reddit
#[derive(Clone)]
pub struct Client {
    /// The inner http client.
    ///
    /// It probably shouldn't be used directly by you.
    /// It also sets a strange user-agent as well in accordance with reddit's request.
    pub client: reqwest::Client,
}

impl Client {
    /// Create a new [`Client`].
    pub fn new() -> Self {
        Self::new_with_user_agent(
            DEFAULT_PLATFORM,
            DEFAULT_APP_ID,
            DEFAULT_APP_VERSION,
            DEFAULT_REDDIT_USERNAME,
        )
    }

    /// Create a new [`Client`] with a user-agent.
    ///
    /// See https://github.com/reddit-archive/reddit/wiki/API#rules
    pub fn new_with_user_agent(
        platform: &str,
        app_id: &str,
        app_version: &str,
        reddit_username: &str,
    ) -> Self {
        let user_agent = format!("{platform}:{app_id}:v{app_version} (by /u/{reddit_username})");

        let mut client_builder = reqwest::Client::builder();
        client_builder = client_builder.user_agent(user_agent);

        let client = client_builder
            .build()
            .expect("failed to build reddit client");

        Self { client }
    }

    /// Get the top posts of a subreddit where subreddit is the name and num_posts is the number of posts to retrieve.
    pub async fn get_subreddit(&self, subreddit: &str, num_posts: usize) -> Result<Thing, Error> {
        let url = format!("https://www.reddit.com/r/{subreddit}.json?limit={num_posts}");
        let res = self.client.get(&url).send().await?.error_for_status()?;

        // Reddit will redirect us here if the subreddit could not be found.
        const SEARCH_URL: &str = "https://www.reddit.com/subreddits/search.json?";
        if res.url().as_str().starts_with(SEARCH_URL) {
            return Err(Error::SubredditNotFound);
        }

        let text = res.text().await?;
        serde_json::from_str(&text).map_err(|error| Error::Json {
            data: text.into(),
            error,
        })
    }

    /// Get the post data for a post from a given subreddit
    pub async fn get_post(&self, subreddit: &str, post_id: &str) -> Result<Vec<Thing>, Error> {
        let url = format!("https://www.reddit.com/r/{subreddit}/comments/{post_id}.json");
        Ok(self
            .client
            .get(&url)
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?)
    }
}

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

#[cfg(test)]
mod test {
    use super::*;

    async fn get_subreddit(name: &str) -> Result<(), Error> {
        let client = Client::new();
        // 25 is the default
        let subreddit = client.get_subreddit(name, 100).await?;
        println!(
            "# of children: {}",
            subreddit.data.as_listing().unwrap().children.len()
        );
        Ok(())
    }

    #[tokio::test]
    #[ignore]
    async fn get_post_works() {
        let post_data = [
            ("dankmemes", "h966lq"),
            // ("dankvideos", "h8p0py"), // Subreddit got privated, last tested 12/23/2022. Uncomment in the future to see if that is still the case.
            ("oddlysatisfying", "ha7obv"),
        ];
        let client = Client::new();

        for (subreddit, post_id) in post_data.iter() {
            let post = client
                .get_post(subreddit, post_id)
                .await
                .expect("failed to get post");
            dbg!(&post);
        }
    }

    #[tokio::test]
    #[ignore]
    async fn get_subreddit_works() {
        let subreddits = [
            "forbiddensnacks",
            "dankmemes",
            "cursedimages",
            "MEOW_IRL",
            "cuddleroll",
            "cromch",
            "cats",
            "cursed_images",
            "aww",
        ];

        for subreddit in subreddits.iter() {
            match get_subreddit(subreddit).await {
                Ok(()) => {}
                Err(Error::Json { data, error }) => {
                    let line = error.line();
                    let column = error.column();

                    // Try to get error in data
                    let maybe_data = data.split('\n').nth(line.saturating_sub(1)).map(|line| {
                        let start = column.saturating_sub(30);

                        &line[start..]
                    });

                    let _ = tokio::fs::write("subreddit-error.json", data.as_bytes())
                        .await
                        .is_ok();

                    panic!(
                        "failed to get subreddit \"{subreddit}\": {error:#?}\ndata: {maybe_data:?}"
                    );
                }
                Err(error) => {
                    panic!("failed to get subreddit \"{subreddit}\": {error:#?}");
                }
            }
        }
    }

    #[tokio::test]
    #[ignore]
    async fn invalid_subreddit() {
        let client = Client::new();
        let error = client.get_subreddit("gfdghfj", 25).await.unwrap_err();
        assert!(error.is_subreddit_not_found(), "error = {error:#?}");
    }
}