#use LWP::Simple; # start! I wouldn't copy
use HTML::TreeBuilder; # anything seen in here,
use HTML::Entities; # it's for your own good
use DBI;
use DBD::SQLite;
use strict;
my %showtime;
my %gotime = (
'parse' => \&doshit,
'time' => sub { print curti(), "\n" },
'help' => \&helpme,
'log' => \&lastlog
);
#main program
sub doshit {
my $html = HTML::TreeBuilder->new_from_file('Showtime.htm');
$html ? wlog("tree built") : wlog("tree error");
open(LISP, ">content.txt") or warn "Failed to open: $!\n";
wlog("looking for table");
my $table = $html->look_down('_tag', 'table', #gets the table to work with
'bgcolor', '#bfa89b',
sub { table_header_has($_[0], "ETA") },
sub { not table_header_has($_[0], "Starting") });
wlog("table found");
my @fields = qw(gar1 gar2 season station comp airtime eta gar3 gar4);
wlog("grabbing values from tree");
foreach my $anime ($table->look_down('_tag', 'tr', #starts looking through the rows
'align', undef,
'bgcolor', '#e9dfdb')){
my $name = $anime->look_down('_tag', 'a', 'name', qr/\d+/)->parent(); #gets the name (for link stays as hash)
my $link = $name->parent()->look_down('_tag', 'a', 'href', qr/anidb/); #get the RIGHT link
$showtime{$name->as_text}->{'link'} = $link ? anidb_short($link->{'href'}) : undef; #sets up the hash
#print LISP "$name -> $link\n";
my $count = 0; # hackish, fix later
foreach my $node ($anime->look_down('_tag', 'td')) {
$showtime{$name->as_text}->{$fields[$count++]} = $node->as_text; #gets all those other stupid values
}
} @fields = ("link",@fields[3,5,6]); #dump the "/gar\d/s" ;-)
wlog("storing values");
foreach my $key (sort keys %showtime) {
print LISP "$key -> @{[ map { $showtime{$key}->{$_} } @fields ]}\n";
}
wlog("work done, deleting tree");
$html->delete; close(LISP) or warn "Failed to close: $!\n";
print "All done....";
}
my ($subgex,@lastlog);
if @ARGV { $ARGV_[0]->() and exit; } #better get it right. ^^;
print curti(), " - " and helpme();
while($subgex !~ /exit/){
foreach my $regex (sort keys %gotime) {
if($subgex =~ /$regex/) {
$gotime{$regex}->();
print "Now what?\n";
}
}
chomp($subgex = <STDIN>);
}
#core subroutines
sub table_header_has {
my ($table, $str) = @_; wlog("looking for $str");
return defined $table->look_down('_tag', 'th',
sub { $_[0]->as_text() eq $str });
}
sub anidb_short { #turns the ugly "anidb.info/perl-bin/animedb.pl?show=anime&aid=xxxx" into "anidb.info/axxxx"
my $url = shift;
$url =~ s/^http.*aid.(\d+)$/$1/i;
$url = 'http://anidb.info/a' . $url;
return $url;
}
sub write_database {
my $dbh = DBI->connect("dbi:SQLite:dbname=mahou.db","","") or warn "Can't connect: $!\n";
}
#stuff for the idler
sub wlog {
my $write = shift;
$write = "@{[ curti() ]} \u$write.";
print "$write\n" and push(@lastlog,$write);
} # simple logger thing
sub curti {
my @time = @{[ gmtime ]}[2,1,0];
$time[0] = $time[0]-3 < 0 ? $time[0]+9 : $time[0]-3;
return sprintf("%02d:%02d:%02d", @time);
} # gets the time... DEAN'S WAY!
sub helpme {
print "Commands: help, parse, time, log, exit\n";
}
sub lastlog {
print "No logs.\n" and return unless @lastlog;
print foreach map { $_, "\n" } @lastlog;
}