rendered paste bodyuse 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 %things = (
'time' => sub { print curti(), "\n" },
'parse' => \&sql_writer,
'find' => sub { sql_finder() },
'help' => sub { print curti(), " - Commands: parse, find, time, exit\n" }
);
chomp(my $subjex = <STDIN>);
while($subjex !~ /exit/io){
foreach my $regex (sort keys %things) {
if($subjex =~ /^\s*($regex)(\s+.*)?/i) {
$things{$1}->($2);
}
}
chomp($subjex = <STDIN>);
} # made this a little nicer
### ###
########################
# #
# core subroutines #
# #
########################
### ###
# # # # # # # # # # # # #
# stuff for anidb links #
# # # # # # # # # # # # #
sub anidb_link { # makes pretty URLs
my $url = shift;
return '-' if !$url;
$url = 'http://anidb.info/a' . $url;
return $url;
}
sub anidb_id { # return AIDs
my $url = shift;
$url =~ s/^http.*aid.(\d+)$/$1/i;
return $url;
}
# # # # # # # # # # #
# sql stuff... ugh! #
# # # # # # # # # # #
sub sql_finder {
my $search = shift;
print curti(), " Searching for: $search\n";
my $dbh = DBI->connect("dbi:SQLite:dbname=mahou.db","","",
{ RaiseError => 1, AutoCommit => 0 });
$dbh->disconnect;
print curti(), " Searching done.\n";
}
sub sql_writer {
my $hash = core_parse(); # get the hash
print curti(), " Starting write.\n";
my $dbh = DBI->connect("dbi:SQLite:dbname=mahou.db","","",
{ RaiseError => 1, AutoCommit => 0 });
if (1 == 1) { # BUT WHAT IF IT FAILS?! (it's for testing)
eval {
$dbh->do('DELETE FROM mahou_data');
my $sth = $dbh->prepare('INSERT INTO mahou_data values(?,?,?,?,?)');
foreach my $anime (sort keys %{$hash}) {
$sth->execute( $anime, @{[ map { $hash->{$anime}{$_} } ('link','station','airtime','eta') ]} );
}
$dbh->commit;
};
if($@) {
$dbh->rollback;
print curti(), " Error.\n";
}
}
$dbh->disconnect;
print curti(), " Database closed.\n";
}
# # # # # # # # # # # # # # # #
# download, parse and related #
# # # # # # # # # # # # # # # #
sub table_header_has {
my ($table, $str) = @_;
return defined $table->look_down('_tag', 'th',
sub { $_[0]->as_text() eq $str });
}
sub core_parse {
my @fields = ('number', 'series', 'season', 'station', 'company', 'airtime', 'eta', 'eps', 'links');
my ($html,%showtime) = (HTML::TreeBuilder->new_from_file("Showtime.htm"),undef);
# my $html = get("http://mahou.org/Showtime");
# $html = HTML::TreeBuilder->new_from_content($html);
my $table = $html->look_down(
'_tag', 'table', 'bgcolor', '#bfa89b',
sub { table_header_has($_[0], "ETA") },
sub { not table_header_has($_[0], "Starting") }
);
foreach my $anime ($table->look_down('_tag', 'tr',
'align', undef, # skips the first <tr>, which we don't need
'bgcolor', '#e9dfdb')
){
my $name = $anime->look_down('_tag', 'a', 'name', qr/\d+/)->parent(); # gets the name, for LINK: stays as hash; pc!!
my $link = $name->parent()->look_down('_tag', 'a', 'href', qr/anidb/); # get the ^<RIGHT>^ link (which is kinda good)
$showtime{$name->as_text}->{'link'} = $link ? anidb_id($link->{'href'}) : undef; # starts the hash->hash
map { $showtime{$name->as_text}->{$fields[$_]} = # why waste @nodes? xD
@{[ $anime->look_down('_tag', 'td') ]}[$_]->as_text } (3,5,6);
}
$html->delete;
print curti(), " All done parse.\n";
return \%showtime;
}
# # # # # # # # # # # #
# stuff for the idler #
# # # # # # # # # # # #
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 super_report {
# foreach my $anime (sort keys %showtime) {
# print WORK "$anime <-<+>-> ", anidb_link($showtime{$anime}->{'link'}),
# "\n Station -> ", $showtime{$anime}->{'station'},
# "\n Airtime -> ", $showtime{$anime}->{'airtime'},
# "\n ETA -> ", $showtime{$anime}->{'eta'}, "\n\n";
# }
}