예제 #1
0
  private void getMyUsername() {
    // Use cache to load my username:
    if (JCache.isInCache(JCache.cache_auth_name, "me")) {
      myID = (String) JCache.get(JCache.cache_auth_name, "me");
      return;
    }

    facecraper.scrape(L_LOGIN, L_HOME, null, "{'my_username': '******' }");

    Matcher match =
        Pattern.compile("\\/.+?\\/(.+?)$").matcher(facecraper.elem("my_username", 0).attr("href"));
    if (match.find()) myID = match.group(1);

    // If the username doesn't exist prepend : "profile.php?id="
    JCache.put(JCache.cache_auth_name, "me", myID);
  }
예제 #2
0
  @SuppressWarnings("unchecked")
  public <T> T get(String path, Class<T> classtype, Object... params) {
    String[] path_split = path.split("/");

    String who_what = path_split[0];

    // Grab user/page:
    String link_user = L_HOME;

    boolean is_ID = Util.isNumeric(who_what);
    if (who_what.toLowerCase().equals("me")) {
      link_user += myID;
      who_what = myID;
    } else link_user += (is_ID ? "profile.php?id=" : "") + who_what;

    if (path_split.length == 1) {
      User new_user = new User();
      new_user.construct(
          facecraper,
          link_user
              + (is_ID
                  ? "%2Fabout&sk=about&section=contact-info&pnref=about"
                  : "/about?section=contact-info&pnref=about"),
          who_what);
      return (T) new_user;
    } else {
      switch (path_split[1].toLowerCase()) {
        case S_FRIENDS:
          int start = 0;
          int length = -1;
          try {
            start = (int) params[0];
          } catch (Exception e) {
          }
          try {
            length = (int) params[1];
          } catch (Exception e) {
          }

          if (path_split.length == 3) {
            try {
              return (T)
                  User.constructFriends(
                          facecraper,
                          link_user + (is_ID ? "&sk=friends" : "/friends"),
                          who_what,
                          Integer.parseInt(path_split[2]),
                          1)
                      .get(0);
            } catch (Exception e) {
              e.printStackTrace();
              return null;
            }
          } else
            return (T)
                User.constructFriends(
                    facecraper,
                    link_user + (is_ID ? "&sk=friends" : "/friends"),
                    who_what,
                    start,
                    length);
        case S_FEED:
          // Fetch all recent posts from a certain user:
          return (T) facecraper.scrape(L_LOGIN, link_user, null, T_FEEDS).elems("feeds");
        case S_HOME:
          {
            facecraper.setProperty(
                WebScraper.Props.ENGINE_GET_CALLBACK,
                new EngineCallback() {
                  public void before_get(PhantomJS ctx) {}

                  public void after_get(PhantomJS ctx) {
                    ctx.waitForLoad(ctx.getClient());

                    for (int i = 0; i < 10; i++) {
                      ((JavascriptExecutor) ctx.getClient())
                          .executeScript("window.scrollTo(0, document.body.scrollHeight)");
                      try {
                        Thread.sleep(500);
                      } catch (Exception e) {
                      }
                    }
                  }
                });

            return (T) facecraper.scrape(L_LOGIN, L_HOME, null, T_HOME_FEEDS).elems("home");
          }
        case S_PAGE:
          break;
        case S_PUBLISH:
          break;
        case S_SEARCH:
          break;
        case S_UNPUBLISH:
          break;
      }
    }
    return null;
  }
예제 #3
0
 public void end() {
   facecraper.end();
 }