All pastes #764958 Raw Edit

Stuff

public text v1 · immutable
·published 2007-11-07 18:29 UTC
rendered paste body
####################################################
#                      Rating                      #
####################################################

# 1 : notice $nick
# 2 : privmsg $nick
# 3 : privmsg $channel
set cricketmethod 1

### Events
bind pub - "!rating" rating

package require http

proc rating {nick uhost handle channel text} {
  switch -- [string tolower [lindex [split $text] 0]] {
    odi {
      rating:download $nick $channel odi
    }
    test {
      rating:download $nick $channel test
    }
    default {
      putquick "NOTICE $nick :Commands:"
      putquick "NOTICE $nick :!rating ODI"
      putquick "NOTICE $nick :!rating TEST"
    }
  }
}

proc rating:download {nick chan class} {
  global data
  switch -exact -- $::cricketmethod {
    "1" {set method "notice" ; set who "$nick"}
    "2" {set method "privmsg" ; set who "$nick"}
    "3" {set method "privmsg" ; set who "$chan"}
  }
  set url "http://icc-cricket.yahoo.com/rankings/rankings.html"
  set data [getwebdata $url]
  if {$data==0} {
    putserv "$method $who :There was an error fetching the data. Contact an admin to report this or try again later."
    return
  }
  set foundstart 0
  foreach line [split $data \r\n] {
    if {[string match -nocase "<div id=\"rankings-header\"><a name=\"$class\">*</a></div>" $line]} {
      set word "\037\002[string range $line [expr 40 + [string length $class] -3] [expr [string length $line]-11]]\002\037"
      putserv "$method $who :$word"
      set foundstart 1
      set output ""
    } elseif {$foundstart && [string match -nocase {<div class="rankings_bottom">*</div>*} $line]} {
      # output the date
      set word [string range $line 29 [expr [string length $line]-15]]
      putserv "$method $who :$word"
      return
    } elseif {$foundstart} {
      # firstcol - the ranking
      if {[string match -nocase {<div class="firstcol">*</div>} $line]} {
        set word [string range $line 25 [expr [string length $line]-11]]
        append output "[format %-3s $word]"
      # secondcol = info bold
      } elseif {[string match -nocase {<div class="secondcol"><b>*</b></div>} $line]} {
        set word [string range $line 26 [expr [string length $line]-11]]
        append output "[format %-16s "\002$word\002"]"
      # secondcol = info normal
      } elseif {[string match -nocase {<div class="secondcol">*</div>} $line]} {
        set word [string range $line 23 [expr [string length $line]-7]]
        append output "[format %-15s $word]"
      # output everything
      } elseif {[string match -nocase {</div>} $line]} {
        putserv "$method $who :$output"
        set output ""
      }
    }
  }
}



proc getwebdata {u} {
  ::http::config -useragent Mozilla
  if {[catch {::http::geturl $u -timeout 20000} id]} {
    putlog "\[GetWeb Error\] $u : $id"
    return 0
  } elseif {[::http::status $id]=="timeout"} {
    putlog "\[GetWeb Error\] $u : timeout"
    ::http::cleanup $id
    return 0
  } elseif {[string match -nocase ok [set e [::http::code $id]]]} {
    putlog "\[GetWeb Error\] $u : $e"
    ::http::cleanup $id
    return 0
  } {
    set info [::http::data $id]
    ::http::cleanup $id
    return $info
  }
}