require 'rubygems' require 'erb' require 'twitter' class Twhere def initialize(config_file) config = YAML::load(File.open(config_file)) @locations_file = config[:twhere][:locations_file] @template_file = config[:twhere][:template_file] @tweets_file = config[:twhere][:tweets_file] @include_replies = config[:twhere][:include_replies] @include_unlocated = config[:twhere][:include_unlocated] @twitter_username = config[:twitter][:username] @twitter_password = config[:twitter][:password] @google_maps_api_key = config[:google_maps][:api_key] end def result # load known locations @locations = YAML::load(File.open(@locations_file)) # create Twitter proxy object twitter = Twitter::Base.new(Twitter::HTTPAuth.new(@twitter_username, @twitter_password)) # load saved tweets (or don't) tweets = [] begin tweets = YAML::load(File.open(@tweets_file)) if @tweets_file rescue Errno::ENOENT end # add new tweets to collection twitter.user_timeline.each do |t| # we only care about these variables h = {:twitter_id => t.id, :text => t.text, :created_at => t.created_at} # this appears to be necessary since neither Array#| nor Array#uniq works as expected tweets << h unless tweets.include? h end tweets.reject! { |t| t[:text] =~ /^@/ } unless @include_replies tweets.reject! { |t| not t[:text] =~ /#[a-z]+/ } unless @include_unlocated # sort tweets, oldest first tweets.sort! { |a, b| a[:twitter_id].to_i <=> b[:twitter_id].to_i } # save tweets to file File.open(@tweets_file, 'w') { |f| f.puts tweets.to_yaml } if @tweets_file # group tweets by location @located_tweets = {} location = nil tweets.each do |t| md = t[:text].match(/#([a-z]+)/) location = md[1] if md if location and @locations.keys.include? location @located_tweets[location] ||= [] @located_tweets[location] << t end end # open template file and process with ERB ERB.new(File.read(@template_file)).result(binding) end def run puts self.result end end