#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 ($returned,%things) = (undef,( 'parse' => \&core_parse, 'time' => sub { print curti(), "\n" }, 'write' => \&sql_writer ) ); print curti(), " - Commands: parse, time, exit\n"; chomp(my $subjex = ); while($subjex !~ /exit/io){ foreach my $regex (keys %things) { $returned = $things{$regex}->() if $subjex =~ /$regex/i; } chomp($subjex = ); } # made this a little nicer ###################### ## core subroutines ## ###################### sub table_header_has { my ($table, $str) = @_; return defined $table->look_down('_tag', 'th', sub { $_[0]->as_text() eq $str }); } sub anidb_link { # makes pretty URLs my $url = shift; $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; } sub sql_finder { my $search = shift; sql_writer(); #for now always write my $dbh = DBI->connect("dbi:SQLite:dbname=mahou.db","","", { RaiseError => 1, AutoCommit => 0 }); my $field = $search =~ /^\s*(?:\d+)|(?:id:?\d+)\s*$/io ? 'aid' : 'name'; eval { my $sth = $dbh->prepare("select * from mahou_data where ? like" . '%' . "?" . '%'); $sth->execute( $field, $search ); while(my @lol = $sth->fetchrow_array) { print "@lol\n"; } }; print "ERRRRROORRR\n" if $@; $dbh->disconnect; } sub sql_writer { my $hash = core_parse(); #for now always parse file into %showtime hash my $dbh = DBI->connect("dbi:SQLite:dbname=mahou.db","","", { RaiseError => 1, AutoCommit => 0 }); 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; }; $dbh->rollback if $@; $dbh->disconnect; } sub core_parse { my $html = HTML::TreeBuilder->new_from_file('Showtime.htm'); # openings files, #$html ? wlog("tree built") : wlog("tree error"); # building trees, open(WORK, ">content.txt") or warn "Failed to open: $!\n"; # and logging. :) <-- pun? my @fields = ('number', 'series', 'season', 'station', 'company', 'airtime', 'eta', 'eps', 'links'); my %showtime; my $table = $html->look_down('_tag', 'table', 'bgcolor', '#bfa89b', # gets the content table sub { table_header_has($_[0], "ETA") }, sub { not table_header_has($_[0], "Starting") } ); foreach my $anime ($table->look_down('_tag', 'tr', 'align', undef, # <-- this line is for skipping- 'bgcolor', '#e9dfdb')){ # the first which isn't needed. 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 <> 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); } 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"; } return(\%showtime); # temp? $html->delete; close(WORK) or warn "Failed to close: $!\n"; print "All done.\n"; } ######################### ## 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!