2012-05-21 60 views
0

下面是我的irc bot中的一段代碼。它所做的就是檢查特定帳戶的新推文,然後在頻道中回覆它。 現在,有沒有更好的方法來檢查新的推文?我在1..999999999感覺有點不理想,而且ddos'y。檢查新鳴叫的最佳方法

tweetzsr = {} 
xzsr = {} 
zsr = {} 

on :message, ".start zsr" do |m| 
    if zsr[m.channel] == true 
    m.reply "already doing it.." 
    else 
    m.reply "ok." 
    zsr[m.channel] = true 
    for i in 1..99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 
     sleep 60 
     puts "#{xzsr[m.channel]} = x, tweetzsr = #{tweetzsr[m.channel]}" 
     tweetzsr[m.channel] = Twitter.user_timeline("twiitterracount").first.text 
     if xzsr[m.channel] == tweetzsr[m.channel] 
     nil 
     else 
     m.reply "#{tweetzsr[m.channel]} - via twitter feed" 
     xzsr[m.channel] = tweetzsr[m.channel] 
     end 
    end 
    end 
end 

回答

0

是你的無限循環嗎?

它更改爲這對提高神智

loop do 
    sleep 60 
    newtweet[m.channel] = Twitter.user_timeline("twitteracount").first.text 
    next if oldtweet[m.channel] == newtweet[m.channel] 
    m.reply "#{newtweet[m.channel]} - via twitter" 
    oldtweet[m.channel] = newtweet[m.channel] 
end 

你錯過了兩個end關鍵字。

+0

習語紅寶石應該使用而'環do'然後'而TRUE'。 –

+1

<3更新了相應的答案 – shime

3

首先,對於無限循環使用loop方法。

最適合這種東西的是使用Twitter's streaming api。這個api會向Twitter發送一個請求,然後這個請求會將任何新的數據推送給你的客戶端。爲此,有一個名爲TweetStream的寶石。

實例:

TweetStream::Client.new.userstream do |status| 
    m.reply "#{status.text} - via twitter" 
end 
+0

我更新了第一個鏈接中的代碼,即我的「twitter部分」的所有代碼,tweetstream會如何處理它? – qwerty1911

+0

你的代碼有點混亂,所以我不完全確定你在做什麼。在上面的例子中,每個新推文都會調用一次該塊(如果檢出文檔,有關於處理刪除和錯誤的信息)。我假設你正在試圖在你展示的代碼中做什麼。 –