wm withdraw . package require Winico #I need to configure http to use a proxy... package require http ; http::config -proxyhost 172.29.149.142 -proxyport 8080 set target_page "http://www.kexp.org/playlist/playlist.asp" set autoupdate 0 set ico [winico createfrom c:/cup.ico] winico taskbar add $ico -text "Bom dia!" -callback "actualizar %m" proc actualizar {mensagem} { if {$mensagem == "WM_LBUTTONDBLCLK"} { toggle ::autoupdate } if {$mensagem == "WM_LBUTTONDOWN"} { updateNow } if {$mensagem == "WM_RBUTTONDOWN"} { exit } } proc updateNow {} { winico taskbar modify $::ico -text [scrapText] } proc scrapText {} { if { [catch {set html [http::data [http::geturl $::target_page]]} errohttp] } { return "Erro! $errohttp" } else { set linha "[between "class=\"programtime\">" and "</tr>" in $html]" set songinfo [wsplit $linha "<td valign=\"top\">" ] set artista [string trim [between " " and "</td>" in [lindex $songinfo 2]]] #I know I should do something better about replacing html entities... set titulo [string map { ")" ")" "(" "(" "'" "'" } [string trim [string map {"</td>" ""} [lindex $songinfo 4]]] ] set album [string trim [string map {"</td>" ""} [lindex $songinfo 6]]] return "$artista - $titulo" } } proc between { start "and" end "in" text} { set string_start [string first $start $text] set string_end [string first $end $text $string_start] set start_removed [string replace [string range $text $string_start $string_end] 0 [expr [string length $start ]-1] ] return [string replace $start_removed end end ] } proc wsplit {string sep} { set first [string first $sep $string] if {$first == -1} { return [list $string] } else { set l [string length $sep] set left [string range $string 0 [expr {$first-1}]] set right [string range $string [expr {$first+$l}] end] return [concat [list $left] [wsplit $right $sep]] } } proc toggle varName { upvar 1 $varName var set var [expr {$var ? 0 : 1}] } proc every {ms body} {eval $body; after $ms [info level 0]} every 120000 {if {$::autoupdate} { updateNow } }
RS: Note that the wsplit can be done simpler:
- map the separating string to a single char that cannot appear in the string
- split on that single char
proc wsplit {str sep} { split [string map [list $sep \0] $str] \0 } % wsplit This<>is<>a<>test. <> This is a test.