#!/usr/bin/env ruby

require 'rubygems'
require 'rufus/scheduler'
require 'databuilder'
require 'db'

puts "Startup"

scheduler = Rufus::Scheduler.start_new

scheduler.cron '*/5 * * * *' do

  puts "Starting scrape"

  scraper = TwitterScraper.new

  TwDB.users.each do |screen_name|

    puts "Scraping #{screen_name}"

    min_max = TwDB.min_max_ids(screen_name)

    tweets = scraper.get_timeline_json(
      :screen_name => screen_name,
      :since_id => min_max[1]
      )
    if tweets.length > 0
      TwDB.add_tweets(tweets)
    end
  end

  puts "Done with scrape"

end

scheduler.join
