Google Weather/Time/Calculation

By ataraxia on Mar 21, 2009

A snippet to retrieve a number of bits of useful information from Google.
Can look up Weather or Time for a location, and can look up results from Google's calculator.
Usage: In a channel where the bot is running...

For weather, write: w [location]
For time, write:
t [location]
For calculation: c 1 + 1
To store your own location for future use:
L location
For time and weather, location is optional if your own location has been previously set.

;;--Simple google interface.
;;--
;;--Performs calculation, time, and weather checks on text triggers.
;;--
;;--Bugs: Google sometimes doesn't provide bits of weather data, in which case the bot will just not retrieve ANY.
;;--
;;--Enjoy. ~ataraxia_

;;-- This section self explanatory.
;;-- Aliases you can call, channel popups.

alias no_wait {

  hadd -m commands except. $+ # true
  msg # * Command flood protection disabled in this channel.

}
alias cmd_wait {

  if ($hget(commands,except. $+ #)) hdel commands except. $+ #
  msg # * Command flood protection enabled in this channel.

}

menu channel {

  Google commands:
  .Disable flood protection:no_wait
  .Enable flood protection:cmd_wait
  .Disable in channel:hadd -m nocommands $chan true
  .Enable in channel:hdel nocommands $chan 

}

;--Main text command.
on *:TEXT:*:*:{

  if ($hget(nocommands,$chan)) halt

  if ($left($1,1) = *) {

    ;--Get trigger.
    var %z $right($1,-1)
    if ($len(%z) > 1) halt

    if (%z = l) {

      if ($2) {
        writeini locations.ini people $nick $regsubex($2-,/([^\d\w])/ig,$chr(37) $+ $base($asc(\1),10,16,2))
        msg # * Stored location for $nick as: $2-
        halt
      }

      else {

        msg # * Currently stored location for $nick is: $regsubex($readini(locations.ini,n,people,$nick),/(\x25[0-9a-f]{2})/ig,$chr($base($remove(\A,$chr(37)),16,10)))
        halt

      }

    }

    ;--Open appropriate socket name.
    if (%z isin twc) {

      ;--Flood protections. (Basic - needs improvement.)
      if (!$hget(commands,except. $+ $chan)) {
        if ($hget(commands,$nick $+ . $+ $chan)) { .raw notice # $nick : $+ System usage disallowed. Try again in $hget(commands,$nick $+ . $+ $chan).unset seconds. | halt }
        hadd -mu300 commands $nick $+ . $+ $chan true
      }

      if (!$2) && (!$readini(locations.ini,n,people,$nick)) { 

        raw notice # $nick : $+ Please input a valid location.
        halt

      }

      var %s g. $+ $replacex(%z,w,weather,t,time,c,calc) $+ . $+ $chan $+ . $+ $ticks
      sockopen %s google.com 80

      ;--Replace any non-alphanumerics with HTTP-compliant hex strings. (%20 etc.)
      sockmark %s $readini(locations.ini,n,people,$nick)
      if ($2) sockmark %s $regsubex($2-,/([^\d\w])/ig,$chr(37) $+ $base($asc(\1),10,16,2))

      .raw notice # $nick : $+ Fetching data for location: $regsubex($sock(%s).mark,/(\x25[0-9a-f]{2})/ig,$chr($base($remove(\1,$chr(37)),16,10))) $+ . This may take a moment..
      hadd -m commands %s $nick

    }

  }
}

on *:sockopen:g.*:{

  if ($sockerr) { 
    msg $gettok($sockname,3,46) * Sorry $hget(commands,$sockname) $+ , there has been a problem communicating with the server. Please try again in a moment.
    halt
  }

  ;--Fetch correct page depending on sockname.
  if ($gettok($sockname,2,46) = weather) sockwrite -tn $sockname GET /search?hl=en&q=Weather%20in%20 $+ $sock($sockname).mark

  if ($gettok($sockname,2,46) = time) sockwrite -tn $sockname GET /search?hl=en&q=Time%20in%20 $+ $sock($sockname).mark

  if ($gettok($sockname,2,46) = calc) sockwrite -tn $sockname GET /search?hl=en&q= $+ $sock($sockname).mark

  ;--Requisite headers. Connection keep-alive to stop Google from killing the socket before mIRC finishes reading.
  sockwrite -tn $sockname HOST: www.google.com
  sockwrite -tn $sockname CONNECTION: Keep-alive
  sockwrite -tn $sockname $str($crlf,2)

}

on *:sockread:g.*:{
  ;--Sockread using a binvar - prevent the socket waiting forever for a CRLF by using -fn 4096
  sockread -fn 4096 &r
  while ($sockbr > 0) {
    var %r $bvar(&r, 1-).text

    ;--Return results.

    if ($window(@gout)) echo @gout %r

    if ($gettok($sockname,2,46) = calc) { 

      if ($regex(%r,/<font size=\+1><b>(.+?)<.b>/ig)) { 
        msg $gettok($sockname,3,46) * Calculation request by $hget(commands,$sockname) $+ : $remove($replace($utfencode($regsubex($regml(1),/(<\/?font.*?>)/ig,$null)),<sup>,^),</sup>)
        sockclose $sockname
      }

    }

    if ($gettok($sockname,2,46) = time) { 

      if ($regex(%r,/<b>Time</b>\sin\s<b>(.+?)</b>(.+?)\x3C/ig)) set -u5 %l. [ $+ [ $sockname ] ] $regml(1) $+ $remove($regml(2),</td>)

      if ($regex(%r,/valign=.+?><b>(.+?)<.b>/ig)) { 
        if (*Web* !iswm $regml(1)) {
          msg $gettok($sockname,3,46) * Time request by $hget(commands,$sockname) for: $utfencode(%l. [ $+ [ $sockname ] ] - $regsubex($regml(1),/(<\/?font.*?>)/ig,$null))
          sockclose $sockname
        }
      }

    }

    if ($gettok($sockname,2,46) = weather) { 

      if ($regex(%r,/>Weather</b>\sfor\s<b>(.+?)</ig)) set -u5 %l. [ $+ [ $sockname ] ] $regml(1)

      if ($regex(%r,/font-size:140%.+>(.+?)</b>.+?Current.+?<b>(.+?)</b>.+?Wind:(.+?)<br>Humidity:(.+?)<.+/ig)) { 
        msg $gettok($sockname,3,46) * Weather request by $hget(commands,$sockname) for: $utfencode(%l. [ $+ [ $sockname ] ] - Current Conditions: $regml(2) / Temperature: $regml(1) / Wind: $regml(3) / Humidity: $regml(4)) 
        sockclose $sockname
      }

    }

    ;--Continue loop reading.

    if ($sock($sockname)) sockread -fn 4096 &r

  }
}

;--Only called if socket closes by itself, not if sockclose is triggered by information found.
on *:sockclose:g.*:msg $gettok($sockname,3,46) * Sorry, $hget(commands,$sockname) $+ , no information was found for your $gettok($sockname,2,46) request.

Comments

Sign in to comment.
Shadowless   -  Aug 31, 2015

dead

 Respond  
Aurora801   -  Nov 16, 2013

[Fri 09:29:28pm] [[device]] .gcalc 2 + 2
[Fri 09:29:28pm] [notice] [cloudIPD]: Fetching data for location: 2 + 2. This may take a moment..
[Fri 09:29:28pm] [[cloudIPD]] * Sorry, [device], no information was found for your calc request.

I modified 3 lines of code, to make it look nicer on my network. I changed noticing to nick only on 2 lines, and changed the text event line so it picked up on gcalc/calc. I'm only using it for calc as I have weather/time scripts.

 Respond  
Jaredmf1012   -  Apr 10, 2010

[05:28] <&[DA]Jared> *t 11111
[05:28] -Igglybuff:#Mt.Silver- [DA]Jared :Fetching data for location: 11111. This may take a moment..

[05:28] -> #Mt * Sorry, [DA]Jared, no information was found for your time request.

Mt No such nick/channel

 Respond  
Pangaea   -  Dec 25, 2009

What is the location format??

<PanAway> *w dublin, ireland -Anubis:#Avalon-Games-Room- PanAway :Fetching data for location: dublin, ireland. This may take a moment..

* Sorry, Pan`Away, no information was found for your weather request. *w dublin -Anubis:#Avalon-Games-Room- Pan`Away :Fetching data for location: dublin. This may take a moment.. * Sorry, Pan`Away, no information was found for your weather request. *w ireland -Anubis:#Avalon-Games-Room- Pan`Away :Fetching data for location: ireland. This may take a moment.. * Sorry, Pan`Away, no information was found for your weather request. ?
 Respond  
err0r007   -  Jul 22, 2009

a few would like you to convert your Fahrenheit temp to Celsius

formula is figure - 32 * 5 / 9

i use $round to make it look better.. nice job nick

 Respond  
texasboy2084   -  Jun 20, 2009

this is a good script, is there a way to add heat index to it?

 Respond  
Calyp5o   -  Jun 11, 2009

Cheers, just added this to my script due to the absence of you.

 Respond  
horstefan   -  Mar 27, 2009

im looking up amsterdam on google.nl (i edited the script to work with that)
i already fixed this by removing the bvar piece of your code.

 Respond  
ataraxia   -  Mar 25, 2009

Wow. How did you manage that? Which location are you looking up weather for? I've never seen nor experienced such before.

 Respond  
horstefan   -  Mar 25, 2009

im getting:

  • Line too long: $bvar (line 121, weather.mrc)
    (like 8 times or something)
 Respond  
ataraxia   -  Mar 23, 2009

There's an option in the popups to disable the flood protection, and you're also able to set the flood time-out to what you wish - simply look in the code for where the comment states the flood protection is, and set the 300 second limit to something smaller. :)

 Respond  
GrimReefer   -  Mar 23, 2009

[13:27] -GrimReefer:#blank- System usage disallowed. Try again in 274 seconds.

i guess you can only use this script every 5 mins.. i get the same for all 3 commands.

other than that works just as you say.. ty

 Respond  
Aucun50   -  Mar 21, 2009

Looks neat

 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.