public boolean getFollowers(Twitter twitter) {
    boolean newActivity = false;

    try {
      List<User> followers =
          twitter.getFollowersList(
              useSecondAccount
                  ? AppSettings.getInstance(context).secondScreenName
                  : AppSettings.getInstance(context).myScreenName,
              -1,
              200);
      User me = twitter.verifyCredentials();

      int oldFollowerCount = sharedPrefs.getInt("activity_follower_count_" + currentAccount, 0);
      Set<String> latestFollowers =
          sharedPrefs.getStringSet(
              "activity_latest_followers_" + currentAccount, new HashSet<String>());

      Log.v(TAG, "followers set size: " + latestFollowers.size());
      Log.v(TAG, "old follower count: " + oldFollowerCount);
      Log.v(TAG, "current follower count: " + me.getFollowersCount());

      List<User> newFollowers = new ArrayList<User>();
      if (latestFollowers.size() != 0) {
        for (int i = 0; i < followers.size(); i++) {
          if (!latestFollowers.contains(followers.get(i).getScreenName())) {
            Log.v(TAG, "inserting @" + followers.get(i).getScreenName() + " as new follower");
            newFollowers.add(followers.get(i));
            newActivity = true;
          } else {
            break;
          }
        }
      }

      insertFollowers(newFollowers);

      latestFollowers.clear();
      for (int i = 0; i < 50; i++) {
        if (i < followers.size()) {
          latestFollowers.add(followers.get(i).getScreenName());
        } else {
          break;
        }
      }

      SharedPreferences.Editor e = sharedPrefs.edit();
      e.putStringSet("activity_latest_followers_" + currentAccount, latestFollowers);
      e.putInt("activity_follower_count_" + currentAccount, me.getFollowersCount());
      e.commit();
    } catch (TwitterException e) {
      e.printStackTrace();
    }

    return newActivity;
  }
  public void testUserAsJSON() throws Exception {
    // single User
    HttpClientWrapper http = new HttpClientWrapper();
    JSONObject json = getJSONObjectFromClassPath("/dao/user.json");
    User user = new UserJSONImpl(json);
    Assert.assertTrue(user.isGeoEnabled());
    Assert.assertFalse(user.isVerified());
    Assert.assertEquals(id1.screenName, user.getName());
    Assert.assertEquals(id1.screenName, user.getScreenName());
    Assert.assertNotNull(user.getLocation());
    Assert.assertNotNull(user.getDescription());
    Assert.assertNotNull(user.getProfileImageURL());
    Assert.assertNotNull(user.getURL());
    Assert.assertFalse(user.isProtected());

    Assert.assertTrue(0 <= user.getFavouritesCount());
    Assert.assertTrue(0 <= user.getFollowersCount());
    Assert.assertTrue(0 <= user.getFriendsCount());
    Assert.assertNotNull(user.getCreatedAt());
    Assert.assertNotNull(user.getTimeZone());
    Assert.assertNotNull(user.getProfileBackgroundImageURL());

    Assert.assertTrue(0 <= user.getStatusesCount());
    Assert.assertNotNull(user.getProfileBackgroundColor());
    Assert.assertNotNull(user.getProfileTextColor());
    Assert.assertNotNull(user.getProfileLinkColor());
    Assert.assertNotNull(user.getProfileSidebarBorderColor());
    Assert.assertNotNull(user.getProfileSidebarFillColor());
    Assert.assertNotNull(user.getProfileTextColor());

    Assert.assertTrue(1 < user.getFollowersCount());
    Assert.assertNotNull(user.getStatus().getCreatedAt());
    Assert.assertNotNull(user.getStatus().getText());
    Assert.assertNotNull(user.getStatus().getSource());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertEquals(-1, user.getStatus().getInReplyToStatusId());
    Assert.assertEquals(-1, user.getStatus().getInReplyToUserId());
    Assert.assertFalse(user.getStatus().isFavorited());
    Assert.assertNull(user.getStatus().getInReplyToScreenName());
    assertDeserializedFormIsEqual(user);
    Assert.assertTrue(0 <= user.getListedCount());
    List<User> users;

    // User list
    users =
        UserJSONImpl.createUserList(
            http.get("http://twitter4j.org/en/testcases/statuses/followers/T4J_hudson.json"), conf);
    Assert.assertTrue(users.size() > 0);
    assertDeserializedFormIsEqual(users);
  }
