Sha256: babf02cf54a208452fd4423037d802cb90dd380365f7b8397e211a1303a1c054

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

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]
    @tweets_file          = config[:twhere][:tweets_file]
    @template_file        = config[:twhere][:template_file]
    @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))
    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

    # 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 }

    # 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sarnesjo-twhere-0.0.0 lib/twhere.rb
sarnesjo-twhere-0.0.1 lib/twhere.rb