ConcertBot — File Browser

ConcertBotv1.0 / ConcertBotv1.0 / ConcertBot.mrc

ConcertBot.mrc — 5.88 KB — Download this file

; ConcertBot 1.0 written by Produkt (Alex Wasserman) on December 19, 2005.
; Contact me at portbox@gmail.com for any bugs, suggestions, or fixes.

alias concertsocket {
  sockclose concertsock
  write -c concertdata.txt
  sockopen concertsock www.jambase.com 80
  sockmark concertsock $1
}

alias bextract {
  ; This function extracts the text between two keywords in a binvar.
  var %start = $bfind($1, $iif($4, $4, 1), $2)
  var %end = $bfind($1, %start, $3)
  return $bvar($1, %start $+ - $+ %end).text
}

on *:sockopen:concertsock: {
  sockwrite -n $sockname GET $+(/search.asp?zip=, $sock($sockname).mark, &radius=, %radius, &pagenum=0 HTTP/1.0)
  sockwrite -n $sockname $crlf
}

on *:sockread:concertsock: {
  if ($sockerr) {
    %respond %concertnick Sorry, a socket error has occurred.
  }
  else {
    :read
    sockread &temp
    if ($sockbr) {
      bwrite concertdata.txt -1 -1 &temp
      goto read
    }
  }
}

on *:sockclose:concertsock: {
  bread concertdata.txt 0 $file(concertdata.txt).size &temp

  if ($bfind(&temp, 1, We're sorry).text > 0) {
    %respond %concertnick That zipcode is invalid, no city was found for $sock($sockname).mark $+ .
  }
  elseif ($bfind(&temp, 1, No current shows).text > 0) {
    %respond %concertnick No concerts were found in your area.
  }
  else {
    var %i = 0, %j = 1
    var %location = $gettok($bextract(&temp, <title>, </title>), 2, 124)
    %respond %concertnick Loading concerts for %location $+ ...

    while ($bfind(&temp, %i, </a> :: <a href="search.asp?dte=)) {
      var %i = $bfind(&temp, $v1, ?dte=)
      if (%j > %results) { break }

      ; Extract the date.
      var %temp = $bextract(&temp, =, ", %i)
      hadd -m concert date $+ %j $replace($mid(%temp, 2, $calc($mid(%temp, 2, 0) - 1)), % $+ 2F, /)

      ; Extract the day of the week.
      var %i = $bfind(&temp, %i, >)
      hadd concert day $+ %j $right($bextract(&temp, >, y, %i), -1)

      :newband
      ; Extract the name of the band.
      var %i = $bfind(&temp, %i, ?bandID=)
      var %temp = $bextract(&temp, >, <, %i)
      var %bands = %bands $mid(%temp, 2, $calc($mid(%temp, 2, 0) - 1))
      inc %i

      ; Handling multiple bands playing at the same event.
      var %x1 = $bfind(&temp, %i, ?bandID=), %x2 = $bfind(&temp, %i, ?venueID=)

      if (%x1 < %x2) {
        %bands = %bands $+ $chr(44)
        goto newband
      }
      hadd concert band $+ %j %bands
      var %bands = $null

      ; Extract the venue.
      var %i = $bfind(&temp, %i, ?venueID=)
      var %temp = $bextract(&temp, >, <, %i)
      hadd concert venue $+ %j $mid(%temp, 2, $calc($mid(%temp, 2, 0) - 1))

      ; Extract the location of the event.
      var %i = $bfind(&temp, %i, ?city=)
      var %temp = $bextract(&temp, =, &, %i)
      var %city = $mid(%temp, 2, $calc($mid(%temp, 2, 0) - 1))
      var %city = $replace(%city, +, $chr(32), % $+ 2C, $chr(44), % $+ 2E, .)
      var %i = $bfind(&temp, %i, ?stateID=)
      var %temp = $bextract(&temp, >, <, %i)
      hadd concert city $+ %j %city $+ , $mid(%temp, 2, $calc($mid(%temp, 2, 0) - 1))
      inc %j

      ; Handle multiple bands playing on the same day.
      var %x1 = $bfind(&temp, %i, ?bandID=), %x2 = $bfind(&temp, %i, </a> :: <a href="search.asp?dte=)
      if (((%x1 < %x2) || ($calc(%x1 + %x2) == %x1)) && (%j <= %results)) {
        hadd concert date $+ %j $hget(concert, date $+ $calc(%j - 1))
        hadd concert day $+ %j $hget(concert, day $+ $calc(%j - 1))
        goto newband
      }
    }

    ; Handling the page displaying something not anticipated.
    if (%i == 0) {
      %respond %concertnick An unknown error has occurred.
    }
    else {
      var %i = 1

      ; Display the information to the user.
      while (%i <= %results) {

        ; If there are numerous bands playing at a common event (a festival), divide the message into parts.
        if ($len($hget(concert, band $+ %i)) <= 300) {
          %respond %concertnick $chr(91) $+ $hget(concert, date $+ %i) - $hget(concert, day $+ %i) $+ ] $&
            $hget(concert, band $+ %i) - playing at $hget(concert, venue $+ %i) in $hget(concert, city $+ %i) $+ .
        }
        else {
          var %bands = $hget(concert, band $+ %i)
          var %r = $regex(multi, %bands, /^(.{300}[^\x2C]*)\x2C/)
          %respond %concertnick $chr(91) $+ $hget(concert, date $+ %i) - $hget(concert, day $+ %i) $+ ] $&
            $regml(multi, 1) $+ $chr(44)
          var %bands = $right(%bands, - $+ $calc($len($regml(multi, 1)) + 1))
          while ($len(%bands) > 300) {
            var %r = $regex(multi, %bands, /^(.{300}[^\x2C]*)\x2C/)
            %respond %concertnick $regml(multi, 1)
            var %bands = $right(%bands, - $+ $calc($len($regml(multi, 1)) + 1)
          }
          %respond %concertnick %bands - playing at $hget(concert, venue $+ %i) in $hget(concert, city $+ %i) $+ .
          var %bands = $null
        }
        inc %i
      }
      hfree concert
    }
  }
  unset %concertnick %respond %radius %results
}

on *:text:!concert*:*: {

  ; Handling format errors and help commands.
  set %respond $iif($chan, notice, msg)
  if ($2 == help) {
    %respond $nick Format: !concert <zipcode> [radius=50] [results=5]
    %respond $nick Zipcode is required, enter the zipcode of the location you wish to find concerts near. $&
      Radius is how many miles away from the zipcode you wish to search. Results is how many rsults you $&
      want displayed. The default radius is 50 miles and the default number of results displayed is 5. These $&
      two parameters are optional.
  }
  elseif ($remove($2-, $chr(32)) isnum) {
    set %concertnick $nick
    set %radius $iif($3, $3, 50)
    set %results $iif($4 <= 30, $4, 5)
    concertsocket $2
  }
  else {
    %respond $nick That is not a valid format. For help on this command, type: !concert help.
  }
}