Exemple #1
0
  public static void main(String[] args) {
    try {
      // This thing blocks
      streaming("football", "volleyball", "basketball");

      List<String> trendingTopics = getTrendsByWoeid(1);

      for (String tweet : trendingTopics) {
        System.out.println(parseText(tweet));
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (TwitterException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
 /*package*/ RelationshipJSONImpl(HttpResponse res, JSONObject json) throws TwitterException {
   super(res);
   try {
     JSONObject relationship = json.getJSONObject("relationship");
     JSONObject sourceJson = relationship.getJSONObject("source");
     JSONObject targetJson = relationship.getJSONObject("target");
     sourceUserId = getLong("id", sourceJson);
     targetUserId = getLong("id", targetJson);
     sourceUserScreenName = getUnescapedString("screen_name", sourceJson);
     targetUserScreenName = getUnescapedString("screen_name", targetJson);
     sourceBlockingTarget = getBoolean("blocking", sourceJson);
     sourceFollowingTarget = getBoolean("following", sourceJson);
     sourceFollowedByTarget = getBoolean("followed_by", sourceJson);
     sourceNotificationsEnabled = getBoolean("notifications_enabled", sourceJson);
   } catch (JSONException jsone) {
     throw new TwitterException(jsone.getMessage() + ":" + json.toString(), jsone);
   }
 }
  private void init(JSONObject json) throws TwitterException {
    id = getLong("id", json);
    text = getUnescapedString("text", json);
    source = getUnescapedString("source", json);
    createdAt = getDate("created_at", json);
    isTruncated = getBoolean("truncated", json);
    inReplyToStatusId = getLong("in_reply_to_status_id", json);
    inReplyToUserId = getLong("in_reply_to_user_id", json);
    isFavorited = getBoolean("favorited", json);
    inReplyToScreenName = getUnescapedString("in_reply_to_screen_name", json);
    retweetCount = getLong("retweet_count", json);
    try {
      if (!json.isNull("user")) {
        user = new UserJSONImpl(json.getJSONObject("user"));
      }
    } catch (JSONException jsone) {
      throw new TwitterException(jsone);
    }
    geoLocation = z_T4JInternalJSONImplFactory.createGeoLocation(json);
    if (!json.isNull("place")) {
      try {
        place = new PlaceJSONImpl(json.getJSONObject("place"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse place:" + json);
      }
    }

    if (!json.isNull("retweeted_status")) {
      try {
        retweetedStatus = new StatusJSONImpl(json.getJSONObject("retweeted_status"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse retweeted_status:" + json);
      }
    }
    if (!json.isNull("contributors")) {
      try {
        JSONArray contributorsArray = json.getJSONArray("contributors");
        contributorsIDs = new long[contributorsArray.length()];
        for (int i = 0; i < contributorsArray.length(); i++) {
          contributorsIDs[i] = Long.parseLong(contributorsArray.getString(i));
        }
      } catch (NumberFormatException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse contributors:" + json);
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse contributors:" + json);
      }
    } else {
      contributors = null;
    }
    if (!json.isNull("entities")) {
      try {
        JSONObject entities = json.getJSONObject("entities");
        int len;
        if (!entities.isNull("user_mentions")) {
          JSONArray userMentionsArray = entities.getJSONArray("user_mentions");
          len = userMentionsArray.length();
          userMentionEntities = new UserMentionEntity[len];
          for (int i = 0; i < len; i++) {
            userMentionEntities[i] =
                new UserMentionEntityJSONImpl(userMentionsArray.getJSONObject(i));
          }
        }
        if (!entities.isNull("urls")) {
          JSONArray urlsArray = entities.getJSONArray("urls");
          len = urlsArray.length();
          urlEntities = new URLEntity[len];
          for (int i = 0; i < len; i++) {
            urlEntities[i] = new URLEntityJSONImpl(urlsArray.getJSONObject(i));
          }
        }

        if (!entities.isNull("hashtags")) {
          JSONArray hashtagsArray = entities.getJSONArray("hashtags");
          len = hashtagsArray.length();
          hashtagEntities = new HashtagEntity[len];
          for (int i = 0; i < len; i++) {
            hashtagEntities[i] = new HashtagEntityJSONImpl(hashtagsArray.getJSONObject(i));
          }
        }

        if (!entities.isNull("media")) {
          JSONArray mediaArray = entities.getJSONArray("media");
          len = mediaArray.length();
          mediaEntities = new MediaEntity[len];
          for (int i = 0; i < len; i++) {
            mediaEntities[i] = new MediaEntityJSONImpl(mediaArray.getJSONObject(i));
          }
        }
      } catch (JSONException jsone) {
        throw new TwitterException(jsone);
      }
    }
    if (!json.isNull("annotations")) {
      try {
        JSONArray annotationsArray = json.getJSONArray("annotations");
        annotations = new Annotations(annotationsArray);
      } catch (JSONException ignore) {
      }
    }
    if (!json.isNull("current_user_retweet")) {
      try {
        myRetweetedStatus = new StatusJSONImpl(json.getJSONObject("current_user_retweet"));
      } catch (JSONException ignore) {
        ignore.printStackTrace();
        logger.warn("failed to parse current_user_retweet:" + json);
      }
    }
  }