#!/usr/bin/perl5.00404

package MyParse;
use HTML::Parser;

use strict;
use vars qw(@ISA);

@ISA=qw(HTML::Parser);

my (%show,$beginrow,%channel);

sub start {
    my ($self, $tag, $attr, $attrseq, $origtext)=@_;

    if ($tag eq "td") {
	$show{intd}=1;
	if ($beginrow) { $show{channel}=1; }
	$beginrow=0;
	foreach (keys %{$attr}) { $show{$_}=$attr->{$_}; }
    }

    if ($tag eq "tr") {
	$beginrow=1;
    }
    
    if ($tag eq "a") {
         ($show{id})=($attr->{href} =~ /\((\d+)\)/);
    }
}

sub text {
    my ($self, $text)=@_;
    if ($show{intd}) {
	$show{text}=$text;
	if ($show{channel}) {
	    $channel{name}=$text;
	}

    }
}
sub end {
    my ($self, $tag, $origtext)=@_;

    if ($tag eq "td") {
        $show{starttime}=$channel{starttime};
	$show{endtime} = $show{starttime} + $show{colspan};
        if ($channel{name} eq $show{text}) {
	   $channel{number}=$show{id};
	} else {
	   print "$channel{number} $channel{name}: $show{text} ";
	   print "($::times[$show{starttime}] - $::times[$show{endtime}]) ID $show{id}\n";
	}
        $channel{starttime} += $show{colspan};	
	undef %show;
    }    
    
    if ($tag eq "tr") {
        undef %channel;
    }
}

package main;

use vars qw(@times);
use HTTP::Request;
use LWP::UserAgent;

$|=1;

my ($sec,$min,$hour) = localtime(time);
if ($min < 57)  { $min = 0;  } else { $min=0; $hour++; }
push @times,sprintf("$hour:%2.2d",$min);

for (1..6) {
    if ($min == 30) { $hour++; $min = 0; } else { $min = 30; }
    if ($hour > 23) { $hour=0; }
    
    push @times,sprintf("$hour:%2.2d",$min);
}

my $ua = new LWP::UserAgent;
my $request = new HTTP::Request(GET => 'http://www.directv.com/cgi-bin/pgm.cgi');
my $response = $ua->request($request);

foreach (split /[\r\n]/, $response->content()) {
   my $p = new MyParse;
   $p->parse($_);
   $p->eof;                
}


