private String addWeibo(OAuth oauth, WeiboContent weiboContent, Long txFollowerId) throws Exception { String result; String text = weiboContent.getText(); if (StringUtils.isEmpty(weiboContent.getOriginalPic())) { result = sendApi.add(oauth, WeiBoConst.ResultType.ResultType_Json, text, defaultIp); } else { result = sendApi.add_pic( oauth, WeiBoConst.ResultType.ResultType_Json, text, defaultIp, weiboContent.getOriginalPic()); } log.info("TencentWeiboServiceImpl.addWeibo()" + weiboContent + "-" + result); int errorCode = analyzeErrorCode(result); if (errorCode == 2) { // error content len error, resent. if (text.contains(",")) { weiboContent.setText(text.replaceFirst(",", ",")); } else if (text.contains("。")) { weiboContent.setText(text.replaceFirst("。", ".")); } else { String errorMsg = "send tx weibo failed since error content len error, please check the text!" + weiboContent + "-" + result; log.info(errorMsg); throw new Exception(errorMsg); } return addWeibo(oauth, weiboContent, txFollowerId); } else if (errorCode == 0) { // success String weiboId = analyzeWeiboId(result); syncRecordService.addSyncRecord( new SyncRecord( txFollowerId, weiboId, DateUtil.formatDate(new Date(), Constants.SYNC_DATE_FORMATE))); Thread.sleep(1000); return weiboId; } throw new Exception("send tx weibo failed!"); }
@Override public void write(List<WeiboContent> weiboContents) throws Exception { Long txFollowerId = follower.getSeqId(); OAuth oauth = TencentOAuthCache.getOAuth(txFollowerId, true); if (oauth == null) { log.info("no tx oauth,please check it " + txFollowerId); throw new Exception("no tencent cache"); } if (weiboContents != null && !weiboContents.isEmpty()) { for (WeiboContent weiboContent : weiboContents) { WeiboContent retweetedContent = weiboContent.getRetweetedContent(); if (retweetedContent == null) { addWeibo(oauth, weiboContent, txFollowerId); } else { // first far a weibo for origion weibo. String result = addWeibo(oauth, retweetedContent, txFollowerId); // second talk about. String response = sendApi.comment( oauth, WeiBoConst.ResultType.ResultType_Json, weiboContent.getText(), defaultIp, result.toString()); int errorCode = analyzeErrorCode(response); if (errorCode == 0) { // success String weiboId = analyzeWeiboId(response); syncRecordService.addSyncRecord( new SyncRecord( txFollowerId, weiboId, DateUtil.formatDate(new Date(), Constants.SYNC_DATE_FORMATE))); Thread.sleep(1000); } } } } }