proc gettitles {tvkanal url} {
global tv tvdb
set token [http::geturl $url]
upvar #0 $token state
set lines 0
foreach line [split $state(body) "\n"] {
incr lines
if {[string match "*<span class='udsendelse-symboler'>*" $line]} {
set line [string range $line 0 [string first "<span class='udsendelse-symboler'>" $line]]
}
if {[regexp $tv(regex) $line garbage time title]} {
if {[string match -nocase "*</span>*" $title]} {
set title [string range $title 0 [expr [string first "</span>" $title] - 1]]
}
#$time should be converted to the min of day
set time [hhmm2minofday $time]
tv:add $tvkanal $time $title
}
}
if {$lines < 15} {
timer 1 "gettitles $tvkanal $url"
putserv "PRIVMSG #dktv :Update of $tvkanal failed, retrying in 60secs"
putlog "Update of $tvkanal failed, retrying in 60secs"
} else {
putlog "Updated $tvkanal"
putserv "PRIVMSG #dktv :Updated $tvkanal"
}
}
package require http
#tvrip2.tcl version 2.10 Released 15-08-2003
set tv(lastid) 0
set tv(regex) "<div><span class='.*'>(.*)</span> <span class='.*' onclick='.*'>(.*)</span>.*"
setudef flag tvrip
proc tv:add {station time title} {
global tv tvdb
set id [incr tv(lastid)]
set tvdb($id,station) $station
set tvdb($id,time) $time
set tvdb($id,title) $title
return $id
}
bind pub -|- !rehash pub:update:list
proc pub:update:list {nick uhost handle chan text} {
putserv "PRIVMSG $chan :Updateing lists"
tv:update:list
}
proc tv:update:list {} {
global tvrip tvdb
catch { unset tvdb }
set tvrip(db) ""
set kanalnavne ""TV2" "TV2ZULU" "DR1" "DR2" "TV3+" "TV3" "TVDK1" "TVDK2""
set i 0
foreach kanal $kanalnavne {
incr i
set url "http://tv.tv2.dk/tv/listning.php?sektion&Soegeord=&Kategori=0&Periode=-1&KanalID=$i&Region=0&dato=3"
utimer [expr $i * 5] "gettitles [join [lindex $kanalnavne [expr $i - 1]]] $url"
}
}
proc getnow {} {
#Return min of day
set usecs [clock seconds]
set h [clock format $usecs -format "%H"]
set m [clock format $usecs -format "%M"]
if {[string index $h 0]==0} { set h [string index $h 1] }
# set h [expr $h + 6] ; #To compensate for timezone diff.
if {$h > 23} { set h [expr $h - 24] }
if {[string index $m 0]==0} { set m [string index $m 1] }
set res [expr $h * 60]
set res [expr $res + $m]
if {$res > 1400} { set res [expr $res - 1400] }
return $res
}
proc hhmm2minofday {time} {
set hh [join [lindex [split $time ":"] 0]]
set min [join [lindex [split $time ":"] 1]]
if {[string index $hh 0]==0} { set hh [string index $hh 1] }
if {[string index $min 0]==0} { set min [string index $min 1] }
set res [expr $hh * 60]
set res [expr $res + $min]
return $res
}
proc gettitles {tvkanal url} {
global tv tvdb
set token [http::geturl $url]
upvar #0 $token state
set lines 0
foreach line [split $state(body) "\n"] {
incr lines
if {[string match "*<span class='udsendelse-symboler'>*" $line]} {
set line [string range $line 0 [string first "<span class='udsendelse-symboler'>" $line]]
}
if {[regexp $tv(regex) $line garbage time title]} {
if {[string match -nocase "*</span>*" $title]} {
set title [string range $title 0 [expr [string first "</span>" $title] - 1]]
}
#$time should be converted to the min of day
set time [hhmm2minofday $time]
tv:add $tvkanal $time $title
}
}
if {$lines < 15} {
timer 1 "gettitles $tvkanal $url"
putserv "RIVMSG #dktv :Update of $tvkanal failed, retrying in 60secs"
putlog "Update of $tvkanal failed, retrying in 60secs"
} else {
putlog "Updated $tvkanal"
putserv "PRIVMSG #dktv :Updated $tvkanal"
}
}
proc nowplus {min} {
set next [expr $min + [getnow]]
if {$next > 1440} { set next [expr $next - 1440] }
return $next
}
proc notifychan {s t} {
foreach chan [channels] {
if {[lsearch [channel info $chan] +tvrip]>-1 && [lsearch [channel info $chan] -inactive]>-1} {
putserv "PRIVMSG $chan :\[$s\] $t"
}
}
}
bind time - "* * * * *" tv:db:warn
proc tv:db:warn {min hour day month year} {
global tvdb
if {[getnow]==304} { tv:update:list } ; #tv2.dk resets at 300
# set warn [nowplus 5]
set warn [getnow]
putloglev 5 * "Looking for programs with time $warn"
set size [array size tvdb]
for {set i 0} {$i<$size} {incr i} {
set col [array get tvdb $i,*]
if {$col != ""} {
set time [tvdb:getfield $i time]
putloglev 4 * "$time == $warn"
if {$time == $warn} {
set title [tvdb:getfield $i title]
set station [tvdb:getfield $i station]
notifychan $station $title
}
}
}
}
proc tvdb:getfield {id field} {
global tvdb
return [join [lindex [array get tvdb $id,$field] 1]]
}
proc humantime {time} {
set hour 0
set min 0
while {$time>59} {
set time [expr $time - 60]
incr hour
}
set min $time
if {$hour<9} { set hour "0$hour" }
if {$min<9} { set min "0$min" }
return $hour:$min
}
proc islater {now time} {
#return 1 if time is later then now
if {$now < 300} {
#After midnight and before update
if {$time > $now && $time < 300} { return 1 }
return 0
}
if {$now == 300} { return 0 }
if {$now > 300} {
#After update time
if {$time > $now} { return 1 }
if {$time < 300} { return 1 }
return 0
}
}
#!tv <kanal>
#!tvall
#!search
bind pub -|- !tv pub:tv
proc pub:tv {nick uhost handle chan text} {
global tvdb
set kanal [join [lindex $text 0]]
#Find next 5 programs on kanal
set size [array size tvdb]
set now [getnow]
set found 0
for {set i 0} {$i<$size} {incr i} {
set col [array get tvdb $i,*]
if {$col != ""} {
set time [tvdb:getfield $i time]
set station [tvdb:getfield $i station]
if {[string match -nocase $station $kanal]} {
if {[islater $now $time]} {
incr found
set title [tvdb:getfield $i title]
set htime [humantime $time]
putserv "PRIVMSG $nick :$htime $title"
if {$found==5} { break }
}
}
}
}
}
bind pub -|- !tvall pub:tvall
proc pub:tvall {nick uhost handle chan text} {
global tvdb
#Find next program for all channels.
set size [array size tvdb]
set now [getnow]
set found ""
for {set i 0} {$i<$size} {incr i} {
set col [array get tvdb $i,*]
if {$col != ""} {
set time [tvdb:getfield $i time]
set station [tvdb:getfield $i station]
if {[lsearch $found $station]!=-1} {
if {[islater $now $time]} {
lappend found $station
set title [tvdb:getfield $i title]
set htime [humantime $time]
putserv "PRIVMSG $nick :$htime \[$station\] $title"
putloglev 8 * "Found=[llength $found]"
if {[llength $found]==8} { break }
}
}
}
}
}
bind pub -|- !search pub:search
proc pub:search {nick uhost handle chan text} {
global tvdb
set program [join [lindex $text 0]]
#Find all future programs containing program (max 10)
set size [array size tvdb]
set now [getnow]
set found 0
for {set i 0} {$i<$size} {incr i} {
set col [array get tvdb $i,*]
if {$col != ""} {
set time [tvdb:getfield $i time]
set title [tvdb:getfield $i title]
if {[string match -nocase "*$program*" $title]} {
if {[islater $now $time]} {
incr found
set station [tvdb:getfield $i station]
set htime [humantime $time]
putserv "PRIVMSG $nick :$htime \[$station\] $title"
if {[llength $found]==10} { break }
}
}
}
}
if {$found==0} { putserv "PRIVMSG $nick :Sorry, no matches" }
}
#TODO : pub triggers /msg triggers
if {![info exists tv(started)]} {
utimer 20 tv:update:list
set tv(started) 1
}