/** * This function logs in to reddit and returns an ArrayList containing a modhash and cookie. * * @param username The username * @param password The password * @return An array containing a modhash and cookie * @throws IOException If connection fails * @throws ParseException If parsing JSON fails */ private ArrayList<String> hashCookiePair(String username, String password) throws IOException, ParseException { ArrayList<String> values = new ArrayList<String>(); JSONObject jsonObject = Utils.post( "api_type=json&user="******"&passwd=" + password, new URL("http://www.reddit.com/api/login/" + username), getCookie()); JSONObject valuePair = (JSONObject) ((JSONObject) jsonObject.get("json")).get("data"); values.add(valuePair.get("modhash").toString()); values.add(valuePair.get("cookie").toString()); return values; }
/** Mark a message as read. */ public static void markAsRead(User user, String fullname) throws IOException { String urlString = "http://www.reddit.com/api/read_message/"; // urlString += ".json"; URL url = new URL(urlString); JSONObject jsonObject = (JSONObject) Utils.post("id=" + fullname + "&" + "uh=" + user.getModhash(), url, user); // // DEBUG // // System.out.println(Utils.getJSONDebugString(jsonObject)); }
/** * This function submits a link or self post. * * @param title The title of the submission * @param linkOrText The link of the submission or text * @param selfPost If this submission is a self post * @param subreddit Which subreddit to submit this to * @return A JSONObject * @throws IOException If connection fails * @throws ParseException If JSON parsing fails */ private JSONObject submit(String title, String linkOrText, boolean selfPost, String subreddit) throws IOException, ParseException { return Utils.post( "title=" + title + "&" + (selfPost ? "text" : "url") + "=" + linkOrText + "&sr=" + subreddit + "&kind=" + (selfPost ? "link" : "self") + "&uh=" + getModhash(), new URL("http://www.reddit.com/api/submit"), getCookie()); }