Weather Bot

By Scakk on Dec 14, 2007

This script will retrieve the weather for US zipcodes from the website www.localweather.net.

Update: Added alias so you can trigger it yourself and message the channel with the response.
Update: Added axtra error checking to the alias ( weatherm ). Thanks to Tokensoup.
Update: Added extra stuff for UV and Preassure. ( help from some one who wishes to remain namelss )
Update 1/13/09: Added alteration to specified line as noted by A Hylian Human.

Big Update 1/13/09: Redid entire script. Added forecast option.

Update 1/16/09: Altered triggers and adjusted a few of the script lines.
Triggers listed below:

Updates above no longer valid for the newest version of script.

Will list either weather or forecast.
!w 12345
!w 12345
!weather 12345
!weather 12345

Will list forecast.
!f 12345
!forecast 12345

Update 27Jan10: Completely redid it using another site as the old one changed way to much to use.

Update 4Feb10: Updated Trigger regex thanks to Jethro_ Also updated the script to show Weather Warnings if there are any.

on $*:text:/^!w(eather)?|f(orecast)?/Si:#: {
  if ($2 isnum) {
    if ($istok(!w !weather,$1,32)) { set %usweatherWF W }
    if ($istok(!f !forecast,$1,32)) { set %usweatherWF F }
    set %usweatherzip $2 | set %usweatherchan $chan
    sockopen usweather www.localweather.net 80
  }
}
on *:sockopen:usweather: {
  sockwrite -n $sockname GET $+(/weather/?pands=,%usweatherzip,&forecast=zandh) HTTP/1.1
  sockwrite -n $sockname Host: www.localweather.net
  sockwrite -n $sockname Connection: close
  sockwrite -n $sockname Authorization: Basic 
  sockwrite -n $sockname $crlf
}
on *:sockread:usweather: {
  sockread %whtemp
  if (warnings.html isin %whtemp) { set %whtempwarn $remove($nohtml(%whtemp),$chr(9),Advisories) }
  if (Location Not Found isin %whtemp) { set %whtempwrong Yes }
  if (class="pname"> isin %whtemp) { set %whtemplocation $remove($nohtml(%whtemp),$chr(9)) }
  ; Weather
  if (Lat: isin %whtemp) { set %whtemplat $remove($nohtml(%whtemp),$chr(9)) }
  if (class="u-met"> isin %whtemp) { inc %t_t 1 | set %whtemperC $+ %t_t $remove($nohtml(%whtemp),$chr(9)) }
  ; Forecast
  if (class="dt"> isin %whtemp) { inc %yy 1 | set %suweatherdate $+ %yy $remove($nohtml(%whtemp),$chr(9)) }
  if (class="hi"> isin %whtemp) { inc %ii 1 | set %suweatherhi $+ %ii $remove($nohtml(%whtemp),$chr(9)) }
  if (class="lo"> isin %whtemp) { inc %iii 1 | set %suweatherlo $+ %iii $remove($nohtml(%whtemp),$chr(9)) }
  if (class="wx"> isin %whtemp) { inc %oo 1 | set %suweathercondition $+ %oo $remove($nohtml(%whtemp),$chr(9)) }
}
on *:sockclose:usweather: { 
  if (%whtempwrong = Yes) { 
    msg %usweatherchan That zip code is incorrect. Please try another one.
    unset %t_t %y_y %whtemp* %usweather* %suweather* %oo %yy %ii
  }
  else {
    msg %usweatherchan Location:12 %whtemplocation
    if (%usweatherWF = W) {
      var %whcurF $gettok(%whtemperC1,1,38) $+ $chr(176) F 
      var %whcurC $round($calc(($gettok(%whtemperC1,1,38) - 32) * 5/9),0) $+ $chr(176) C
      msg %usweatherchan Latitude:03 $removecs($gettok(%whtemplat,4,58),N,E,S,W,Lon) Longitude:03 $removecs($gettok(%whtemplat,5,58),N,E,S,W,Lon)
      msg %usweatherchan Currently:07 %whcurF /07 %whcurC and $gettok(%whtemperC1,2-,67) $&
        Feels Like:7 $replace($gettok($remove(%whtemperC2,Feels Like:),1,70),°,$+($chr(176),$chr(32),F)) $&
        /07 $round($calc($gettok($remove(%whtemperC2,Feels Like:),1,38) -32) *5/9),1) $+ $+($chr(176),$chr(32),C)
      msg %usweatherchan Dew Point:13 $replace($gettok($gettok(%whtemperC3,2,58),1,70),°,$chr(176)) $+ $+($chr(32),F)) $&
        /13 $round($calc($gettok($remove(%whtemperC3,Dew Point:),1,38) -32) *5/9),1) $+ $+($chr(176),$chr(32),C) $&
        Pressure:10 $+($gettok($remove(%whtemperC5,Pressure:),1,110),n) /10 $gettok(%whtemperC5,2,110)
      if (%whtempwarn) { msg %usweatherchan Weather Warning: $+ $+($chr(3),4,%whtempwarn) }
      msg %usweatherchan Website: http://www.localweather.net/weather/?pands= $+ %usweatherzip $+ &forecast=zandh
    }
    if (%usweatherWF = F) {
      msg %usweatherchan 7 $+ $usweather(%suweatherdate2) 4>>12 $iif(%suweatherhi1 = %suweatherlo2,$usweather2(%suweatherlo1),$usweather2($v1)) 4>>>3 %suweathercondition2
      msg %usweatherchan 7 $+ $usweather(%suweatherdate3) 4>>12 $usweather2(%suweatherhi1) 4>>>3 %suweathercondition3 
      msg %usweatherchan 7 $+ $usweather(%suweatherdate4) 4>>12 $usweather2(%suweatherhi2) 4>>>3 %suweathercondition4 
      msg %usweatherchan 7 $+ $usweather(%suweatherdate5) 4>>12 $usweather2(%suweatherhi3) 4>>>3 %suweathercondition5 
      msg %usweatherchan 7 $+ $usweather(%suweatherdate6) 4>>12 $usweather2(%suweatherhi4) 4>>>3 %suweathercondition6 
      msg %usweatherchan 7 $+ $usweather(%suweatherdate7) 4>>12 $usweather2(%suweatherhi5) 4>>>3 %suweathercondition7 
      msg %usweatherchan 7 $+ $usweather(%suweatherdate8) 4>>12 $usweather2(%suweatherhi6) 4>>>3 %suweathercondition8 
      $iif($gettok(%suweatherdate9,2,32) isnum,msg %usweatherchan 7 $+ $usweather(%suweatherdate9) 4>>12 $usweather2(%suweatherhi7) 4>>>3 %suweathercondition9 )
      $iif($gettok(%suweatherdate10,2,32) isnum,msg %usweatherchan 7 $+ $usweather(%suweatherdate10) 4>>12 $usweather2(%suweatherhi8) 4>>>3 %suweathercondition10 )
    }
    unset %t_t %y_y %whtemp* %usweather* %suweather* %oo %yy %ii %iii
  }
}
alias usweather {
  return $iif($istokcs(MON TUE WED THU FRI SAT SUN,$left($1,3),32),$replace($1,$left($1,3),$+($left($1,3),$chr(32))),$+($1,$chr(32)))
}
alias usweather2 {
  return $replacecs($replace($1,:,$+(:,$chr(32)),°,$+($chr(176),$chr(32))),F,$+(F,$chr(32)))
}
alias -l nohtml {
  var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x,&nbsp;)
  return %x
}

Comments

Sign in to comment.
Shadowless   -  Aug 31, 2015

1)
on $:text:/^!w(eather)?|f(orecast)?/Si:#: {
if ($2 isnum) {
if ($istok(!w !weather,$1,32)) { set %usweatherWF W }
if ($istok(!f !forecast,$1,32)) { set %usweatherWF F }
set %usweatherzip $2 | set %usweatherchan $chan
sockopen usweather www.localweather.net 80
}
ignore -u60
!@
}
no flood:)

2) www.localweather.com, not www.localweather.net

3) msg %usweatherchan Latitude:03 $removecs($gettok(%whtemplat,3,58),N,E,S,W,Lon) Longitude:03 $removecs($gettok(%whtemplat,4,58),N,E,S,W,Lon)

 Respond  
chachin   -  Mar 12, 2012
  • /sockopen: 'usweather' socket in use (line 6, weather.mrc)
 Respond  
chachin   -  Mar 12, 2012

hey would it be possible to make it save locations based on usesrs. and could you make it look nice and tidy like this one :)

[12:17:32am] !w
[12:17:35am] <+Amy> Baytown, Texas (29.8°N 95.0°W) - Updated: 00:56 CDT
[12:17:36am] <+Amy> Conditions: Partly Cloudy - Temperature: 17.2°C (63°F) - High/Low: 24/18°C (75/64°F)
[12:17:36am] <+Amy> Dew Point: 17°C (63°F) - Humidity: 95%
[12:17:37am] <+Amy> Wind: SSE at 4.0 MPH (6.4 KPH)

 Respond  
