Since I work on Linux/OSX command line all day, I prefer to get my information such as stock quote or weather via command line. Here is a script to do that.
A couple of notes:
- I use curl instead of the TclCurl package because my system does not come with TclCurl
- The script employs Google Weather API to retrieve weather data. The rest of the script deals with converting that data from XML format to human-readable format.
- I use this script as part of my geek tool output.
Here is a sample output of the script:
$ weather Seattle, WA 98121 2009-05-08 22:15:09 +0000 Condition : Overcast Temperature (F): 61 Humidity : 47% Wind : NE at 2 mph Fri 63 45 Mostly Sunny Sat 68 47 Mostly Sunny Sun 67 49 Mostly Sunny Mon 61 47 Chance of Showers Bothell, WA 98012 2009-05-08 22:18:51 +0000 Condition : Cloudy Temperature (F): 55 Humidity : 58% Wind : N at 2 mph Fri 59 41 Mostly Sunny Sat 63 43 Mostly Sunny Sun 65 45 Mostly Sunny Mon 58 41 Chance of Showers
Here is the script itself:
#!/usr/bin/env tclsh
package require tdom
# Provide the default zip codes of none specified
if {$argc == 0} { set argv {98121 98012} }
foreach location $argv {
set xml [exec curl --silent http://www.google.com/ig/api?weather=$location]
set doc [dom parse $xml]
set root [$doc documentElement]
#
# Show Forecast Information
#
set inf [$doc getElementsByTagName forecast_information]
set data ""
append data "[[$inf selectNodes city] getAttribute data] "
append data "[[$inf selectNodes postal_code] getAttribute data] "
append data "[[$inf selectNodes current_date_time] getAttribute data] "
puts "$data"
#
# Show Current Condition
#
set currentConditions [$doc getElementsByTagName current_conditions]
set labels {
condition "Condition"
temp_f "Temperature (F)"
humidity "Humidity"
wind_condition "Wind"
}
set longest 0
foreach {tag labl} $labels {
set length [string length $labl]
if {$length > $longest} { set longest $length }
}
foreach {tag labl} $labels {
set node [$currentConditions selectNodes $tag]
set data [$node getAttribute data]
regsub {[^:]*: *} $data "" data
puts [format " %-*s: %s" $longest $labl $data]
}
puts ""
# Show Conditions for Next Days
foreach cond [$doc getElementsByTagName forecast_conditions] {
set data ""
append data "[[$cond selectNodes day_of_week] getAttribute data] "
append data "[[$cond selectNodes high] getAttribute data] "
append data "[[$cond selectNodes low] getAttribute data] "
append data "[[$cond selectNodes condition] getAttribute data] "
puts " $data"
}
puts ""
}
Posted on May 8, 2009
0