/** * Get a new modhash by scraping and return it * * @param client * @return */ public static String doUpdateModhash(HttpClient client) { final Pattern MODHASH_PATTERN = Pattern.compile("modhash: '(.*?)'"); String modhash; HttpEntity entity = null; // The pattern to find modhash from HTML javascript area try { HttpGet httpget = new HttpGet(Constants.MODHASH_URL); HttpResponse response = client.execute(httpget); // For modhash, we don't care about the status, since the 404 page has the info we want. // status = response.getStatusLine().toString(); // if (!status.contains("OK")) // throw new HttpException(status); entity = response.getEntity(); BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); // modhash should appear within first 1200 chars char[] buffer = new char[1200]; in.read(buffer, 0, 1200); in.close(); String line = String.valueOf(buffer); entity.consumeContent(); if (StringUtils.isEmpty(line)) { throw new HttpException( "No content returned from doUpdateModhash GET to " + Constants.MODHASH_URL); } if (line.contains("USER_REQUIRED")) { throw new Exception("User session error: USER_REQUIRED"); } Matcher modhashMatcher = MODHASH_PATTERN.matcher(line); if (modhashMatcher.find()) { modhash = modhashMatcher.group(1); if (StringUtils.isEmpty(modhash)) { // Means user is not actually logged in. return null; } } else { throw new Exception("No modhash found at URL " + Constants.MODHASH_URL); } if (Constants.LOGGING) Common.logDLong(TAG, line); if (Constants.LOGGING) Log.d(TAG, "modhash: " + modhash); return modhash; } catch (Exception e) { if (entity != null) { try { entity.consumeContent(); } catch (Exception e2) { if (Constants.LOGGING) Log.e(TAG, "entity.consumeContent()", e); } } if (Constants.LOGGING) Log.e(TAG, "doUpdateModhash()", e); return null; } }
public static String checkResponseErrors(HttpResponse response, HttpEntity entity) { String status = response.getStatusLine().toString(); String line; if (!status.contains("OK")) { return "HTTP error. Status = " + status; } try { BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); line = in.readLine(); if (Constants.LOGGING) Common.logDLong(TAG, line); in.close(); } catch (IOException e) { if (Constants.LOGGING) Log.e(TAG, "IOException", e); return "Error reading retrieved data."; } if (StringUtils.isEmpty(line)) { return "API returned empty data."; } if (line.contains("WRONG_PASSWORD")) { return "Wrong password."; } if (line.contains("USER_REQUIRED")) { // The modhash probably expired return "Login expired."; } if (line.contains("SUBREDDIT_NOEXIST")) { return "That subreddit does not exist."; } if (line.contains("SUBREDDIT_NOTALLOWED")) { return "You are not allowed to post to that subreddit."; } return null; }
public static String checkIDResponse(HttpResponse response, HttpEntity entity) throws CaptchaException, Exception { // Group 1: fullname. Group 2: kind. Group 3: id36. final Pattern NEW_ID_PATTERN = Pattern.compile("\"id\": \"((.+?)_(.+?))\""); // Group 1: whole error. Group 2: the time part final Pattern RATELIMIT_RETRY_PATTERN = Pattern.compile("(you are trying to submit too fast. try again in (.+?)\\.)"); String status = response.getStatusLine().toString(); String line; if (!status.contains("OK")) { throw new Exception("HTTP error. Status = " + status); } try { BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent())); line = in.readLine(); if (Constants.LOGGING) Common.logDLong(TAG, line); in.close(); } catch (IOException e) { if (Constants.LOGGING) Log.e(TAG, "IOException", e); throw new Exception("Error reading retrieved data."); } if (StringUtils.isEmpty(line)) { throw new Exception("API returned empty data."); } if (line.contains("WRONG_PASSWORD")) { throw new Exception("Wrong password."); } if (line.contains("USER_REQUIRED")) { // The modhash probably expired throw new Exception("Login expired."); } if (line.contains("SUBREDDIT_NOEXIST")) { throw new Exception("That subreddit does not exist."); } if (line.contains("SUBREDDIT_NOTALLOWED")) { throw new Exception("You are not allowed to post to that subreddit."); } String newId; Matcher idMatcher = NEW_ID_PATTERN.matcher(line); if (idMatcher.find()) { newId = idMatcher.group(3); } else { if (line.contains("RATELIMIT")) { // Try to find the # of minutes using regex Matcher rateMatcher = RATELIMIT_RETRY_PATTERN.matcher(line); if (rateMatcher.find()) throw new Exception(rateMatcher.group(1)); else throw new Exception("you are trying to submit too fast. try again in a few minutes."); } if (line.contains("DELETED_LINK")) { throw new Exception("the link you are commenting on has been deleted"); } if (line.contains("BAD_CAPTCHA")) { throw new CaptchaException("Bad CAPTCHA. Try again."); } // No id returned by reply POST. return null; } // Getting here means success. return newId; }