[英]Creating a persistant connection to twitter stream API using R
我目前正在使用R中的streamR包來從twitter中的過濾器流中傳輸推文。 我有一個握手的ROAuth對象,我用它。 我的代碼看起來像:
# load the Twitter auth object
load("twitter_oAuth3.RData")
load("keywords3.RData")
streamTweet = function(){
require(streamR)
require(ROAuth)
stack = filterStream(file.name="",track=keywords,timeout=500,oauth=twitter_oAuth)
return(stack)
}
我想創建一個實時應用程序,它涉及將這些推文轉儲到activeMQ主題中。 我的代碼是:
require(Rjms)
# Set logger properties
url = "tcp://localhost:61616"
type = "T"
name = "TwitterStream"
# initialize logger
topicWriter = initialize.logger(url,type,name)
topicWrite = function(input){
# print("writing to topic")
to.logger(topicWriter,input,asString=TRUE,propertyName='StreamerID',propertyValue='1')
return()
}
logToTopic = function(streamedStack){
# print("inside stack-writer")
stacklength = length(streamedStack)
print(c("Length: ",stacklength))
for(i in 1:stacklength){
print(c("calling for: ",i))
topicWrite(streamedStack[i])
}
return()
}
現在我的問題是filterStream()
需要的超時問題。 我看了一眼,發現了這個函數的調用:
url <- "https://stream.twitter.com/1.1/statuses/filter.json"
output <- tryCatch(oauth$OAuthRequest(URL = url, params = params,
method = "POST", customHeader = NULL,
writefunction = topicWrite, cainfo = system.file("CurlSSL",
"cacert.pem", package = "RCurl")), error = function(e) e)
我嘗試刪除超時組件,但它似乎不起作用。 有沒有辦法可以永久維護一個流(直到我殺了它),它會在每個推文進入主題時轉儲?
PS我知道調用twitter4j API的java實現。 但是,我不知道如何在R中做到這一點。
streamR
包的文檔提到filterStream()
timeout選項的默認選項為0,這將使連接永久保持打開狀態。
我引用:
“數字,連接到流的最大時間長度(以秒為單位)。連接將在此期間后自動關閉。例如,將超時設置為10800將使連接保持打開3小時。默認值為0,這將保持連接永久打開。“
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.