コード例 #1
0
  public StatusWapper obtainWeibo(Timeline tm, JcUser jcUser, int page) {
    StatusWapper statusWapper = null;
    int i = 0;
    try {
      statusWapper = tm.getUserTimeline(COUNT_PER_PAGE, page, jcUser);
      LOG.debug(++callCount + "st call weibo api (now is userTimeline)");
    } catch (WeiboException e) {
      LOG.error("occured a exception when obtaining weibo use weibo api" + e);
      LOG.error(++callCount + "st call weibo api (now is comments) cause error");
      // outOfLimit = true;
      jcUser.setOutOfLimit(true);
      return null;
    }

    // download pictures and/or obtain comments
    List<Status> statuses = statusWapper.getStatuses();
    for (i = 0; i < statuses.size(); i++) {
      Status s = statuses.get(i);
      Place place = new Place();
      try {
        if (s.getLatitude() != -1 && s.getLongitude() != -1)
          s.setFormatedAddress(place.formattedAddress(s.getLatitude(), s.getLongitude()));
        else s.setFormatedAddress(null);
      } catch (Exception e) {
        LOG.error("occured a exception when format address " + e);
        LOG.error("try again");
        try {
          if (s.getLatitude() != -1 && s.getLongitude() != -1)
            s.setFormatedAddress(place.formattedAddress(s.getLatitude(), s.getLongitude()));
          else s.setFormatedAddress(null);
        } catch (Exception e2) {
          LOG.error("occured a exception when format address " + e2);
          s.setFormatedAddress(null);
        }
      }
      if (s.getCommentsCount() > 0) s.setComments(obtainComments(s));
      statuses.set(i, downloadImage(s));
      // if (callCount >= limit || outOfLimit == true)
      if (jcUser.isOutOfLimit()) break;
    }
    LOG.debug("this time get statues count is" + statuses.size());
    int index = 0;
    // if (callCount >= limit || outOfLimit == true)
    if (jcUser.isOutOfLimit()) index = i;
    else index = i + 1;
    int len = statuses.size();
    int times = len - index;
    for (int j = 0; j < times; j++) {
      Status s = statuses.remove(index);
      LOG.debug(
          "removing id:"
              + s.getId()
              + ", creatAt:"
              + StringUtil.formatDate(s.getCreatedAt(), "yyyy-MM-dd HH:mm"));
    }
    LOG.debug("after remove get statues count is" + statuses.size());
    LOG.debug("after remove get statusWapper.statues count is" + statusWapper.getStatuses().size());
    return statusWapper;
  }
コード例 #2
0
  /** call Weibo api to obtain weibos return count weibos have been saved */
  @Override
  public JcUser obtainWeibo(JcUser jcUser, ServletContext context) {
    int count = 0;
    int swoopCount = 0;
    int totalNumber = 0;
    int totalPage = 0;
    StatusWapper statusWapper = null;

    // get user's AccessToken by sessionId.
    // this.jcUser =
    // jcUserPersistenceService.getUserBySessionId(jcUser.getSession()); it
    // should be handled by security validation
    if (jcUser.getAccessToken() == null || jcUser.getAccessToken().length() == 0) return jcUser;
    this.jcUser = jcUser;
    preparePath(context);

    tm.client.setToken(jcUser.getAccessToken());
    c.client.setToken(jcUser.getAccessToken());

    // begin to obtain weibo from weibo api.
    statusWapper = obtainWeibo(tm, jcUser, 1);
    count += weiboPersistenceService.saveStatuses(statusWapper.getStatuses());
    swoopCount += statusWapper.getStatuses().size();

    totalNumber = (int) (statusWapper.getTotalNumber()); // total weibos the
    // user have
    totalPage = totalNumber % 100 == 0 ? totalNumber / 100 : totalNumber / 100 + 1;
    // because weibo api has a limit that only 100 weibos can be obtained
    // each request.
    for (int page = 2; page < totalPage; page++) {
      statusWapper = obtainWeibo(tm, jcUser, page);
      // if (callCount >= limit || outOfLimit == true)
      if (jcUser.isOutOfLimit()) break;
      count += weiboPersistenceService.saveStatuses(statusWapper.getStatuses());
      swoopCount += statusWapper.getStatuses().size();
    }
    LOG.debug("obtained " + swoopCount + " Weibos");
    LOG.debug("save " + count + " Weibos");
    this.jcUser.setWeiboCount(count);
    this.jcUser.setThumbnailCount(thumbnailCount);
    this.jcUser.setLargeCount(largeCount);
    this.jcUser.setCommentCount(commentCount);
    return this.jcUser;
  }