| WWW-Yahoo-Groups documentation | Contained in the WWW-Yahoo-Groups distribution. |
WWW::Yahoo::Groups::Mechanize - Control WWW::Mechanize for WYG.
This module is a subclass of WWW::Mechanize that permits us a bit more control over some aspects of the fetching behaviour.
This module inherits from WWW::Mechanize, which inherits from LWP::UserAgent. As such, any method available to either of them is available here. Any overridden methods will be explained below.
As for "new()" in WWW::Mechanize but sets the agent string to our custom agent.
Sets or gets whether we are in debugging mode. Returns true if set, else false.
warn "Awooga!" if $self->debug;
$self->debug( 1 );
We override get in order to provide some behind the scenes actions.
We allow you to rate limit your downloading. See autosleep.
We automatically click Accept on adult confirmation. So I hope you agree to all that.
If debug is enabled, then it will display a warning showing the URL.
I should probably shift the advertisement interruption skipping into this method at some point, along with the redirect handling.
It will throw a X::WWW::Yahoo::Groups::BadFetch if
it is unable to retrieve the specified page.
Returns 0 if success, else an exception object.
my $rv = $y->get( 'http://groups.yahoo.com' );
$rv->rethrow if $rv;
# or, more idiomatically
$rv = $y->get( 'http://groups.yahoo.com' ) and $rv->rethrow;
Allows one to configure the sleep period between fetches The default is 1 (as of 1.86).
my $period = $ua->autosleep;
$ua->autosleep( 10 ); # for a 10 second delay
Iain Truskett <spoon@cpan.org>
| WWW-Yahoo-Groups documentation | Contained in the WWW-Yahoo-Groups distribution. |
package WWW::Yahoo::Groups::Mechanize; our $VERSION = '1.91';
our @ISA = qw( WWW::Mechanize ); use WWW::Mechanize; use Net::SSL; use Params::Validate qw( validate_pos SCALAR ); use strict; use warnings FATAL => 'all'; require WWW::Yahoo::Groups::Errors; Params::Validate::validation_options( WWW::Yahoo::Groups::Errors->import() );
sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->agent("Mozilla/5.0 (LWP; $class)"); return $self; }
sub debug { my $self = shift; $self->{__PACKAGE__.'-debug'} = ($_[0] ? 1 : 0) if @_; $self->{__PACKAGE__.'-debug'}; }
sub get { my $self = shift; my $url = $_[0]; warn "Fetching $url\n" if $self->debug; my $rv; $rv = eval { # Fetch page my $rv = $self->SUPER::get(@_); # Throw if problem X::WWW::Yahoo::Groups::BadFetch->throw(error => "Unable to fetch $url: ". $self->res->code.' - '.$self->res->message) if ($self->res->is_error); # Sleep for a bit if (my $s = $self->autosleep() ) { sleep( $s ); } # Return something 0; }; if ( $self->uri and $self->uri =~ m,/adultconf\?, ) { my $form = $self->form_number( 0 ); if ($self->debug) { for my $form ( $self->forms ) { warn $form->dump; } } warn "Clicking accept for adultconf\n" if $self->debug; $self->click( 'accept' ); } if ($@) { die $@ unless ref $@; $@->rethrow if $@->fatal; $rv = $@; } return $rv; }
sub autosleep { my $w = shift; my $key = __PACKAGE__.'-sleep'; if (@_) { my ($sleep) = validate_pos( @_, { type => SCALAR, callbacks => { 'is integer' => sub { shift() =~ /^ \d+ $/x }, } }, # number ); $w->{$key} = $sleep; } return ( exists $w->{$key} ? $w->{$key} : 1 ); } 1; __DATA__