qwerty1911
qwerty1911

Reputation: 147

Best way to check for a new tweet

Heres a piece of code from my irc bot. What it does is that it checks for new tweets at an specific account, then replies it in the channel. Now, is there an better way to check for new tweets? that for i in 1..999999999 feels a bit unoptimal, and 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

Upvotes: 0

Views: 139

Answers (2)

Jakub Hampl
Jakub Hampl

Reputation: 40543

First of all, for an infinite loop use the loop method.

Best for this kind of thing is to use Twitter's streaming api. This api will send a single request to Twitter, which will then push any new data to your client. For that there is a gem called TweetStream.

Example usage:

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

Upvotes: 3

shime
shime

Reputation: 9008

Is that your infinite loop?

Change it to this to improve sanity

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

You're missing the two end keywords.

Upvotes: 0

Related Questions