i trying write r program using package twitter
, keep running rate limit. hacked try sleep until rate limit finished. count<10
intended keep program moving if there no returnable values string. searchedtweets
global variable holds results. how can make more "r-like"? how can correctly use retryonratelimit
parameter this? how can better handle not finding tweets?
searchstring<-c('#bringbackourgirls','#yesallwomen','#heforshe','#notallmen','#icebucketchallenge','#occupycentral','#ferguson','#gamergate','#youoksis','#blacklivesmatter','#stopgamergate','#tweetlikeaneurotypical','#cancelcolbert','#umbrellarevolution','#aminext','#iftheygunnedmedown','#whyistayed','#dudesgreetingdudes','#girlslikeus','#mensrights','#mgtow','#redpill','#cheerstosochi','#notonemore','#not1more','#donlemonreporting','#crimingwhilewhite','#loveislove','#prayforparis','#lovewins','#istandwithahmed','#istandwithplannedparenthood','#shoutyourabortion','#prolife','#oromoprotests','#kony2012','#sosblakaustralia') searchedtweets<<-null lapply(searchstring,function(y){ r_tweets<-null count<-0 while(length(r_tweets)<=0 && count<10){ r_tweets<-searchtwitter(y, n=50, lang="en-us", since=null, until=null, locale=null, geocode=null, sinceid=null, maxid=null, resulttype=null, retryonratelimit=0) sys.sleep(60*2)#seconds count<-count+1 cat("count: ",count," on term: ",y,"\n") } if(count<10){ searchedtweets<<-rbind.data.frame(searchedtweets,r_tweets) sources<-sapply(r_tweets,function(x)x$getstatussource()) sources<-gsub("</a>","",sources) sources<-strsplit(sources,">") sources<-sapply(sources,function(x)ifelse(length(x)>1,x[2],x[1])) source_table=table(sources) pie(source_table[source_table>10]) cat("###term: ",y," done!\n") #pie(source_table) }else{cat("couldnt use term: ",y)} })
you should able more methodical timing requests not rate limited or how long sleep rate limit reset. response headers tell rate limit.
$ oksocial -i 'https://api.twitter.com/1.1/search/tweets.json?q=%40twitterapi' http/1.1 200 ok ... x-rate-limit-limit: 180 x-rate-limit-remaining: 179 x-rate-limit-reset: 1453674071
this give limit , remaining requests before next reset.
$ date -r 1453674071 sun 24 jan 2016 14:21:11 pst
Comments
Post a Comment