-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedditService.java
59 lines (47 loc) · 2.06 KB
/
RedditService.java
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
package sample.service;
import net.dean.jraw.RedditClient;
import net.dean.jraw.http.NetworkAdapter;
import net.dean.jraw.http.OkHttpNetworkAdapter;
import net.dean.jraw.http.UserAgent;
import net.dean.jraw.models.Account;
import net.dean.jraw.models.Submission;
import net.dean.jraw.models.SubredditSort;
import net.dean.jraw.models.TimePeriod;
import net.dean.jraw.oauth.Credentials;
import net.dean.jraw.oauth.OAuthHelper;
import net.dean.jraw.pagination.DefaultPaginator;
public class RedditService {
private RedditClient redditClient;
public void login(String username, String password) {
UserAgent userAgent = new UserAgent("red-reader", "com.blogspot.gochev", "v0.1", "nayden_gochev");
//TODO change it
Credentials credentials = Credentials.script("username", "password",
"18t2hp3BFVnT0Q", "-vwVXoGfNVKUD6yknrYXBlTDxO8");
// This is what really sends HTTP requests
NetworkAdapter adapter = new OkHttpNetworkAdapter(userAgent);
// Authenticate and get a RedditClient instance
redditClient = OAuthHelper.automatic(adapter, credentials);
Account me = redditClient.me().about();
System.out.println(me.getName());
}
public DefaultPaginator<Submission> getTopSubmissions(){
DefaultPaginator<Submission> paginator = redditClient.frontPage()
.limit(50) // 50 posts per page
.sorting(SubredditSort.TOP) // top posts
.timePeriod(TimePeriod.ALL) // of all time
.build();
return paginator;
// Listing<Submission> top50MostPopular = paginator.next();
// for(Submission submission : top50MostPopular) {
// System.out.println(submission.getSubreddit());
// System.out.println(submission.getTitle());
// System.out.println(submission.getCreated());
// System.out.println(submission.getAuthor());
// System.out.println(submission.getCommentCount());
// System.out.println(submission.getDistinguished());
// }
}
public String getAccountName(){
return redditClient.me().about().getName();
}
}