ThE ViPeR   -  Feb 04, 2012

The only reply I get is the location.

 Respond  
ThE ViPeR   -  Feb 04, 2012

No I haven't changed anything in it.

 Respond  
Scakk   -  Feb 04, 2012

I am not having any issues with nor is anyone else where I am that has tried it. Have you changed something in the script?

TheWhistler  -  Nov 21, 2014

00:30] <CrazyFrogOnLilypad> .:( Location: ):.
[00:30] <CrazyFrogOnLilypad> .:( Latitude: Longitude: ):.
[00:30] <CrazyFrogOnLilypad> .:( Currently: ° F / -18° C and ):.
[00:30] <CrazyFrogOnLilypad> .:( Dew Point: F ):.
[00:30] <CrazyFrogOnLilypad> .:( Website: http://www.localweather.net/weather/?pands=31639&forecast=zandh ):.

this weather FILE ISNT WORKING AS YOU CAN SEE ABOVE

Sign in to comment

Pam G.   -  Feb 04, 2012

Oooh ok I hope the script is resolve for you.

 Respond  
ThE ViPeR   -  Feb 03, 2012

Actually the only reply I get is location but not the rest.

 Respond  
Pam G.   -  Feb 03, 2012

Viper it only work when others go to your chat to see their weather. The ops in the chatroom is out of luck seeing their weather on it. If that is what you were saying.

 Respond  
Scakk   -  Feb 03, 2012

@ThE ViPeR - It is working fine for me as I just tested it.

 Respond  
ThE ViPeR   -  Feb 02, 2012

This no longer works properly. Erunners was ok but this one was awesome.

 Respond  
Pam G.   -  Jan 24, 2012

All I know now that the weather code that Erunner did works. But I would like it where I can also type the weather trigger and it lets me in on seeing the weather also. It would be neat if it did something for us too. Well I know it works b/c I had a friend come inside and did the weather thing for hisself and it works.

 Respond  
Pam G.   -  Jan 18, 2012

Can anyone make a weather bot script that will really work I tried all the codes on here for weather bot or script and nothing work.

 Respond  
ryan7461035   -  Apr 11, 2011

This is a good script the only thing thats missing is if someone types !weather country it not work could someone help me with this? edit it tell me what to do?

 Respond  
Jethro   -  Feb 15, 2011

Reborg, what's gonna go wrong with your zip code being seen?

 Respond  
Reborg   -  Feb 15, 2011

Nice work. The only thing wrong with it is that if you don't want anybody seeing your ZIP code then you're dead meat with that code.

 Respond  
ericfx   -  Mar 08, 2010

Excellent work Scakk thank you.

 Respond  
duckz   -  Feb 20, 2010

ahhh maybe thats why I am getting false triggering ... Ill try your version.

edit - that seems to have fixed the issue - THANKS!

 Respond  
Gummo   -  Feb 20, 2010

The following regex

/^!w(eather)?|f(orecast)?/Si

is incorrect. It will trigger when someone simply says "f" instead of "!f".
This is made worse by the fact that the regex isn't locked to even a word break, so any word starting with "f" will set it off...
To fix it easily you can use

/^!(w(eather)?|f(orecast)?)\b/Si

That's why it needs the parentheses around the main regex.

 Respond  
duckz   -  Feb 15, 2010

Ive discovered an issue - not sure if its just my remotes conflicting or if its this scripting - could you test this and verify?

random text is setting the script off and returning > That zip code is incorrect. Please try another one.

Example - type in a random word followed by random numbers. foxtrot 38919230 No trigger or nothing.

 Respond  
Jethro   -  Feb 09, 2010

I think the question has been answered here. duckz asked of it in the forum. I was actually confused. Now I see what he's trying to do.

 Respond  
Scakk   -  Feb 09, 2010

The below examples could work for the normal weather and the forecast version.

For normal weather part.

on *:text:!bhweatherW:#: { 
  $weatherm(90210,W)
}

