#!/usr/bin/env ruby # # Quick and dirty viewing of the forecast on the command line # require 'open-uri' class Weather def initialize @zipcode = 10128 @verbose = false end def real_main(argv) while not argv.empty? arg = argv.shift if arg == '-h' or arg == '-help' print_help elsif arg == '-v' or arg == '-verbose' @verbose = true else @zipcode = arg.to_i end end run end private def run log 'Using zipcode ' + @zipcode.to_s url = 'http://www.weather.com/outlook/travel/businesstraveler/local/' + @zipcode.to_s + '?lswe=' + @zipcode.to_s + '&lwsa=Weather36HourBusinessTravelerCommand&from=searchbox_localwx' content = open(url).read lines = content.split /\n/ state = 0 times = ['Today : ', 'Tonight : ', 'Tomorrow : '] forecast = nil type = nil temp = nil puts lines.each do |line| line.scan /([^<]+)([^<]+)
/i do |res| type = res[0] end end if type line.scan /([^<]+)/ do |res| temp = res[0] temp = temp.gsub /&[^;]*;/, '' end end if forecast and type and temp time = times[state] state += 1 puts time + forecast + ' ' + type + ' ' + temp forecast = nil type = nil temp = nil end end end def log(s) if @verbose STDERR.puts '*** ' + s STDERR.flush end end def print_help def e(s) STDERR.puts s end e 'Usage ' + File.basename($0) + ' ? ?' e 'where options include' e ' -h || -help print this message' e ' -v || -verbose use verbose mode' end end def main(argv) Weather.new.real_main argv end main ARGV