Skip to content

Instantly share code, notes, and snippets.

@ts-3156
Forked from kimukou/getFollowingUsers2.groovy
Created March 4, 2011 04:57
Show Gist options
  • Save ts-3156/854193 to your computer and use it in GitHub Desktop.
Save ts-3156/854193 to your computer and use it in GitHub Desktop.
// forkを利用してみたいからこの1行だけ追加してみた!
// test using: 1)Groovy Console Java Web Start
// http://dl.getdropbox.com/u/653108/groovy/console.jnlp
//
// 2)IntelliJ 10.0.1 CE
// Grape Plugin : http://plugins.intellij.net/plugin/?idea&id=4702
//
// reference site: bluepapa32’s site
// http://d.hatena.ne.jp/bluepapa32/20101228/1293466511
@Grab(group = 'org.twitter4j', module='twitter4j-core', version='[2.1,)')
import twitter4j.*
Twitter twitter = new TwitterFactory().getInstance()
// ■OAuth need api use
// necessary http://twitter.com/oauth_clients/new add application code
//def consumerKey = "hogehoge"
//def consumerSecret="fugafuga"
//twitter.setOAuthConsumer(consumerKey,consumerSecret)
def screenName="kimukou_26"
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
def eMsg = e.getMessage()
String errorCode = eMsg.substring(0, 3)
if(errorCode.startsWith("5") || errorCode.startsWith("4")) {
continuousErrorCount++
if(continuousErrorCount >= 3) {
println "return null because of three continuous error"
break
}
long currentTime = System.currentTimeMillis()
if(currentTime - lastAPICallSuccessTime > 3000){
println "return null because of The interval of the error is too long. ${(double)(currentTime - lastAPICallSuccessTime)/1000} seconds"
break
}
println eMsg
continue
}
end = System.currentTimeMillis();
println "error $apiCallCount , $screenName ,${(double)(end - start)/1000} seconds : $eMsg"
break
}
isLastAPICallSuccess = true
continuousErrorCount = 0
if(rawData == null || rawData.isEmpty())break
dataToReturn.addAll(rawData)
println "$screenName , $cursor , ${(double)(System.currentTimeMillis() - lastAPICallSuccessTime)/1000} seconds"
if(!rawData.hasNext())break
cursor = rawData.getNextCursor()
}
end = System.currentTimeMillis()
println "$screenName time:${(double)(end - start)/1000} seconds $apiCallCount counts, ${dataToReturn.size()} "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment