This snippet looks up the current and next day weather for any area you search. It uses yahoo's WOEID (where on earth ID) to find the location. So you can search for a city and it will attempt to choose the most likely. WOEID is world wide and should display results accordingly.
If it finds c/celsius or f/fahrenheit in the search pattern, it will display the results in those units. It defaults to fahrenheit.
Some searches include:
!Weather philly
!w paris
!w LAX
!w JFK
!w 90210
!w berlin celcius
At the bottom of the snippet I have included the variables you can use to customize the displayed output.
on *:SOCKOPEN:YahooWeather:{
tokenize 32 $sock($sockname).mark
if ($sockerr) { $1 $false | halt }
sockwrite -n $sockname GET /forecastrss?w= $+ $2 $+ $iif($3,&u= $+ $3) HTTP/1.1
sockwrite -n $sockname Host: weather.yahooapis.com
sockwrite -n $sockname Connection: Close
sockwrite -n $sockname $crlf
}
on *:SOCKREAD:YahooWeather:{
tokenize 32 $sock($sockname).mark
if ($sockerr) { $1 $false | halt }
var %read
sockread %read
%read = $regsubex(%read,/ +/g,$chr(32))
if ($regex(%read,/<title>City not found<\/title>/i)) {
$1 $false
sockclose YahooWeather
halt
}
if ($regex(%read,/<yweather:(\S+)(.*)\/>/)) {
var %prefix = $iif($regml(1) == forecast,$iif($hget(Weather,today_day),tomorrow,today),$regml(1))
noop $regex($regml(2),/\b(.*?)="(.*?)"/g))
var %num_match = $regml(0)
while (%num_match) {
wadd $+(%prefix,_,$regml($calc(%num_match - 1))) $regml(%num_match)
dec %num_match 2
}
}
elseif ($regex(%read,/<title>(.*?)<\/title>/i)) {
wadd title $regml(1)
}
elseif (%read == </rss>) {
$1 $true
sockclose $sockname
halt
}
}
on *:SOCKOPEN:WOEID:{
tokenize 32 $sock($sockname).mark
if ($sockerror) { $1 $false | halt }
sockwrite -n $sockname GET /v1/places.q(' $+ $urlencode($2-) $+ ')?appid= $+ $Y_APPID HTTP/1.1
sockwrite -n $sockname Host: where.yahooapis.com
sockwrite -n $sockname Connection: Close
sockwrite -n $sockname $crlf
}
on *:SOCKREAD:WOEID:{
tokenize 32 $sock($sockname).mark
if ($sockerror) { $1 $false | halt }
var %read
sockread %read
If ($regex(%read,/yahoo:total="0"\/>/)) { $1 $false | halt }
Elseif ($regex(%read,/<woeid>(.*)<\/woeid>/)) {
$1 $regml(1)
sockclose $sockname
halt
}
}
on *:text:*:*:{
if ($regex($1-,/^[!.@&]w(?:eather)? (.*)$/i)) {
if (%weather.flood == 3) {
msg # Flood Protection Activated.
inc -u10 %weather.flood 1
}
Elseif (%weather.flood > 3) {
return
}
elseif (%weather.channel) {
msg # Another weather lookup is already in progress, please try again momentarly
inc -u5 %weather.flood 1
}
Else {
getWOEID UpdateWeather $regsubex($regml(1),/\b(c(elcius)?|f(arenheit)?)\b/i,)
set %weather.u $lower($left($regml(1),1))
set %weather.channel #
inc -u5 %weather.flood 1
}
}
}
alias weatherReplace { return $regsubex($1,/&(\S+?)&/g,$hget(Weather,\1)) }
alias -l wADD { hadd -m Weather $1- }
alias -l Y_APPID { return L8j7ok7V34GTI61wyDk20Uy5PazMYrKUEAHruXNndmOzvkOHgQfVlOz6Az8R9Ib_1A-- }
alias urlEncode { return $regsubex($$1,/([\W\D])/g,$+(%,$base($asc(\t),10,16))) }
alias GetWeather {
if ($regex($1-,/^\S+ \S+( [fc])?$/i)) {
if $hget(Weather) { hfree Weather }
if ($sock(YahooWeather)) { sockclose YahooWeather }
sockopen YahooWeather weather.yahooapis.com 80
sockmark YahooWeather $1-
}
}
alias GetWOEID {
if ($sock(GetWOEID)) { sockclose GetWOEID }
if ($2) {
sockopen WOEID where.yahooapis.com 80
sockmark WOEID $1-
}
}
alias UpdateWeather {
if ($1) {
getWeather DisplayWeather $1 %weather.u
}
else {
msg %weather.channel Unable to find location, please try again!
unset %weather.*
}
}
alias DisplayWeather {
if ($1) {
msg %weather.channel $weatherreplace(&title&)
var %x = Today, High: &today_high&&units_temperature& Low: &today_low&&units_temperature& Cur: &condition_text& &condition_temp&&units_temperature&
.timer 1 1 msg %weather.channel $weatherreplace(%x)
var %x = Tomorrow, High: &tomorrow_high&&units_temperature& Low: &tomorrow_low&&units_temperature& &tomorrow_text&
.timer 1 2 msg %weather.channel $weatherreplace(%x)
}
else {
msg %weather.channel Unable to find weather report, please try a different location.
}
unset %weather.*
}
; ############################################
; ## DISPLAY VARIABLES and Examples ##
; ############################################
/*
&astronomy_sunrise& = 6:46 am
&astronomy_sunset& = 6:40 pm
&atmosphere_humidity& = 54
&atmosphere_pressure& = 982.05
&atmosphere_rising& = 0 ;// 0 is steady, 1 is rising, 2 is falling
&atmosphere_visibility& = 16.09
&condition_code& = 28
&condition_date& = Wed, 29 Sep 2010 11:53 am PDT ;// Last time weather was update
&condition_temp& = 27
&condition_text& = Mostly Cloudy
&location_city& = Beverly Hills
&location_country& = United States
&location_region& = CA
&title& = Conditions for Beverly Hills, CA at 11:53 am PDT
&today_code& = 30
&today_date& = 29 Sep 2010
&today_day& = Wed
&today_high& = 30
&today_low& = 18
&today_text& = Partly Cloudy
&tomorrow_code& = 30
&tomorrow_date& = 30 Sep 2010
&tomorrow_day& = Thu
&tomorrow_high& = 28
&tomorrow_low& = 18
&tomorrow_text& = Partly Cloudy
&units_distance& = km
&units_pressure& = mb
&units_speed& = km/h
&units_temperature& = C
&wind_chill& = 27
&wind_direction& = 210 ;// 0-359, 0 is north, 180 is south
&wind_speed& = 9.66
*/