示例#3
0
  public static Record buildTweet(Schema schema, Status status) {
    GenericRecordBuilder builderTweet = new GenericRecordBuilder(schema);

    builderTweet.set("created_at", status.getCreatedAt().getTime());
    builderTweet.set("favorite_count", status.getFavoriteCount());
    builderTweet.set("favorited", status.isFavorited());
    builderTweet.set("id", status.getId());
    builderTweet.set("in_reply_to_screen_name", status.getInReplyToScreenName());
    if (status.getInReplyToStatusId() != -1)
      builderTweet.set("in_reply_to_status_id", status.getInReplyToStatusId());
    if (status.getInReplyToUserId() != -1)
      builderTweet.set("in_reply_to_user_id", status.getInReplyToUserId());
    builderTweet.set("lang", status.getLang());
    builderTweet.set("possibly_sensitive", status.isPossiblySensitive());
    builderTweet.set("retweet_count", status.getRetweetCount());
    builderTweet.set("retweeted", status.isRetweeted());
    builderTweet.set("source", status.getSource());
    builderTweet.set("text", status.getText());
    builderTweet.set("truncated", status.isTruncated());
    if (status.getWithheldInCountries() != null)
      builderTweet.set("withheld_in_countries", Arrays.asList(status.getWithheldInCountries()));
    if (status.getGeoLocation() != null)
      builderTweet.set(
          "coordinates",
          Arrays.asList(
              status.getGeoLocation().getLatitude(), status.getGeoLocation().getLongitude()));

    builderTweet.set("entities", buildEntities(schema.getField("entities").schema(), status));

    if (status.getPlace() != null)
      builderTweet.set(
          "place",
          buildPlace(schema.getField("place").schema().getTypes().get(1), status.getPlace()));

    User user = status.getUser();
    if (user != null && schema.getField("user") != null) {
      Schema schemaUser = schema.getField("user").schema();
      GenericRecordBuilder builderUser = new GenericRecordBuilder(schemaUser);
      builderUser.set("contributors_enabled", user.isContributorsEnabled());
      builderUser.set("created_at", user.getCreatedAt().getTime());
      builderUser.set("default_profile", user.isDefaultProfile());
      builderUser.set("default_profile_image", user.isDefaultProfileImage());
      builderUser.set("description", user.getDescription());
      builderUser.set(
          "entities",
          buildURLEntity(schemaUser.getField("entities").schema(), user.getURLEntity()));
      builderUser.set("favourites_count", user.getFavouritesCount());
      builderUser.set("followers_count", user.getFollowersCount());
      builderUser.set("friends_count", user.getFriendsCount());
      builderUser.set("geo_enabled", user.isGeoEnabled());
      builderUser.set("id", user.getId());
      builderUser.set("is_translator", user.isTranslator());
      builderUser.set("lang", user.getLang());
      builderUser.set("listed_count", user.getListedCount());
      builderUser.set("location", user.getLocation());
      builderUser.set("name", user.getName());
      builderUser.set("screen_name", user.getScreenName());
      builderUser.set("profile_background_color", user.getProfileBackgroundColor());
      builderUser.set("profile_background_image_url", user.getProfileBackgroundImageURL());
      builderUser.set(
          "profile_background_image_url_https", user.getProfileBackgroundImageUrlHttps());
      builderUser.set("profile_background_tile", user.isProfileBackgroundTiled());
      builderUser.set("profile_banner_url", user.getProfileBannerURL());
      builderUser.set("profile_image_url", user.getProfileImageURL());
      builderUser.set("profile_image_url_https", user.getProfileBackgroundImageUrlHttps());
      builderUser.set("profile_link_color", user.getProfileLinkColor());
      builderUser.set("profile_sidebar_border_color", user.getProfileSidebarBorderColor());
      builderUser.set("profile_sidebar_fill_color", user.getProfileSidebarFillColor());
      builderUser.set("profile_text_color", user.getProfileTextColor());
      builderUser.set("profile_use_background_image", user.isProfileUseBackgroundImage());
      builderUser.set("protected", user.isProtected());
      builderUser.set("show_all_inline_media", user.isShowAllInlineMedia());
      builderUser.set("statuses_count", user.getStatusesCount());
      builderUser.set("time_zone", user.getTimeZone());
      builderUser.set("url", user.getURL());
      builderUser.set("utc_offset", user.getUtcOffset());
      builderUser.set("verified", user.isVerified());
      if (user.getStatus() != null && schemaUser.getField("status") != null)
        builderUser.set(
            "status",
            buildTweet(schemaUser.getField("status").schema().getTypes().get(1), user.getStatus()));
      if (user.getWithheldInCountries() != null)
        builderUser.set("withheld_in_countries", Arrays.asList(user.getWithheldInCountries()));
      builderTweet.set("user", builderUser.build());
    }

    if (status.getQuotedStatus() != null && schema.getField("quoted_status") != null)
      builderTweet.set(
          "quoted_status",
          buildTweet(
              schema.getField("quoted_status").schema().getTypes().get(1),
              status.getQuotedStatus()));

    if (status.getRetweetedStatus() != null && schema.getField("retweeted_status") != null)
      builderTweet.set(
          "retweeted_status",
          buildTweet(
              schema.getField("retweeted_status").schema().getTypes().get(1),
              status.getRetweetedStatus()));

    return builderTweet.build();
  }