For the forecast part

on *:text:!bhweatherF:#: { 
 $weatherm(90210,F)
}
 Respond  
duckz   -  Feb 09, 2010

Previously it was .... ; Usage /weatherm 12345 or /weatherm 12345 forecast

That permitted me to do something like this

on *:text:!bhweather:#: {
/weatherm 90210
}

So users never knew how to call zipcodes they only knew how to call the trigger.

How would I structure the trigger coding for what you have given?

Would it be

on *:text:!bhweather:#: {
$weatherm(90210,W)
}

Ohhhhh and sorry to bother ya - how hard would it be to add in "humidity" and "wind" to the script ? I think the old script had those ...

Thanks again!

 Respond  
Scakk   -  Feb 09, 2010

Give this a try.

; Usage $weatherm(ZIPCODE,F) or $weatherm(ZIPCODE,W)
alias weatherm {
  if ($2 == W) { set %usweatherWF W }
  if ($2 == F) { set %usweatherWF F }
  set %usweatherzip $1 | set %usweatherchan $chan
  sockopen usweather www.localweather.net 80
}
 Respond  
duckz   -  Feb 09, 2010

Ok - deleted all my other scripting in remote and got this to work ... now to figure out what was making it not work. :)

Could you please help with the triggering? I want to be able to use a command to call the !w 12345 trigger /weather 12345 would call the trigger?

Thanks.

 Respond  
duckz   -  Feb 04, 2010

So is this working for you?

Also the previous version had some command lines >

; Usage /weatherm 12345 or /weatherm 12345 forecast

Would be great if this one used command lines as well so that I could set up the bot to use an alternate trigger - I dont want 500 people all trying to put in their zip codes ... if they only know 1 trigger
!givemeweather , and that called up the actual trigger it be awesome.

 Respond  
Jethro   -  Feb 04, 2010

I overlooked a matter that the parentheses in the regex match will confuse mirc for $regml(1) when entering the optional command !weather or !forecast
I edited my suggestion to reflect the change below:

on $*:text:/^!w(eather)?\s\d+|f(orecast)?\s\d+/Si:#: {
  if ($istok(!w !weather,$1,32)) { set %usweatherWF W }
  if ($istok(!f !forecast,$1,32)) { set %usweatherWF F }
  set %usweatherzip $2 | set %usweatherchan $chan
  sockopen usweather www.localweather.net 80
}
 Respond  
Jethro   -  Feb 04, 2010

You're welcome, Scakk.

This:> on $*:text:/^!w(eather)?|f(orecast)?/Si:#: {
if ($2 isnum) {
if ($istok(!w !weather,$1,32)) { set %usweatherWF W }
if ($istok(!f !forecast,$1,32)) { set %usweatherWF F }
set %usweatherzip $2 | set %usweatherchan $chan
sockopen usweather www.localweather.net 80
}
}
Can be:

on $*:text:/^!w(eather)?\s(\d+)|f(orecast)?\s(\d+)/Si:#: {
  if ($istok(!w !weather,$1,32)) { set %usweatherWF W }
  if ($istok(!f !forecast,$1,32)) { set %usweatherWF F }
  set %usweatherzip $regml(1) | set %usweatherchan $chan
  sockopen usweather www.localweather.net 80
}

which saves the if $2 isnum statement and an end bracket.

 Respond  
Scakk   -  Feb 04, 2010

Updated with your on text version. Thanks Jethro_. Still working on leanring more and more regex.

 Respond  
Jethro   -  Feb 04, 2010

This regex can be changed:> on $*:text:/^!/Si:#: {To:

on $*:text:/^!w(eather)?|f(orecast)?/Si:#: {

which saves an additional use of the letter w and f. And the [] isn't necessary since you want to match for the exclamation mark only. Also, no $regml(1) is referenced so the outer () can be omitted.

 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.