/
top_posts.rs
45 lines (42 loc) · 1.51 KB
/
top_posts.rs
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
extern crate rsreddit;
use rsreddit::client::Reddit;
use rsreddit::model::sort_time::SortTime;
use rsreddit::oauth2::{AuthorizationTime, RedditApiScope, RedditOAuth};
use rsreddit::util::convert_scope_vec_to_string;
fn main() {
// Show top posts for authorized user
// OAuth2 Authorization stuff
let mut reddit_oauth = RedditOAuth::default().build();
let mut scopes = Vec::new();
scopes.push(RedditApiScope::read);
let scope_string = convert_scope_vec_to_string(&scopes);
// Authenticate user. Returns bearer token
let bearer_token =
reddit_oauth.authorize_client(&scope_string, Some(AuthorizationTime::permanent));
if let Some(token) = bearer_token {
// Reddit client object
let reddit = Reddit::default().bearer_token(token).build();
// Query top posts of /r/rust with a limit of 20 posts of the current day
let answer = reddit
.top(Some("/r/rust"), SortTime::day, "", "", 0, 20, false, false)
.unwrap();
// Get "after" tag from Listing to browse the following posts
let after = answer.data.after.unwrap();
// Query hot posts after previous ones
let next_answer = reddit.top(
Some("/r/rust"),
SortTime::day,
&after,
"",
0,
20,
false,
false,
);
// Do stuff with Listing
match next_answer {
Ok(a) => println!("{:?}", a),
Err(e) => println!("{}", e),
}
}
}