#!/usr/bin/env ruby

require 'rubygems'
require 'tweetstream'
require 'solr'
require 'pp'

tweet_count = 0
skip_count = 0

STOPWORDS = %w{the to and is in it you of for on my that at with me do have just this be so are not was but out 
  up what now new from your like good no get all about we if time as day will one twitter how can some an am by 
  going they go or has know today there love more work too got he back think did lol when see really had great off 
  would need here thanks been blog still people who night want why home should well much then right make last over 
  way can't does getting watching its only her post his morning very she them could first than better after tonight 
  our again down news man im looking us tomorrow best into any hope week nice show yes where take check come trying 
  fun say working next happy were even live watch feel thing life little never something bad free doing world video 
  sure yeah bed let use their look being long done sleep before year find awesome big things ok another him cool 
  old ever help anyone made ready days die other read because two playing though house always also listening maybe}

  pp STOPWORDS if ENV['DEBUG']=="true"

  client = TweetStream::Client.new('rickyrobinson','woodstock')
  solr = Solr::Connection.new('http://broadway.citemine.com:8983/solr', :autocommit => :on)

  pp "Connected to Solr" if ENV['DEBUG']=="true"

  client.on_error do |message|
    pp message
  end

  client.on_delete do |status_id, user_id|
    Tweet.delete_all(["status_id = ?", status_id])
    puts "Deleted tweet."
  end

  client.on_limit do |skipped|
    puts "Skipped #{skipped} tweets due to rate limiting."
    skip_count = skipped
  end

  client.filter(:track => STOPWORDS) do |status|

    begin
      doc = {}

      doc[:id] = status.id
      doc[:fromID] = status.user.id
      doc[:fromName] = status.user.screen_name
      doc[:toID] = status.in_reply_to_user_id unless status.in_reply_to_user_id.nil?
      doc[:toName] = status.in_reply_to_screen_name unless status.in_reply_to_screen_name.nil?

      # Check for new style retweet
      if status.has_key? 'retweeted_status'
        doc[:isRT] = true
        doc[:originID] = status.retweeted_status.user.id
        doc[:originName] = status.retweeted_status.user.screen_name
      elsif match = status.text.match(/^ *RT *:? *@ *(\w+)/)
        # old style retweet
        doc[:originName] = match[1]
        doc[:isRT] = true
      else
        doc[:isRT] = false
      end

      doc[:timestamp] = Time.parse(status.created_at).utc.xmlschema
      doc[:text] = status.text

      pp doc if ENV['DEBUG']=="true"

      solr.add(doc)

      tweet_count += tweet_count
      
      pp "Count: #{tweet_count}" if ENV['DEBUG']=="true"
    rescue Exception => e
      pp e
    end

  end

