/** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { File file = new File("src/password.txt"); File actsub = new File("src/act.sub.txt"); Scanner scanner = new Scanner(file); String password = scanner.nextLine(); scanner.close(); User user = new User("RedditsOfSubstance", password); Utils.setUserAgent("RedditsofSubstance V0.1"); try { user.connect(); } catch (Exception e) { System.out.println("Couldn't connect!"); e.printStackTrace(); } List<Subreddit> subreddits = Subreddits.list(user, "popular"); List<Subreddit> actSubList = subreddits fout = new FileWriter( "Distribution_" + Double.toString(_lowerBound) + "_" + Double.toString(_highBound) + ".txt"); fileout = new PrintWriter(fout,true); fileout.print(now.getTime().toString() + ", " + weight + ","+ count +"\n"); fileout.close(); /*for (Subreddit sr : subreddits) { System.out.println(sr.getUrl() + " " + sr.getTitle()); } */ }
/** * This function returns a "response" (me.json) JSON data containing info about the user. <br> * * @return JSON data containing info about the user */ private JSONObject info() throws IOException, ParseException { if (cookie == null || modhash == null) { System.err.printf( "Please invoke the \"connect\" function before attempting to call any other API functions."); Runtime.getRuntime().exit(-1); } JSONObject jsonObject = Utils.get("", new URL("http://www.reddit.com/api/me.json"), getCookie()); return (JSONObject) jsonObject.get("data"); }
/** * 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()); }
/** * This function returns a list of messages * * @param user The user * @return A list containing messages * @throws IOException If connection fails */ public static List<Message> getMessages(User user, MessageType messageType, int limit) throws IOException { ArrayList<Message> messages = new ArrayList<Message>(); String urlString = "http://www.reddit.com/message/"; switch (messageType) { case UNREAD: urlString += "unread"; break; case INBOX: urlString += "inbox"; break; case SENT: urlString += "sent"; break; } urlString += ".json"; urlString += "?"; urlString += "limit=" + limit; URL url = new URL(urlString); JSONObject jsonObject = (JSONObject) Utils.get(url, user); // // DEBUG // // System.out.println(Utils.getJSONDebugString(jsonObject)); JSONObject data = (JSONObject) jsonObject.get("data"); JSONArray children = (JSONArray) data.get("children"); for (int i = 0; i < children.size(); i++) { JSONObject jsonData = (JSONObject) children.get(i); messages.add(new Message(jsonData)); } return messages; }