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; ## Setup and global! my (%setup); setup(); core_parse(); ## SQL Stuff! sub sql_create { unlink q(mahou.db); my $dbh = DBI->connect ( q(dbi:SQLite:dbname=mahou.db),undef,undef, { RaiseError => 1, AutoCommit => 0 } ) or die q(SC: error connecting); eval { foreach my $command ( q[CREATE TABLE data ( name TEXT PRIMARY KEY, aid INTEGER, station INTEGER, airtime TEXT, eta TEXT )], q[CREATE TABLE meta ( hit INTEGER miss INTEGER, last INTEGER )], q[CREATE TABLE conf ( timezone INTEGER, debug INTEGER, local INTEGER )], ) { $dbh->do($command); } $dbh->do(q[INSERT INTO conf values(16,1,1)]); $dbh->commit; }; if ($@) { $dbh->disconnect; die q(SC: creation failure); } $dbh->disconnect; } ## Parse related stuff. sub table_header_has { my ($table, $str) = @_; return defined $table->look_down( q(_tag), q(th), sub { $_[0]->as_text() eq $str } ); } # by pc486 sub core_parse { my %showtime; note(q(core_parse: starting)) if $setup{q(debug)}; my $html = get(q(http://localhost:8049/Showtime.htm)) and note(q(core_parse: html secured)) if $setup{q(debug)}; die q(CP: html) unless $html; $html = HTML::TreeBuilder->new_from_content($html); # first step: find time table note(q(core_parse: closing in on table)) if $setup{q(debug)}; my $table = $html->look_down( q(_tag), q(table), q(bgcolor), q(#bfa89b), sub { table_header_has($_[0], q(ETA)) }, sub { not table_header_has($_[0], q(Starting)) } ); # by pc486 & edit by dean # second step: find ACTUAL time table note(q(core_parse: parsing table)) if $setup{q(debug)}; foreach my $anime ($table->look_down( q(_tag), q(tr), q(align), undef )) { # first get name, stay as hash, get link, move on my $name = $anime->look_down(q(_tag), q(a), q(name), qr/\d+/)->parent(); my $link = $name->parent()->look_down(q(_tag), q(a), q(href), qr/anidb/); # populates the showtime hash my @info = $anime->look_down(q(_tag), q(td)); %{$showtime{$name->as_text}} = map { $_->[0] => $info[$_->[1]]->as_text } ( [qw(station 3)], [qw(airtime 5)], [qw(eta 6)] ); $showtime{$name->as_text}->{q(link)} = anidb_link($link->{q(href)} or undef); } $html->delete; note(q(core_parse: returning hash)) if $setup{q(debug)}; return %showtime; } ## Sets up the configuration. sub setup { sql_create() unless -e q(mahou.db); # check db exists && die q(SU: mahou.db unwritable) unless -w q(mahou.db); # is writable my $dbh = DBI->connect ( # open db q(dbi:SQLite:dbname=mahou.db),undef,undef, { RaiseError => 1, AutoCommit => 0 } ) or die q(SU: connect); my @setup = $dbh->selectrow_array(q(SELECT * from conf)) or die q(SU: fetch); # grab array and move it to %setup %setup = map { $_ => shift(@setup) } qw(tmzn debug local); if ($setup{debug}) { note(q(setup: setup is complete)); note(qq( > $_:\t=> $setup{$_})) foreach keys %setup; } return 0; } ## Useful little functions. sub note { my $note = shift; print curti(), qq( - \u$note\n); return 1; } # pretty debugging sub anidb_link { my $url = shift; return q(-) unless $url; $url =~ s/http.+aid.(\d+)/$1/; return qq(http://anidb.net/a$url); } sub curti { my @time = @{[ gmtime ]}[2,1,0]; $time[0] = ($time[0]+$setup{q(tmzn)}) % 24; return sprintf(q(%02d:%02d:%02d),@time); } # grabs & adjusts the time from GMT