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