WWW::Google::Images::Image - Image object for WWW::Google::Images


WWW-Google-Images documentation Contained in the WWW-Google-Images distribution.

Index


Code Index:

NAME

Top

WWW::Google::Images::Image - Image object for WWW::Google::Images

Constructor

Top

new($agent, $content, $context)

Creates and returns a new WWW::Google::Images::Image object.

Accessors

Top

$image->content_url()

Returns the url to the image file itself.

$image->context_url()

Returns the url to the web page including the image.

Other methods

Top

$image->save_content(%args)

Save the image file. The default is to keep its original file name, but this behavior can be altered using optional parameters.

Optional parameters:

file => $file

Use $file as file name.

dir => $directory

Use $directory as a directory path to save the file. If only 'file' is specified, it will be saved in a path relative to the current working directory.

base => $base

Use $base with lowercase original extension added as file name.

$image->save_context(%args)

Save the web page. The default is to keep its original file name, but this behavior can be altered using optional parameters.

Optional parameters:

file => $file

Use $file as file name.

dir => $directory

Use $directory as a directory path to save the file. If only 'file' is specified, it will be saved in a path relative to the current working directory.

base => $base

Use $base with lowercase original extension added as file name.

COPYRIGHT AND LICENSE

Top

AUTHOR

Top

Guillaume Rousse <grousse@cpan.org>


WWW-Google-Images documentation Contained in the WWW-Google-Images distribution.
# $Id: Image.pm,v 1.10 2006/08/22 13:03:13 rousse Exp $
package WWW::Google::Images::Image;

use File::Basename;
use File::Path;

sub new {
    my ($class, $agent, $content, $context) = @_;

    my $self = bless {
        _agent   => $agent,
        _content => $content,
        _context => $context,
    }, $class;

    return $self;
}

sub content_url {
    my ($self) = @_;
    return $self->{_content};
}

sub context_url {
    my ($self) = @_;
    return $self->{_context};
}

sub save_content {
    my ($self, %args) = @_;

    my $file = $self->_get_file($self->{_content}, %args);
    $self->_save_as($file, $self->{_content});

    return $file;
}

sub save_context {
    my ($self, %args) = @_;

    my $file = $self->_get_file($self->{_context}, %args);
    $self->_save_as($file, $self->{_context});

    return $file;
}

sub _save_as {
    my ($self, $file, $url) = @_;

    # make sure the path exist
    my $dir = dirname($file);
    mkpath($dir) unless -d $dir;

    # save file
    $self->{_agent}->get($url, ":content_file" => $file );
    $self->{_agent}->back();
}

sub _get_file {
    my ($self, $url, %args) = @_;

    my $file;
    if ($args{file}) {
        $file = $args{file};
    } elsif ($args{base}) {
        $url =~ /(\.\w+)$/;
        $file = $args{base} . lc($1);
    } else {
        $url =~ /([^\/]+)$/;
        $file = $1;
    }

    my $dir;
    if ($args{dir}) {
        $dir = $args{dir};
    } else {
        $dir = '.';
    }

    return $dir . '/' . $file;
}

1;