All pastes #572833 Raw Edit

more of the same

public text v1 · immutable
·published 2007-06-17 21:31 UTC
rendered paste body
#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 @lastlog;
my %things = (
               'parse' => \&core_parse,
               'time'  => sub { print curti(), "\n" }
             );

print curti(), " - Commands: parse, time, exit\n";
chomp(my $subjex = <STDIN>);
while($subjex !~ /exit/io){
  foreach my $regex (keys %things) {
    $things{$regex}->() if $subjex =~ /$regex/i;
  } chomp($subjex = <STDIN>);
} # 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_short {  # makes pretty URLs
  my $url = shift;
  $url =~ s/^http.*aid.(\d+)$/$1/i;
  $url = 'http://anidb.info/a' . $url;
  return $url;
}

sub sql_work {

}

sub core_parse {
  my $html = HTML::TreeBuilder->new_from_file('Showtime.htm');  # openings files, 
  #$html ? wlog("tree built") : wlog("tree error");             # building trees,
  open(LISP, ">content.txt") or warn "Failed to open: $!\n";    # and logging. :) <-- pun?
  
  my %showtime; my @fields = ('number', 'series', 'season', 'station', 'company', 'airtime', 'eta', 'eps', 'links');
  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 <tr> 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 <<RIGHT>> link (which is kinda good)

    $showtime{$name->as_text}->{'link'} = $link ? anidb_short($link->{'href'}) : undef; # starts the hash->hash


    #print "@nodes\n@fields\n";
    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 LISP "$anime  <-<+>-> ", $showtime{$anime}->{'link'},
               "\n  Station -> ", $showtime{$anime}->{'station'},
               "\n  Airtime -> ", $showtime{$anime}->{'airtime'},
               "\n  ETA     -> ", $showtime{$anime}->{'eta'}, "\n\n";
  }

  $html->delete; close(LISP) 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!