Posted to tcl by dkf at Thu May 06 12:37:06 GMT 2010view pretty

package require http
package require tdom

bind PUB - !start startFetchAndPrint
bind PUB - !stop stopFetchAndPrint

proc getCurrentMessage {url} {
    set t [http::geturl $url]
    set doc [dom parse [http::data $t]]
    http::cleanup $t

    set titles {}
    foreach node [$doc selectNodes /channel/item/title] {
        lappend titles [$node text]
    }

    $doc delete
    return [join $titles \n]
}

# The work to do each iteration
proc fetchAndPrint {} {
    global currentMessage currentSchedule targetChannel

    # Reschedule the call to this in 60 seconds, saving the token so we
    # can cancel it later. (That enables !stop)
    set currentSchedule [after 60000 fetchAndPrint]

    # Fetch the current message
    set msg [getCurrentMessage "http://rit.me.uk/livescore/cricrss.php"]

    # Print it (line at a time) if there's been a change
    if {$msg ne $currentMessage} {
	set currentMessage $msg
	foreach line [split $msg \n] {
	    puthelp "PRIVMSG $targetChannel :\00310$line\003"
	}
    }
}

# How to start things
proc startFetchAndPrint {nick uhost hand chan arg} {
    global currentMessage targetChannel

    set currentMessage {}
    set targetChannel $chan
    # This will install the next scheduled callback immediately
    fetchAndPrint
}

# How to stop things
proc stopFetchAndPrint {nick uhost hand chan arg} {
    global currentSchedule
    if {[info exists currentSchedule]} {
	after cancel $currentSchedule
    }
}