2 Day Weather

By TheImrac on Sep 29, 2010

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
*/

Comments

Sign in to comment.
Hornet1962   -  Feb 13, 2016

I get the following results:

[21:32] !weather 27932
[21:32] ConditionsforEdenton,NCat9:14pmEST
[21:32] Today, High: Low: Cur:
[21:32] Tomorrow, High: Low:

Is there something I can change to get the proper results?

 Respond  
Mrmot   -  Dec 01, 2014

How to change defaults to celsius?

 Respond  
jasonh   -  Oct 31, 2011

works with airport codes that wunderground scripts dont work with

 Respond  
Stewie1k94   -  Mar 24, 2011

great works and thanks for sharing it its quite helpful cause i dont like watching weather on tv lol

 Respond  
Callumlord   -  Feb 23, 2011

Great script :))

 Respond  
pacino23   -  Oct 12, 2010

This script works great on my bot (mIRC 6.35) with no problems so far. Thanks for sharing it, will vote once I have been a member 3 for days.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.