# Grid module for TiVoWeb.
# Shows an what's on in a grid
#
# Based very heavily on LJ's whatson module
# http://www.ljs.nildram.co.uk/tivo.html
#
# 20030129 20:00 v0.90
# 20030131 18:00 Modded by Alex Vallat (AlexV@ComPorts.com)
# 20030131 23:00 v0.95 Changed time headers to take into account timezone
proc action_grid {chan path eenv} {
global channeltablestation channeltablenum
global logotablename logotableindex
global db
global guideindexdir
global tzoffset
global hrsel chnsel wodatetz wotimetz env chnvals chnlabels datevals datelabels timevals timelabels
global minday maxday
global reccount
global usformat
if { $env(TV_STD) != "PAL"} {
set usformat 1
} else {
set usformat 0
}
set chnsel "2"
set hrsel 3
set wodatetz [expr ([clock seconds] + $tzoffset) - (([clock seconds] + $tzoffset) % 86400)]
set totalseconds [expr (([clock seconds] + $tzoffset - $wodatetz) )]
set wotimetz [expr ($totalseconds / 3600 )]
set hourseconds [expr (3600-($totalseconds - ($wotimetz * 3600)))]
if { $hourseconds < 300 } {
# min 5 mins to change
set hourseconds 300
}
eval $eenv
puts $chan [html_start "What's On Grid"]
puts $chan ""
if { $eenv == "" } {
set timevals ""
set datevals ""
set datelabels ""
set timelabels ""
set chnvals "3 2"
if { $usformat } {
set chnlabels "{Favorite Channels} {Channels Watched}"
} else {
set chnlabels "{Favourite Channels} {Channels Watched}"
}
for {set i 1} {$i < 24} {incr i 1} {
append timevals "$i "
if {$i == 1} {
set hour "Hour"
} else {
set hour "Hours"
}
append timelabels "{$i $hour} "
}
set minday "99999"
set maxday "00000"
set ictr 0
ForeachMfsFile fsid name type "/Schedule" "" {
scan $name "%d:%d:" schedsid schedday
if {$minday > $schedday} {
set minday $schedday
}
if {$maxday < $schedday} {
set maxday $schedday
}
incr ictr
if { $ictr > 500 } {
break
}
}
set minseconds [expr $minday * 86400]
set maxseconds [expr $maxday * 86400]
for {set i $minseconds} {$i <= $maxseconds} {incr i 86400} {
if { $usformat } {
set timestr [clock format $i -format "%a %1m/%1d"]
} else {
set timestr [clock format $i -format "%a %e[switch -glob [clock format $i -format "%e"] {1? {format th} *1 {format st} *2 {format nd} *3 {format rd} default {format th} }] %b"]
}
append datevals "$i "
append datelabels "\{$timestr\} "
}
} else {
if { $submit == "Watch" } {
if { [string length $channel] == 0 } {
SendKey "livetv"
} else {
SendKey "guide"
after 2000
SendKey "select"
for {set i 0} {$i < [string length $channel]} {incr i 1} {
after 200
SendKey "num[ string index $channel $i]"
}
after 200
SendKey "enter"
}
}
}
puts $chan [html_form_start "GET" "/grid" "name=\"search\""]
puts $chan [html_table_start "" "" ""]
puts $chan "
"
puts $chan [td "" [html_form_select "chnsel" $chnvals $chnlabels $chnsel "onChange=\"searchhelp(this.options.selectedIndex)\""]]
puts $chan [td "" [html_form_select "hrsel" $timevals $timelabels $hrsel "onChange=\"searchhelp(this.options.selectedIndex)\""]]
puts $chan [td "" [html_form_input "submit" "submit" "Display"]]
if { $usformat } {
set me ""
} else {
set me "me"
}
puts $chan [html_form_end]
set chkdate [expr $wodatetz / 86400]
if { $chkdate > $minday } {
puts -nonewline $chan [td "" "[html_link "/grid?hrsel=$hrsel&chnsel=$chnsel&wodatetz=[expr $wodatetz - 86400]&wotimetz=$wotimetz&submit=Search" "<< - 1 day"]" ]
}
if { $chkdate > $minday || $wotimetz > 0 } {
if { $wotimetz == 0 } {
set datem1hrtz [expr $wodatetz - 86400]
set timem1hrtz 23
} else {
set datem1hrtz $wodatetz
set timem1hrtz [expr $wotimetz - 1]
}
puts -nonewline $chan [td "" " [html_link "/grid?hrsel=$hrsel&chnsel=$chnsel&wodatetz=$datem1hrtz&wotimetz=$timem1hrtz&submit=Search" "< - 1 hour"]" ]
}
if { $chkdate < $maxday || $wotimetz < 23 } {
if { $wotimetz == 23 } {
set datep1hrtz [expr $wodatetz + 86400]
set timep1hrtz 0
} else {
set datep1hrtz $wodatetz
set timep1hrtz [expr $wotimetz + 1]
}
puts -nonewline $chan [td "" " [html_link "/grid?hrsel=$hrsel&chnsel=$chnsel&wodatetz=$datep1hrtz&wotimetz=$timep1hrtz&submit=Search" "+ 1 hour >"]" ]
}
if { $chkdate < $maxday } {
puts $chan [td "" " [html_link "/grid?hrsel=$hrsel&chnsel=$chnsel&wodatetz=[expr $wodatetz + 86400]&wotimetz=$wotimetz&submit=Search" "+ 1 day >>"]" ]
}
puts $chan "
"
puts $chan [html_table_end]
set watchLink "/grid?hrsel=$hrsel&chnsel=$chnsel&wodatetz=$wodatetz&wotimetz=$wotimetz&submit=Watch"
set minseconds [expr $wodatetz + ($wotimetz * 3600) - $tzoffset]
set maxseconds [expr $minseconds + (3600 * $hrsel)]
set hr [expr $wotimetz - ($tzoffset / 3600)]
if { $hr < 0 } {
incr chkdate -1
} elseif { $hr > 23 } {
incr chkdate 1
}
set columnWidth 40
set tableWidth [expr (((12*$hrsel)+1)*$columnWidth)]
puts $chan [html_table_start "width=$tableWidth" "" ""]
puts $chan ""
set hourheader ""
for {set x 0} {$x < $hrsel} {incr x 1} {
set hrinc [expr ($wotimetz + $x) % 24]
append hourheader [th "class=GridTime width=$columnWidth cellpadding=0" "$hrinc:00"] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "class=GridTime width=$columnWidth" "$hrinc:30"] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "] [th "width=$columnWidth" " "]
}
puts $chan [tr "align=left width=$columnWidth" [th "Channel"] $hourheader ]
puts $chan ""
set reccount 0
foreach channum [lsort -integer [array names channeltablenum]] {
set stationfsid $channeltablenum($channum)
set data $channeltablestation($stationfsid)
set favorite [lindex $data 1]
if { $chnsel == "3" && $favorite == 0 } {
continue
}
set callsign [lindex $data 2]
set filler 1
puts $chan " "
puts $chan [td "width=$columnWidth class=Channel" [html_link "$watchLink&channel=$channum" "$callsign" "class=Channel"]]
if { [expr ($wotimetz - ($tzoffset / 3600) + 24) % 24] < 5 } {
incr chkdate -1
ForeachMfsFile fsid name type "/Schedule" "$stationfsid:$chkdate" {
if {[catch [sue_wochandata $chan $fsid $minseconds $maxseconds $callsign $stationfsid $filler $columnWidth] error]} {
puts "sue_wochandata $fsid $minseconds $callsign $stationfsid"
puts "$error"
}
}
set filler 0
incr chkdate 1
}
ForeachMfsFile fsid name type "/Schedule" "$stationfsid:$chkdate" {
if {[catch [sue_wochandata $chan $fsid $minseconds $maxseconds $callsign $stationfsid $filler $columnWidth] error]} {
puts "sue_wochandata $fsid $minseconds $callsign $stationfsid"
puts "$error"
}
}
incr chkdate 1
set filler 0
ForeachMfsFile fsid name type "/Schedule" "$stationfsid:$chkdate" {
if {[catch [sue_wochandata $chan $fsid $minseconds $maxseconds $callsign $stationfsid $filler $columnWidth] error]} {
puts "sue_wochandata $fsid $minseconds $callsign $stationfsid"
puts "$error"
}
}
incr chkdate -1
puts $chan "
"
}
puts -nonewline $chan [html_table_end]
puts $chan [html_end]
}
proc sue_wochandata {chan fsid startsecs endsecs callsign stationfsid filler columnWidth} {
global db
global tzoffset
global usformat
global images
global tvratingnums tvratingvals
global advisorynums advisoryvals
global mpaaratingnums mpaaratingvals
global reccount
RetryTransaction {
set stationday [db $db openid $fsid]
set showings [dbobj $stationday gettarget Showing]
}
set cells ""
for {set x 0} {$x < [llength $showings]} {incr x 20} {
RetryTransaction {
foreach showingfsid [lrange $showings $x [expr $x+19]] {
regexp {([0-9]*)/(.*)} $showingfsid junk showingobjid subobjid
set showing [db $db openidconstruction $showingobjid $subobjid]
set date [dbobj $showing get Date]
set prgtime [dbobj $showing get Time]
set duration [dbobj $showing get Duration]
set secs [expr $date * 86400 + $prgtime]
if { $secs >= $endsecs } {
continue
}
set finsecs [expr $secs + [dbobj $showing get Duration]]
if { $finsecs <= $startsecs } {
continue
}
set program [dbobj $showing get Program]
set title [strim [dbobj $program get Title]]
if { $usformat } {
set timestrtz [clock format [expr $secs + $tzoffset] -format "%l:%M %P"]
set timeendtz [clock format [expr $finsecs + $tzoffset] -format "%l:%M %P"]
} else {
set timestrtz [clock format [expr $secs + $tzoffset] -format "%H:%M"]
set timeendtz [clock format [expr $finsecs + $tzoffset] -format "%H:%M"]
}
set partindex [dbobj $showing get PartIndex]
set partcount [dbobj $showing get PartCount]
set series [dbobj $program get Series]
set seriesfsid ""
if { $series != "" } {
set seriesfsid [dbobj $series fsid]
set episodic [defaultval 1 [dbobj $series get Episodic]]
} else {
set episodic 1
}
set description [strim [dbobj $program get Description]]
set eptitle [strim [dbobj $program get EpisodeTitle]]
if { $partcount != "" && $partindex != "" } {
append eptitle " ($partindex/$partcount)"
}
if { $::description_hover && $description != "" } {
set description [htmlEncode $description]
regsub -all {\"} $description {\"} description
set attrs "title=\"$description\""
} else {
set attrs ""
}
if { $description == "" } {
set description " "
}
set imagenum -1
set today [expr [clock seconds] / 86400]
if { $date >= $today } {
set recfsid [lindex [get_fsidbyprefix "/Recording/Active" "4:$date:[string range "0000$prgtime" [expr [string length "$prgtime"] - 1] end]:"] 0]
} else {
set recfsid ""
}
if { $recfsid == "" && $date <= $today } {
if { $prgtime == 0 } {
incr date -1
set prgtime 86398
} else {
incr prgtime -2
}
set prgtime "[string range "0000$prgtime" [expr [string length "$prgtime"] - 1] end]"
set recfsid [lindex [get_fsidbyprefix "/Recording/Active" "1:$date:$prgtime:"] 0]
}
if { $recfsid == "" && $date <= $today } {
set recfsid [lindex [get_fsidbyprefix "/Recording/Active" "3:$date:$prgtime:"] 0]
}
if {$recfsid != ""} {
set rec [db $db openid $recfsid]
set recshowing [dbobj $rec get Showing]
set recstation [dbobj $recshowing get Station]
set recstationfsid [dbobj $recstation fsid]
if {$recstationfsid == $stationfsid} {
set state [dbobj $rec get State]
if {$state == 4} {
set imagenum 3
} elseif {$state == 5} {
set imagenum 20
} elseif {$state == 3} {
set imagenum 8
incr reccount 1
} elseif {$state == 6} {
if {$::version3} {
set recbeh [dbobj $rec get RecordingBehavior]
set seltype [dbobj $recbeh get PresentationBehavior]
if {$seltype == 6} {
set imagenum 10
incr reccount 1
} elseif {$seltype == 2} {
set imagenum 11
incr reccount 1
} elseif {$seltype == 3} {
set imagenum 13
incr reccount 1
} else {
set imagenum 1
incr reccount 1
}
} else {
set seltype [dbobj $rec get SelectionType]
if {$seltype == 6} {
set imagenum 10
incr reccount 1
} elseif {$seltype == 9} {
set imagenum 11
incr reccount 1
} elseif {$seltype == 13} {
set imagenum 13
incr reccount 1
} else {
set imagenum 1
incr reccount 1
}
}
} else {
set expdate [dbobj $rec get ExpirationDate]
set expsecs [expr $expdate * 86400 + [dbobj $rec get ExpirationTime]]
set nowsecs [clock seconds]
if {$expdate == 24855} {
set imagenum 0
} elseif {$expsecs < $nowsecs} {
set imagenum 5
} elseif {$expsecs < [expr $nowsecs + 24*60*60]} {
set imagenum 3
}
}
}
}
if {$imagenum >= 0} {
set imagef [lindex $images $imagenum]
if { $imagenum == 20 } {
set imageh [html_link "/confirmdelete/3/$recfsid" "[img "align=absmiddle" $imagef]" "title=\"Click to undelete\""]
} else {
set imageh [img "align=absmiddle" $imagef]
}
} else {
set imageh ""
}
set thumbs ""
if { $series != "" } {
set score [dbobj $series get ThumbData]
if { $score != "" } {
set imagenumt [get_thumbimage $score]
if { $imagenumt >= 0} {
set imagef [lindex $images $imagenumt]
set thumbs [img "align=absmiddle alt=\"\"" $imagef]
}
}
}
if { $cells == "" } {
if { ($secs > $startsecs) } {
if ($filler) {
set spans [expr ($secs - $startsecs) / 300]
append cells [td "colspan=$spans" " $spans Filler"]
set filler 0
}
} else {
set duration [expr $duration + ($secs - $startsecs)]
}
}
if { $finsecs > $endsecs } {
set duration [expr $duration - ($finsecs - $endsecs)]
}
set spans [expr $duration / 300]
append title " $eptitle"
set title [string range $title 0 [expr 8 * $spans]]
if { ($seriesfsid != "") && ($title != "SIGN OFF ") } {
set title [html_link "/series/$seriesfsid" $title "$attrs class=GridShow"]
}
set displayText ""
if { $title == "SIGN OFF " } {
set displayText ""
} else {
set displayText [html_link "/showing/$showingfsid" "$imageh $timestrtz $title " "class=GridTime"]
}
set colWidth [expr $columnWidth * $spans]
set tdClass "GridShow"
if { $displayText == "" } {
set tdClass "GridBlank"
}
append cells [td "class=$tdClass width=$colWidth colspan=$spans " $displayText]
}
}
}
puts $chan $cells
}
##########################################
register_module "grid" "Grid" "TV Grid"