Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.subreddit;
  
  
  
  import java.util.List;

Class to deal with Subreddits

Author(s):
Benjamin Jakobus
Raul Rene Lepsa
 
 public class Subreddits {

    
Returns the subreddits that make up the default front page of reddit.
 
     public List<SubredditlistDefault() {
         List<Subredditsubreddits = null;
 
         try {
             JSONObject object = (JSONObject) Utils.get("/subreddits.json"null);
             JSONObject data = (JSONObjectobject.get("data");
 
             subreddits = constructList((JSONArraydata.get("children"));
 
         } catch (Exception e) {
             ..println("Error retrieving subreddits");
         }
 
         return subreddits;
     }

    
Get a list of Subreddits of a certain SubredditType

Parameters:
subredditType SubredditType (POPULAR, NEW, BANNED)
Returns:
list of Subreddits of that type, null if none are found
 
     public List<SubredditgetSubreddits(SubredditType subredditType) {
 
         List<Subredditsubreddits = null;
 
         try {
             JSONObject object = (JSONObject) Utils.get("/subreddits/" + subredditType.getValue() + ".json"null);
             JSONObject data = (JSONObjectobject.get("data");
 
             subreddits = constructList((JSONArraydata.get("children"));
 
         } catch (Exception e) {
             ..println("Error retrieving subreddits of type: " + subredditType.getValue());
         }
 
         return subreddits;
     }

    
Get a Subreddit by its name

Parameters:
subredditName name of the subreddit to retrieve
Returns:
Subreddit object representing the desired subreddit, or NULL if it does not exist
 
     public Subreddit getSubredditByName(String subredditName) {
 
         try {
             for (Subreddit sub : listDefault()) {
                 if (sub.getDisplayName().equalsIgnoreCase(subredditName)) {
                     return sub;
                 }
             }
 
         } catch (Exception e) {
             ..println("Error retrieving subreddit: " + subredditName);
         }
 
         return null;
     }

    
Creates a list of subreddits by interpreting a given JSONArray of subreddits.

Parameters:
subredditJsonArray Array containing child nodes that form the list of subreddits.
Returns:
A List of Subreddit objects
 
     private static List<SubredditconstructList(JSONArray subredditJsonArray) {
         List<Subredditsubreddits = new ArrayList<Subreddit>();
         JSONObject subredditJsonObject;
         Subreddit subreddit;
 
         for (Object object : subredditJsonArray) {
             subredditJsonObject = (JSONObjectobject;
             subredditJsonObject = (JSONObjectsubredditJsonObject.get("data");
 
             subreddit = new Subreddit();
             subreddit.setCreated(subredditJsonObject.get("created").toString());
            subreddit.setCreatedUTC(subredditJsonObject.get("created_utc").toString());
            subreddit.setDescription(subredditJsonObject.get("description").toString());
            subreddit.setDisplayName(subredditJsonObject.get("display_name").toString());
            subreddit.setId(subredditJsonObject.get("id").toString());
            subreddit.setName(subredditJsonObject.get("display_name").toString());
            subreddit.setNsfw(Boolean.parseBoolean(subredditJsonObject.get("over18").toString()));
            subreddit.setSubscribers(Integer.parseInt(subredditJsonObject.get("subscribers").toString()));
            subreddit.setTitle(subredditJsonObject.get("title").toString());
            subreddit.setUrl(subredditJsonObject.get("url").toString());
            subreddits.add(subreddit);
        }
        return subreddits;
    }
New to GrepCode? Check out our FAQ X