Android Open Source - Reddit-Underground Reddit






From Project

Back to project page Reddit-Underground.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project Reddit-Underground listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
Copyright 2013 Cory Dissinger/*from   w  ww.  java 2  s  .  c o m*/

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package com.cd.reddit;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.cd.reddit.http.RedditRequestor;
import com.cd.reddit.http.util.RedditApiParameterConstants;
import com.cd.reddit.http.util.RedditApiResourceConstants;
import com.cd.reddit.http.util.RedditRequestInput;
import com.cd.reddit.http.util.RedditRequestResponse;
import com.cd.reddit.json.jackson.RedditJsonParser;
import com.cd.reddit.json.mapping.RedditAccount;
import com.cd.reddit.json.mapping.RedditComment;
import com.cd.reddit.json.mapping.RedditJsonMessage;
import com.cd.reddit.json.mapping.RedditLink;
import com.cd.reddit.json.mapping.RedditMessage;
import com.cd.reddit.json.mapping.RedditSubreddit;
import com.cd.reddit.json.util.RedditComments;

/**
 * Highest level API wrapper class. Primary goal should be to provide most intuitive, powerful methods to easily get de-serialized JSON
 * from the Reddit API.
 * <br/>
 * <br/>
 *  Please see the <a href="https://github.com/corydissinger/raw4j/blob/master/IMPLEMENTED_CALLS.md">Github Markdown</a> for full code examples.
 * <br/> 
 * <br/>
 * 
 * @see <a href="http://www.reddit.com/dev/api">Reddit's Full, Live Built-In Documentation</a>
 * 
 * @author <a href="https://github.com/corydissinger">Cory Dissinger</a>
 * @author <a href="https://github.com/ifrins">Francesc Bruguera</a>
 * @author <a href="https://github.com/wizang">Lucas Hill</a>
 *
 */
public class Reddit {
  private final RedditRequestor requestor;
  
  /**
   * @param userAgent The user agent to set
   */
  public Reddit(String userAgent){
        requestor = new RedditRequestor(userAgent);    
  }
        
    /**
     * 
     * @param userAgent The user agent to set
     * @param session The reddit session id to pass to the <code>RedditRequestor</code>
     */
    public Reddit(String userAgent, String session) {
        requestor = new RedditRequestor(userAgent);
        requestor.setSession(session);
    }
        
