Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Created March 3, 2011 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ts-3156/853875 to your computer and use it in GitHub Desktop.
Save ts-3156/853875 to your computer and use it in GitHub Desktop.
this code gets users who follow a authorized user in twitter with twitter4j.
/**
* this code gets users who follow a authorized user in twitter with twitter4j.
* @param twitter
* @param screenName
* @return if a error occurs, this method returns null
*/
public static ArrayList<twitter4j.User> getFollowingUsers2(Twitter twitter, String screenName){
long start = System.currentTimeMillis();
long end = 0;
PagableResponseList<twitter4j.User> rawData = null;
ArrayList<twitter4j.User> dataToReturn = new ArrayList<twitter4j.User>();
int apiCallCount = 0;
int continuousErrorCount = 0;
boolean isLastAPICallSuccess = true;
long lastAPICallSuccessTime = 0;
long cursor = -1;
while(true){
try {
if(isLastAPICallSuccess)
lastAPICallSuccessTime = System.currentTimeMillis();
rawData = twitter.getFriendsStatuses(screenName, cursor);
apiCallCount++;
} catch (TwitterException e) {
isLastAPICallSuccess = false;
String errorCode = e.getMessage().substring(0, 3);
if(errorCode.startsWith("5") || errorCode.startsWith("4")) {
continuousErrorCount++;
if(continuousErrorCount >= 3) {
System.err.println("return null because of three continuous error");
return null;
}
long currentTime = System.currentTimeMillis();
if(currentTime - lastAPICallSuccessTime > 3000){
System.err.println("return null because of The interval of the error is too long. " + (double)(currentTime - lastAPICallSuccessTime)/1000 + "seconds");
return null;
}
System.err.println(e.getMessage());
continue;
}
end = System.currentTimeMillis();
System.err.println("error " + apiCallCount + ", " + screenName + ", " + (double)(end - start)/1000 + "seconds " + ": " + e.getMessage());
return null;
}
isLastAPICallSuccess = true;
continuousErrorCount = 0;
if(rawData == null || rawData.isEmpty())
break;
dataToReturn.addAll(rawData);
System.out.println(screenName + ", " + cursor + ", " + (double)(System.currentTimeMillis() - lastAPICallSuccessTime)/1000 + "seconds");
if(!rawData.hasNext())
break;
cursor = rawData.getNextCursor();
}
end = System.currentTimeMillis();
System.out.println("" + screenName + " time:" + (double)(end - start)/1000 + "seconds " + apiCallCount + "counts, " + dataToReturn.size());
return dataToReturn;
}
@ts-3156
Copy link
Author

ts-3156 commented Mar 4, 2011

comment test

@ts-3156
Copy link
Author

ts-3156 commented Mar 4, 2011

このコードのここが駄目ってところがありましたらforkでご指摘いただけるとありがたいです!

@bambam2829
Copy link

コメントがあると嬉しいです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment