[ create a new paste ] login | about

Link: http://codepad.org/WQN2cYfi    [ raw code | fork | 1 comment ]

Ruby, pasted on Jan 12:
 def update(username)
      location = Twitter.user("#{username}").location
      db = SQLite3::Database.open(@database)
      count_tweets = 0
      saved_tweets = Array.new
      already_saved = 0
      retries = 0
      db.execute("SELECT content_id FROM #{@tableid}").each {|x| saved_tweets << x.join}
      b = Benchmark.realtime do
       begin
        (1..10).each do |page|
            Twitter.user_timeline(username, :page => page, :count => 50).each do |tweet|
               if saved_tweets.include?(tweet.id.to_s)
                 already_saved += 1
               else
                 time = date_time(tweet.created_at).to_time.to_s
                 db.execute("INSERT INTO  #{@tableid} ( username, pdate, content, content_id, location) VALUES(?,?,?,?,?)", username, time, tweet.text, tweet.id, location)
                 printf('*')
                 count_tweets += 1
               end
            end
          end
       rescue Exception => e
         retries += 1
         if retries > 3
           puts "\nThere was a connection problem. Please try later."
         else
           puts "\nFailed #{retries} time(s) to fetch data... Retrying...\n"
           retry
         end
       end
      end
      db.close
      if count_tweets > 0
        printf("\n%i tweets saved to database in %.2f seconds\n", count_tweets, b)
      elsif already_saved > 0
        puts "#{already_saved} tweets are already saved in your database"
      printf("Time elapsed during check out: %.2f seconds\n", b)
      end
  end


Create a new paste based on this one


Comments:
posted by atma on Jan 12
This is the error I get on the terminal. For some reason the begin/rescue clause do not work.

$ ruby morula -s dens update
******************/opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter/response/raise_server_error.rb:15:in `on_complete': Twitter is down or being upgraded. (Twitter::Error::BadGateway)
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/response.rb:9:in `block in call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/response.rb:62:in `on_complete'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/response.rb:8:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/response.rb:8:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/response.rb:8:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/request/url_encoded.rb:14:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/request/multipart.rb:13:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter/request/multipart_with_file.rb:17:in `call'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/connection.rb:207:in `run_request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/faraday-0.7.5/lib/faraday/connection.rb:89:in `get'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter/request.rb:23:in `request'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter/request.rb:11:in `get'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter/client/timelines.rb:208:in `user_timeline'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/twitter-2.0.2/lib/twitter.rb:17:in `method_missing'
from morula:116:in `block (2 levels) in update'
from morula:115:in `each'
from morula:115:in `block in update'
from /opt/local/lib/ruby1.9/1.9.1/benchmark.rb:295:in `realtime'
from morula:114:in `update'
from morula:241:in `<main>'

reply