    /**
     * Once this method is called the cookie returned is set as the session and will be added to all future calls
     * 
     * @param userName Username of the reddit account
     * @param password Password of the reddit acount
     * @return A <code>RedditJsonMessage</code>
     * @throws RedditException 
     */
  public RedditJsonMessage login(final String userName, final String password) throws RedditException{
    final List<String> path = new ArrayList<String>(2);
    final Map<String, String> form = new HashMap<String, String>(2);
    
    path.add(RedditApiResourceConstants.API);
    path.add(RedditApiResourceConstants.LOGIN);

    form.put(RedditApiParameterConstants.API_TYPE, RedditApiParameterConstants.JSON);    
    form.put(RedditApiParameterConstants.USER, userName);
    form.put(RedditApiParameterConstants.PASSWD, password);
    
    final RedditRequestInput requestInput = new RedditRequestInput(path, null, form);
    final RedditRequestResponse response = requestor.executePost(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    final RedditJsonMessage message = parser.parseJsonMessage();
    
    if(!message.getErrors().isEmpty()){
      throw new RedditException("Got errors while logging in: " + message.toString());
    }
    
        requestor.setSession(message.getCookie());
        requestor.setModhashHeader(message.getModhash());
        
    return message;
  }

    public RedditJsonMessage newCaptcha() throws RedditException {
        final List<String> path = new ArrayList<String>(2);
        final Map<String, String> form = new HashMap<String, String>(1);

        path.add(RedditApiResourceConstants.API);
        path.add(RedditApiResourceConstants.NEW_CAPTCHA);

        form.put(RedditApiParameterConstants.API_TYPE, RedditApiParameterConstants.JSON);

        final RedditRequestInput requestInput = new RedditRequestInput(path, null, form);
        final RedditRequestResponse response = requestor.executePost(requestInput);

        final RedditJsonParser parser = new RedditJsonParser(response.getBody());
        final RedditJsonMessage message = parser.parseJsonMessage();

        if (!message.getErrors().isEmpty()){
            throw new RedditException("Got errors while requesting captcha: " + message.toString());
        }

        return message;
    }

  
  public RedditAccount meJson() throws RedditException{
    final List<String> path = new ArrayList<String>(2);
    
    path.add(RedditApiResourceConstants.API);
    path.add(RedditApiResourceConstants.ME_JSON);    
    
    final RedditRequestInput requestInput = new RedditRequestInput(path);
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    final RedditAccount account = parser.parseAccounts().get(0);
    
        requestor.setModhashHeader(account.getModhash());
    
    return account;
  }
  
  //See Reddit's built-in documentation for more detailed info: http://www.reddit.com/dev/api#GET_subreddits_{where}
  public List<RedditSubreddit> subreddits(String byGrouping) throws RedditException{
    final List<String> path = new ArrayList<String>(2);
    
    path.add(RedditApiResourceConstants.SUBREDDITS);
    path.add(byGrouping + RedditApiResourceConstants.DOT_JSON);
    
    final RedditRequestInput requestInput = new RedditRequestInput(path);
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    RedditJsonParser parser = new RedditJsonParser(response.getBody());
    return parser.parseSubreddits();
  }

    //See Reddit's built-in documentation for more detailed info: http://www.reddit.com/dev/api#GET_subreddits_{where}
    //Added ability to include the number of subreddits to pull (limit)
    public List<RedditSubreddit> subreddits(String byGrouping, int limit) throws RedditException{
        final List<String> path = new ArrayList<String>(2);

        path.add(RedditApiResourceConstants.SUBREDDITS);
        path.add(byGrouping + RedditApiResourceConstants.DOT_JSON + "?limit=" + limit);

        final RedditRequestInput requestInput = new RedditRequestInput(path);
        final RedditRequestResponse response = requestor.executeGet(requestInput);

        RedditJsonParser parser = new RedditJsonParser(response.getBody());
        return parser.parseSubreddits();
    }
  
  public List<RedditLink> listingFor(final String subreddit, final String listingType) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(3);

    pathSegments.add(RedditApiResourceConstants.R);
    pathSegments.add(subreddit);    
    pathSegments.add(listingType + RedditApiResourceConstants.DOT_JSON);
    
    final RedditRequestInput requestInput 
      = new RedditRequestInput(pathSegments);
    
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    
    return parser.parseLinks();
  }

  //TODO: The can of worms begins here. Check example-morechildren.json to see what I mean.
  public List<RedditComment> moreChildrenFor(RedditComments theComments, String desiredSort) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(2);
    final Map<String, String> form = new HashMap<String, String>(2);
    
    pathSegments.add(RedditApiResourceConstants.API);
    pathSegments.add(RedditApiResourceConstants.MORECHILDREN);

    final List<String> childrenList = theComments.getMore().getChildren();
    final String linkId       = theComments.getParentLink().getName();
    
    form.put(RedditApiParameterConstants.API_TYPE, RedditApiParameterConstants.JSON);    
    form.put(RedditApiParameterConstants.CHILDREN, StringUtils.join(childrenList.iterator(), ","));
    form.put(RedditApiParameterConstants.LINK_ID, linkId);

    final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, form);
    
    final RedditRequestResponse response = requestor.executePost(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    System.out.println();
    
    return parser.parseMoreChildren();
  }  

  public RedditComments commentsFor(final String subreddit, final String linkId) throws RedditException{
    return commentsFor(subreddit, linkId, 10);
  }  
  
  public RedditComments commentsFor(final String subreddit, final String linkId, final int limit) throws RedditException{
    final List<String> pathSegments     = new ArrayList<String>(2);
    final Map<String, String> queryParams = new HashMap<String, String>(1);
    
    pathSegments.add(RedditApiResourceConstants.R); // "r"
    pathSegments.add(subreddit); // subreddit
    pathSegments.add(RedditApiResourceConstants.COMMENTS); // "comments"
    pathSegments.add(linkId + RedditApiResourceConstants.DOT_JSON);  // ".json"
    
    queryParams.put(RedditApiParameterConstants.LIMIT, Integer.toString(limit)); // "limit" : the limit variable
    
    final RedditRequestInput requestInput 
      = new RedditRequestInput(pathSegments, queryParams);
    
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    
    return parser.parseComments(limit);
  }  
  
  //TODO: Is this always a Link?
  public List<RedditLink> infoForId(final String id) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(2);
    final Map<String, String> queryParams = new HashMap<String, String>(1);
    
    pathSegments.add(RedditApiResourceConstants.API);
    pathSegments.add(RedditApiResourceConstants.INFO + RedditApiResourceConstants.DOT_JSON);    
    
    queryParams.put(RedditApiParameterConstants.ID, id);
    
    final RedditRequestInput requestInput 
      = new RedditRequestInput(pathSegments, queryParams);
    
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    
    return parser.parseLinks();
  }
  
  public RedditJsonMessage comment(String rawMarkdown, String parentId) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(2);
    final Map<String, String> form = new HashMap<String, String>(3);
    
    pathSegments.add(RedditApiResourceConstants.API);
    pathSegments.add(RedditApiResourceConstants.COMMENT);

    form.put(RedditApiParameterConstants.API_TYPE, RedditApiParameterConstants.JSON);    
    form.put(RedditApiParameterConstants.TEXT, rawMarkdown);
    form.put(RedditApiParameterConstants.THING_ID, parentId);

    final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, form);
    
    final RedditRequestResponse response = requestor.executePost(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    
    return parser.parseJsonMessage();
  }

  public void delete(String fullname) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(2);
    final Map<String, String> form = new HashMap<String, String>(2);
    
    pathSegments.add(RedditApiResourceConstants.API);
    pathSegments.add(RedditApiResourceConstants.DEL);

    form.put(RedditApiParameterConstants.API_TYPE, RedditApiParameterConstants.JSON);    
    form.put(RedditApiParameterConstants.ID, fullname);

    final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, form);
    
    final RedditRequestResponse response = requestor.executePost(requestInput);
  }
  
  
    public List<RedditMessage> messages(String inboxType) throws RedditException {
        final List<String> pathSegments = new ArrayList<String>(2);
        
        pathSegments.add(RedditApiResourceConstants.MESSAGE);
        pathSegments.add(inboxType + RedditApiResourceConstants.DOT_JSON);
        
        final RedditRequestInput requestInput
                = new RedditRequestInput(pathSegments);
        
        final RedditRequestResponse response = requestor.executeGet(requestInput);
        final RedditJsonParser parser = new RedditJsonParser(response.getBody());
        
        return parser.parseMessages();
    }

    public void vote(int voteDirection, String fullname) throws RedditException{
        final List<String> pathSegments = new ArrayList<String>(2);
        final Map<String, String> form = new HashMap<String, String>(2);

        pathSegments.add(RedditApiResourceConstants.API);
        pathSegments.add(RedditApiResourceConstants.VOTE);

        form.put(RedditApiParameterConstants.VOTE_DIRECTION, Integer.toString(voteDirection));
        form.put(RedditApiParameterConstants.ID, fullname);

        final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, form);
        final RedditRequestResponse response = requestor.executePost(requestInput);

        final RedditJsonParser parser = new RedditJsonParser(response.getBody());
        final RedditJsonMessage message = parser.parseJsonMessage();

        if(!message.getErrors().isEmpty()){
            throw new RedditException("Got errors while voting: " + message.toString());
        }
    }

    public int markNSFW(String linkId) throws RedditException {
        final List<String> pathSegments = new ArrayList<String>(2);

        pathSegments.add(RedditApiResourceConstants.API);
        pathSegments.add(RedditApiResourceConstants.MARKNSFW);

        final Map<String, String> formParams = new HashMap<String, String>(1);

        formParams.put(RedditApiParameterConstants.ID, linkId);

        final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, formParams);

        final RedditRequestResponse response = requestor.executePost(requestInput);

        return response.getStatus();
    }

    public int unmarkNSFW(String linkId) throws RedditException {
        final List<String> pathSegments = new ArrayList<String>(2);

        pathSegments.add(RedditApiResourceConstants.API);
        pathSegments.add(RedditApiResourceConstants.UNMARKNSFW);

        final Map<String, String> formParams = new HashMap<String, String>(1);

        formParams.put(RedditApiParameterConstants.ID, linkId);

        final RedditRequestInput requestInput = new RedditRequestInput(pathSegments, null, formParams);

        final RedditRequestResponse response = requestor.executePost(requestInput);

        return response.getStatus();
    }

    public RedditAccount userInfoFor(String username) throws RedditException{
        final List<String> pathSegments = new ArrayList<String>(3);

        pathSegments.add(RedditApiResourceConstants.USER);
        pathSegments.add(username);
        pathSegments.add(RedditApiResourceConstants.ABOUT);

        final RedditRequestInput requestInput = new RedditRequestInput(pathSegments);
        final RedditRequestResponse response = requestor.executeGet(requestInput);

        final RedditJsonParser parser = new RedditJsonParser(response.getBody());
        final List<RedditAccount> accounts = parser.parseAccounts();

        if (accounts.size() == 1) {
            return accounts.get(0);
        } else {
            throw new RedditException("Username " + username + " does not exist.");
        }
    }
    
  public List<RedditLink> userHistory(String userName, 
      String listingType) throws RedditException{
    final List<String> pathSegments = new ArrayList<String>(3);

    pathSegments.add(RedditApiResourceConstants.USER);
    pathSegments.add(userName);    
    pathSegments.add(listingType + RedditApiResourceConstants.DOT_JSON);
    
    final RedditRequestInput requestInput 
      = new RedditRequestInput(pathSegments);
    
    final RedditRequestResponse response = requestor.executeGet(requestInput);
    
    final RedditJsonParser parser = new RedditJsonParser(response.getBody());
    
    return parser.parseLinks();
  }
}




Java Source Code List

com.cd.reddit.RedditException.java
com.cd.reddit.Reddit.java
com.cd.reddit.exception.RedditRateLimitException.java
com.cd.reddit.http.QueryBuilder.java
com.cd.reddit.http.RedditRequestor.java
com.cd.reddit.http.util.RedditApiParameterConstants.java
com.cd.reddit.http.util.RedditApiResourceConstants.java
com.cd.reddit.http.util.RedditRequestInput.java
com.cd.reddit.http.util.RedditRequestResponse.java
com.cd.reddit.json.jackson.RedditJacksonManager.java
com.cd.reddit.json.jackson.RedditJsonParser.java
com.cd.reddit.json.mapping.RedditAccount.java
com.cd.reddit.json.mapping.RedditComment.java
com.cd.reddit.json.mapping.RedditJsonMessage.java
com.cd.reddit.json.mapping.RedditLink.java
com.cd.reddit.json.mapping.RedditMessage.java
com.cd.reddit.json.mapping.RedditMore.java
com.cd.reddit.json.mapping.RedditSubreddit.java
com.cd.reddit.json.mapping.RedditType.java
com.cd.reddit.json.util.RedditComments.java
com.cd.reddit.json.util.RedditJsonConstants.java
com.mikedaguillo.reddit_underground.ApplicationTest.java
com.mikedaguillo.reddit_underground.ImageViewScreen.java
com.mikedaguillo.reddit_underground.LoginScreen.java
com.mikedaguillo.reddit_underground.ManualEntryScreen.java
com.mikedaguillo.reddit_underground.RedditInstance.java
com.mikedaguillo.reddit_underground.RedditListItem.java
com.mikedaguillo.reddit_underground.SavedSubredditsScreen.java
com.mikedaguillo.reddit_underground.StartScreen.java
com.mikedaguillo.reddit_underground.SubredditsSelectionScreen.java
com.mikedaguillo.reddit_underground.TinyDB.java
com.mikedaguillo.reddit_underground.SubredditDatabaseModel.SubredditsDatabaseHelper.java