#!/usr/bin/env ruby $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'twhere' require 'optparse' config_file = nil opts = OptionParser.new do |o| o.banner = 'usage: twhere [options]' o.on('-c', '--config FILE', 'specify config file') do |c| config_file = c end o.on_tail('-h', '--help', 'show this message') do $stderr.puts o exit end o.on_tail('-V', '--version', 'show version') do version = YAML::load(File.open(File.join(File.dirname(__FILE__), '..', 'VERSION.yml'))) $stderr.puts "#{version[:major]}.#{version[:minor]}.#{version[:patch]}" exit end end opts.parse! # make sure the config option was given unless config_file $stderr.puts opts exit 1 end # load config config = YAML::load(File.open(config_file)) # make sure config was properly loaded unless config $stderr.puts "could not load config from #{config_file}" exit 1 end # make sure these keys are set [:twhere, :twitter].each do |key| unless config[key] $stderr.puts "could not find :#{key} in config" exit 1 end end # make sure these keys are set [:locations_file, :template_file].each do |key| unless config[:twhere][key] $stderr.puts "could not find :#{key} in config" exit 1 end end # make sure these keys are set [:username, :password].each do |key| unless config[:twitter][key] $stderr.puts "could not find :#{key} in config" exit 1 end end # load known locations locations = YAML::load(File.open(config[:twhere][:locations_file])) # load template template = File.read(config[:twhere][:template_file]) # load saved tweets (or don't) tweets = [] begin tweets = YAML::load(File.open(config[:twhere][:tweets_file])) if config[:twhere][:tweets_file] rescue Errno::ENOENT end # create Twhere instance and puts result twhere = Twhere.new(locations, template, config[:twitter][:username], config[:twitter][:password], tweets) puts twhere.result # save tweets to file File.open(config[:twhere][:tweets_file], 'w') { |f| f.puts tweets.to_yaml } if config[:twhere][:tweets_file]