Skip to content

Instantly share code, notes, and snippets.

@kimukou
Forked from ts-3156/gist:853875
Created March 4, 2011 02:01
Show Gist options
  • Save kimukou/854037 to your computer and use it in GitHub Desktop.
Save kimukou/854037 to your computer and use it in GitHub Desktop.
// 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)
screenName="kimukou_26"
dataToReturn = new ArrayList<twitter4j.User>()
apiCallCount = 0
start = System.currentTimeMillis()
lastAPICallSuccessTime = start
rawData = twitter.getFriendsStatuses(screenName, -1)
apiCallCount++;
println "[$apiCallCount] $screenName , $cursor , ${System.currentTimeMillis() - lastAPICallSuccessTime/1000.0} seconds"
while(rawData != null && !rawData.isEmpty() && rawData.hasNext()){
cursor = rawData.getNextCursor()
rawData = twitter.getFriendsStatuses(screenName, cursor)
apiCallCount++;
println "[$apiCallCount] $screenName , $cursor , ${System.currentTimeMillis() - lastAPICallSuccessTime/1000.0} seconds"
dataToReturn.addAll rawData
lastAPICallSuccessTime = System.currentTimeMillis()
}
/*
long start = System.currentTimeMillis()
long end = 0
def rawData = null
def 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. ${currentTime - lastAPICallSuccessTime/1000.0} seconds"
break
}
println eMsg
continue
}
end = System.currentTimeMillis();
println "error $apiCallCount , $screenName ,${end - start/1000.0} seconds : $eMsg"
break
}
isLastAPICallSuccess = true
continuousErrorCount = 0
if(rawData == null || rawData.isEmpty())break
dataToReturn.addAll rawData
println "$screenName , $cursor , ${System.currentTimeMillis() - lastAPICallSuccessTime/1000.0} seconds"
if(!rawData.hasNext())break
cursor = rawData.getNextCursor()
}
*/
end = System.currentTimeMillis()
println "$screenName time:${end - start/1000.0} seconds $apiCallCount counts, ${dataToReturn.size()} "
println "----------------------------------------"
println " dataToReturn=${dataToReturn.dump()}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment