Example #1
0
  public static HtmlPage login(HtmlPage page, String username, String password) {
    int attempts = 0;
    boolean loggedIn = false;
    while (!loggedIn) {
      try {
        if (attempts > 3) System.exit(1);
        attempts++;
        System.out.println("Logging in... (Attempt " + attempts + ")");

        page = wc.getPage("http://torn.com/login");

        final HtmlForm form = page.getFormByName("login");

        HtmlSubmitInput button = form.getInputByName("btnLogin");

        HtmlTextInput textField = form.getInputByName("player");

        // Change the value of the text field
        textField.setValueAttribute(username);

        final HtmlPasswordInput passwordField = form.getInputByName("password");

        // Change the value of the text field
        passwordField.setValueAttribute(password);

        // Now submit the form by clicking the button and get back the second page.
        page = button.click();

        page = wc.getPage("http://torn.com/index.php");
        loggedIn = true;

      } catch (Exception e) {
        System.out.println(e.toString());
        loggedIn = false;
      }
    }
    return page;
  }
Example #2
0
  public static void main(String[] args) {
    try {
      System.out.println("Starting application...");
      java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

      wc = new WebClient(BrowserVersion.FIREFOX_10);
      // wc = new WebClient(BrowserVersion.FIREFOX_10, "50.117.67.126", 3131);
      final DefaultCredentialsProvider scp = new DefaultCredentialsProvider();
      scp.addCredentials("754d539bc6ade1ad", "454585f105747a76");
      // wc.setCredentialsProvider(scp);
      wc.setJavaScriptEnabled(false);
      wc.setThrowExceptionOnScriptError(false);
      wc.setThrowExceptionOnFailingStatusCode(false);

      // Get the first page
      HtmlPage page = wc.getPage("http://torn.com/index.php");
      if (page.asText().contains("You are no longer logged in."))
        page = login(page, "tempaccount", "temp123");
      else System.out.println("Already logged in.");

      System.out.println("Index.php loaded.");
      int loop = 0;
      int errors = 0;
      while (true) {

        System.out.println("Beginning loop " + loop + "...");
        loop++;
        try {
          if (inHospital(page) || inJail(page)) {
            System.out.println("Sleeping 5 minutes...");
            Thread.sleep(1000 * 5 * 60);
          } else {
            System.out.println("Loading page...");
            wc.setJavaScriptEnabled(true);
            page = wc.getPage("http://torn.com/gym.php");
            // page = wc.getPage("http://torn.com/crimes.php");
            while (onCaptcha(page)) {
              wc.setJavaScriptEnabled(true);
              System.out.println("Captcha encountered.");
              page = solveCaptcha(page);
              wc.setJavaScriptEnabled(false);
            }
            // System.out.println(page.getWebResponse().getContentAsString());
            trainStrength(page, 20);
            // doCrime(page, 5);
            System.out.println("Sleeping 30 minutes...");
            Thread.sleep(1000 * 15);
          }
          page = loadIndex();
        } catch (Exception e) {
          System.out.println(e.toString());
          errors++;
          page = loadIndex();
          if (errors > 3) System.exit(1);
        }
      }

    } catch (Exception e) {
      wc.closeAllWindows();
      e.printStackTrace();
    }
  }