scrapPeer.pm package skeleton

Perl package skeleton for scrapping with log dispatcher

stáhnout snippet
zobrazení: SyntaxHighlighter | GeSHi | Holý text
#   (c) 2007 sh(a)isecure.cz

package scrapPeer;
use strict;

use Text::Iconv;
use Data::Dumper;     # debug
use WWW::Mechanize;   # scraper

use constant BASE => 'http://www.foobar.cz';

# ------------------------------------------------------------------------------
# construct

sub new {
  my ($pkg, $log) = @_;
  
  my $bot = WWW::Mechanize->new();
  $bot->cookie_jar(HTTP::Cookies->new(file => "cookies.dat", autosave => 1));
  $bot->agent_alias( 'Windows IE 6' );
  $bot->stack_depth(0);
  
  my $self = {
    _bot      => $bot,
    _log      => $log
  };
  bless $self, $pkg;
  return $self;
}

# bot getter
sub bot {
  my ($self)  = @_;
  return $self->{_bot};
}

# log dispatch
sub l {
  my ($self, $sev, $mess)  = @_;
  
  if($self->{_log}) {
    $self->{_log}->l($sev, $mess);
  } else {
    print "$mess\n";
  }
}

# ------------------------------------------------------------------------------

sub login {
  my ($self, $user, $pass)  = @_;
  my $url; 
  
  $self->l('info',"Trying to login ".BASE);
  $self->bot->get(BASE);
  
  # ...

  $self->bot->form_number(2);
  $self->bot->field('Login', $user);
  $self->bot->field('Password', $pass);
  $self->bot->submit();
  
  # ...

  $self->l('debug', "loged ok");
}

sub scrap {
  # ...
}

1;


Tagy:
perl 73 řádků | 2008-02-04 20:06:33 | air.kadlec@seznam.cz