Search code examples
javaapireddit

How can I generate a list of Reddit saved items using jraw?


I'm trying to generate a list of all my saved reddit items using JRAW.

I've gone through the Quickstart , and successfully managed to login and retrieve information, and I can get a list of items on the Frontpage from the Cookbook, but I can't work out how I would get a list of my saved items (comments and posts) or a list of my own posts (also comments and posts).

The saved items are at https://www.reddit.com/user/<username>/saved/, but I don't know how to get jraw to retrieve and parse that, or if the api uses a different URL.

Edit: I think I probably need to use a UserContributionPaginator, but I haven't quite worked out exactly how to get it to work yet.


Solution

  • Worked it out.

    package com.jraw;
    
    import net.dean.jraw.RedditClient;
    import net.dean.jraw.http.UserAgent;
    import net.dean.jraw.http.oauth.Credentials;
    import net.dean.jraw.http.oauth.OAuthData;
    import net.dean.jraw.http.oauth.OAuthException;
    import net.dean.jraw.models.Contribution;
    import net.dean.jraw.models.Listing;
    import net.dean.jraw.paginators.UserContributionPaginator;
    
    public class printSaved {
    
        public static void main(String [] args) {
            UserAgent myUserAgent = UserAgent.of("desktop", "com.jraw.printSaved", "v0.01", "user");
            RedditClient redditClient = new RedditClient(myUserAgent);
            String username = "username";
            Credentials credentials = Credentials.script(username, "<password>", "<clientId>", "<clientSecret>");
    
            OAuthData authData = null;
            try {
                authData = redditClient.getOAuthHelper().easyAuth(credentials);
            } catch (OAuthException e) {
                e.printStackTrace();
            }
            redditClient.authenticate(authData);
    
            UserContributionPaginator saved = new UserContributionPaginator(redditClient,"saved",username);
    
            Listing<Contribution> savedList = saved.next();
    
            for (Contribution item : savedList) {
                System.out.println(item);
            }
        }
    }