如何等待Twitter限速

时间:2016-01-24 21:13:25

标签: r twitter rate-limiting

我正在尝试使用包twitteR编写R程序,但我一直在运行速率限制。我一起攻击这个试图睡觉直到速率限制结束。 count<10旨在保持程序移动,如果该字符串没有可返回值。 searchedTweets是一个包含所有结果的全局变量。我怎样才能使这更像&#34; R-like&#34;?具体来说,我如何才能正确使用retryOnRateLimit参数呢?我怎样才能更好地处理找不到任何推文?

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)}
})

1 个答案:

答案 0 :(得分:0)

您应该能够更有条理地计算您的请求的时间,以便不限制速率或重置速率限制的睡眠时间。响应标头会告诉您速率限制。

$ 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

这将在下次重置之前给出限制和剩余请求。

$ date -r 1453674071
Sun 24 Jan 2016 14:21:11 PST