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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#![crate_name = "postmill"]

//! A HTTP API to interface with Postmill sites.
//!
//! This should make writing bots a lot easier.
//!
//! # Examples
//!
//! ```
//! use postmill::Client;
//!
//! let mut client = Client::new("https://raddle.me").unwrap();
//!
//! // Login
//! client.login("rust_postmill_test", "rust_postmill_test").unwrap();
//!
//! // Submit a new post
//! client.submit_post("TestGround", "https://git.sr.ht/~foss/postmill", "Test submission title", "Test submission body").unwrap();
//! ```
//!
//! ```
//! use postmill::Client;
//!
//! let mut client = Client::new("https://raddle.me").unwrap();
//!
//! // Print all the submission titles of a page
//! for submission in client.get_submissions_from_page("new").unwrap() {
//!     println!("Title: {}", submission.title);
//! }
//! ```

use std::error::Error;
use url::Url;
use select::document::Document;
use select::predicate::{Attr, Class, Name, Text};
use cookie::{Cookie, CookieJar};
use reqwest::header::{HeaderMap, HeaderValue};
use chrono::prelude::*;

#[derive(Debug)]
pub struct SubmissionInfo {
    /// The url of the article/site that the submission is pointing to.
    pub url: Url,
    /// The full title of the submission.
    pub title: String,
    /// The username of the author of the submission.
    pub author: String,
    /// The date the submission is posted.
    pub date: DateTime<FixedOffset>,
    /// The forum in which the submission is posted.
    pub forum: String,
    /// The submission id for the forum.
    pub id: u64,
}

pub struct Client {
    root_url: Url,
    http_client: reqwest::Client,
    csrf_token: Option<String>,
    session_cookies: CookieJar
}

impl Client {
    /// Constructs a new `Client`.
    /// This client contains the web session.
    pub fn new(root_url: &str) -> Result<Client, Box<Error>> {
        let client = reqwest::Client::builder()
            .default_headers(Client::default_headers())
            .redirect(reqwest::RedirectPolicy::none())
            .build()?;

        Ok(Client {
            root_url: Url::parse(root_url)?,
            http_client: client,
            csrf_token: None,
            session_cookies: CookieJar::new()
        })
    }

    /// Get the names of all the forums.
    pub fn forums(&mut self) -> Result<Vec<String>, Box<Error>> {
        let resp = self.do_get_request("forums/by_category")?;

        let document = Document::from_read(resp)?;

        Ok(document.find(Class("forum-group-list-item")).map(|item| item.text().trim().to_string()).collect())
    }

    /// Get a list of all the urls of submissions on a page.
    pub fn get_submissions_from_page(&mut self, url: &str) -> Result<Vec<SubmissionInfo>, Box<Error>> {
        let resp = self.do_get_request(url)?;

        let document = Document::from_read(resp)?;

        Ok(document.find(Class("submission-row")).map(|item| {
            let form_element = item.find(Name("form")).next().unwrap();
            let id = form_element.attr("action").unwrap().trim_start_matches("/sv/");

            let inner = item.find(Class("submission-inner")).next().unwrap();
            let url_element = inner.find(Attr("class", "submission-link")).next().unwrap();
            let mut url = url_element.attr("href").unwrap().to_string();
            if url.starts_with("/") {
                url = format!("{}{}", self.root_url.clone(), url);
            }
            let title = url_element.text();

            let date_element = inner.find(Name("time")).next().unwrap();
            let date = date_element.attr("datetime").unwrap();
            let forum = inner.find(Attr("class", "submission-forum")).next().unwrap().text();
            let author = inner.find(Attr("class", "  submission-submitter")).next().unwrap().text();

            SubmissionInfo {
                title: title,
                url: Url::parse(&*url).unwrap(),
                date: DateTime::parse_from_rfc3339(date).unwrap(),
                forum: forum,
                author: author,
                id: id.parse().unwrap()
            }
        }).collect())
    }

    /// Get a list of all the urls of submissions on a page from now until a certain date.
    pub fn get_submissions_from_page_until(&mut self, url: &str, date: DateTime<FixedOffset>) -> Result<Vec<SubmissionInfo>, Box<Error>> {
        let mut resp = self.do_get_request(url)?;

        let mut submissions = Vec::new();

        // Go to each page until we find the current date
        loop {
            let document = Document::from_read(resp)?;

            let mut date_encountered = false;
            submissions.extend(document.find(Class("submission-row")).map(|item| {
                let form_element = item.find(Name("form")).next()
                    .expect("Could not find element 'form' in html source");
                let id = form_element.attr("action")
                    .expect("Could not find attribute 'action' in html element 'form'")
                    .trim_start_matches("/sv/");

                let inner = item.find(Class("submission-inner")).next()
                    .expect("Could not find class 'submission-inner' in html source");
                let url_element = inner.find(Attr("class", "submission-link")).next()
                    .expect("Could not find class='submission-link' attribute in html element 'submission-inner'");
                let mut url = url_element.attr("href")
                    .expect("Could not find attribute 'href' in html element 'submission-link'").to_string();
                if url.starts_with("/") {
                    url = format!("{}{}", self.root_url.clone(), url);
                }
                let title = url_element.text();

                let date_element = inner.find(Name("time")).next()
                    .expect("Could not find element 'time' in html source");
                let date = date_element.attr("datetime")
                    .expect("Could not find attribute 'datetime' in html element 'time'");
                let forum = inner.find(Attr("class", "submission-forum")).next()
                    .expect("Could not find class='submission-forum' attribute in html source").text();
                let author = inner.find(Attr("class", "  submission-submitter")).next()
                    .expect("Could not find class='  submission-submitter' attribute in html source").text();

                SubmissionInfo {
                    title: title,
                    url: Url::parse(&*url).expect("Submission URL is not valid"),
                    date: DateTime::parse_from_rfc3339(date).expect("Submission date is not valid"),
                    forum: forum,
                    author: author,
                    id: id.parse().expect("Submission id is not valid")
                }
            })
            .filter(|sub| {
                // Remove everything that's before the date specified
                if sub.date < date {
                    date_encountered = true;
                    return false;
                }

                return true;
            }));

            if date_encountered {
                return Ok(submissions);
            }

            // Find the url
            let next_element = document.find(Class("pagination")).next()
                .expect("Could not find class 'pagination' in html source")
                .find(Class("next")).next()
                .expect("Could not find class 'next' in html element 'pagination'")
                .find(Name("a")).next()
                .expect("Could not find name 'a' in html element 'next'");
            let next_url = next_element.attr("href")
                .expect("Could not find attribute 'href' in element 'a'").to_string();
            let url = self.root_url.clone().join(&*next_url)?;
            resp = self.do_get_request_url(url)?;
        }
    }

    /// Get a list of all the urls of submissions on a page.
    pub fn get_submission_urls_from_page(&mut self, url: &str) -> Result<Vec<String>, Box<Error>> {
        let resp = self.do_get_request(url)?;

        let document = Document::from_read(resp)?;

        Ok(document.find(Class("submission-title")).map(|item| {
            let url_element = item.find(Name("a")).next().unwrap();
            url_element.attr("href").unwrap().to_string()
        }).collect())
    }

    /// Login as a user.
    /// This sets the session cookie and the csrf token which are bound to the user login.
    pub fn login(&mut self, username: &str, password: &str) -> Result<(), Box<Error>> {
        // First retreive the csrf_token and the session cookie
        let resp = self.do_get_request("login")?;

        // Get the csrf token
        //TODO make this a proper error
        let document = Document::from_read(resp)?;
        let csrf_token = document.find(Attr("name", "_csrf_token")).next()
            .expect("Could not find attribute name='_csrf_token' in html source")
            .attr("value")
            .expect("Could not find attribute 'value' in the '_csrf_token' html element");
        self.csrf_token = Some(csrf_token.to_string().clone());

        // Then use it to login
        // Create the form data
        let params = [("_username", username), ("_password", password), ("_csrf_token", csrf_token)];

        // And the form URL
        let resp = self.do_form_post_request("login_check", &params)?;

        let resp_url = Url::parse(resp.headers().get(reqwest::header::LOCATION)
                                  .expect("Header 'Location' is missing").to_str()?)?;

        // When a login is done successfully we should be automatically routed to the main page
        assert_eq!(resp_url, self.root_url);

        Ok(())
    }

    /// Comment on a forum post.
    pub fn comment_post(&mut self, forum: &str, post_id: u64, comment: &str) -> Result<u64, Box<Error>> {
        // First get the form token
        let resp = self.do_get_request(&*format!("f/{}/{}", forum, post_id))?;

        // Get the submit form token
        //TODO make this a proper error
        let document = Document::from_read(resp)?;
        let form_token = document.find(Attr("name", "comment[_token]")).next()
            .expect("Could not find attribute name='comment[_token]' in html source")
            .attr("value")
            .expect("Could not find attribute 'value' in the 'comment[_token]' html element");

        // Create the form data
        let params = [
            ("comment[_token]", form_token),
            ("comment[comment]", comment),
            ("comment[email]", ""),
            ("comment[submit]", ""),
        ];

        let resp = self.do_form_post_request(&*format!("f/{}/{}/comment_post", forum, post_id), &params)?;

        // Get the comment id, it's the last part of the location
        let comment_id = resp.headers()[reqwest::header::LOCATION].to_str()?.split("/").last()
            .expect("Could not split on '/' in 'Location' header");

        Ok(comment_id.parse()?)
    }

    /// Submit a forum post.
    pub fn submit_post(&mut self, forum: &str, url: &str, title: &str, body: &str) -> Result<(), Box<Error>> {
        // First get the form token
        let resp = self.do_get_request("submit")?;

        // Get the submit form token
        //TODO make this a proper error
        let document = Document::from_read(resp)?;
        let form_token = document.find(Attr("name", "submission[_token]")).next().unwrap().attr("value").unwrap();

        // Create the form data
        let params = [
            ("submission[_token]", form_token),
            ("submission[url]", url),
            ("submission[title]", title),
            ("submission[body]", body),
            ("submission[forum]", forum),
            ("submission[email]", ""),
            ("submission[submit]", ""),
        ];

        self.do_form_post_request("submit", &params)?;

        //TODO return submission id

        Ok(())
    }

    fn default_headers() -> HeaderMap {
        let mut headers = HeaderMap::new();

        headers.insert(reqwest::header::USER_AGENT, HeaderValue::from_static("RustBot"));

        headers
    }

    fn do_get_request_url(&mut self, url: Url) -> Result<reqwest::Response, reqwest::Error> {
        let request = self.http_client.get(url)
            // Set the cookie header
            .headers(self.construct_headers())
            .build()?;
        let resp = self.http_client.execute(request)?.error_for_status()?;
        self.add_cookies(&resp);

        Ok(resp)
    }

    fn do_get_request(&mut self, path: &str) -> Result<reqwest::Response, reqwest::Error> {
        let mut url = self.root_url.clone();
        url.set_path(path);
        let request = self.http_client.get(url)
            // Set the cookie header
            .headers(self.construct_headers())
            .build()?;
        let resp = self.http_client.execute(request)?.error_for_status()?;
        self.add_cookies(&resp);

        Ok(resp)
    }

    fn do_form_post_request(&mut self, path: &str, params: &[(&str, &str)]) -> Result<reqwest::Response, reqwest::Error> {
        let mut url = self.root_url.clone();
        url.set_path(path);
        let request = self.http_client.post(url)
            // Set the cookie header
            .headers(self.construct_headers())
            // Add the form info
            .form(&params)
            .build()?;
        let resp = self.http_client.execute(request)?.error_for_status()?;
        self.add_cookies(&resp);

        Ok(resp)
    }

    fn construct_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();

        // Add all the cookies
        self.session_cookies.iter().for_each(|cookie| {
            let cookie_str = format!("{}={}", cookie.name().clone(), cookie.value().clone());
            headers.insert(reqwest::header::COOKIE, HeaderValue::from_str(&cookie_str)
                           .expect("Could not create 'Cookie' HeaderValue from string"));
        });

        headers.insert(reqwest::header::HOST,
            HeaderValue::from_str(self.root_url.clone().host_str()
                                  .expect("Could not find host string in 'Host' header"))
            .expect("Could not create 'Host' header from HeaderValue"));

        headers.insert(reqwest::header::REFERER,
            HeaderValue::from_str(self.root_url.clone().as_str())
            .expect("Could not create 'Referer' header from HeaderValue"));

        headers
    }

    fn add_cookies(&mut self, response: &reqwest::Response) {
        response.headers().get_all(reqwest::header::SET_COOKIE).iter().for_each(|raw_cookie| {
            let raw_cookie_str = raw_cookie.to_str()
                .expect("Cookie could not be converted to string");
            let cookie = Cookie::parse(raw_cookie_str)
                .expect("Cookie could not be parsed to string");
            self.session_cookies.add(cookie.into_owned());
        });
    }
}

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

    use chrono::Duration;

    #[test]
    fn constructor() {
        Client::new("https://raddle.me").unwrap();
    }

    #[test]
    #[should_panic]
    fn constructor_wrong_url() {
        Client::new("this is not a valid url").unwrap();
    }

    #[test]
    fn forums() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        assert_eq!(raddle.forums().unwrap().contains(&"Anarchism".to_string()), true);
    }

    #[test]
    fn get_submission_urls() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        assert!(raddle.get_submission_urls_from_page("new").unwrap().len() > 0);
    }

    #[test]
    fn get_submissions_from_page() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        assert!(raddle.get_submissions_from_page("new").unwrap().len() > 0);
    }

    #[test]
    fn get_submissions_from_page_until() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        let time_2days_before = (Utc::now() - Duration::days(2)).with_timezone(&FixedOffset::east(0));
        assert!(raddle.get_submissions_from_page_until("new", time_2days_before).unwrap().len() > 0);
    }

    #[test]
    fn login() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        raddle.login("rust_postmill_test", "rust_postmill_test").unwrap();
    }

    #[test]
    #[should_panic]
    fn login_non_existing_user() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        raddle.login("non_existing_user", "non_existing_password").unwrap();
    }

    #[test]
    fn comment_post() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        raddle.login("rust_postmill_test", "rust_postmill_test").unwrap();
        raddle.comment_post("TestGround", 52992, "Test comment").unwrap();
    }

    #[test]
    fn submit_post() {
        let mut raddle = Client::new("https://raddle.me").unwrap();
        raddle.login("rust_postmill_test", "rust_postmill_test").unwrap();
        raddle.submit_post("TestGround", "https://git.sr.ht/~foss/postmill", "Test submission title", "Test submission body").unwrap();
    }
}