WWW-Moviepilot

 view release on metacpan or  search on metacpan

lib/WWW/Moviepilot.pm  view on Meta::CPAN

        host    => 'www.moviepilot.de',
    });

    # direct retrieval
    my $movie = $m->movie( 'matrix' );

    # search
    my @movies = $m->search_movie( 'matrix' );
    foreach my $movie ( @movies ) {
        print $movie->display_title;
    }

    # cast of a movie
    my @cast = $m->cast( 'matrix' );
    foreach my $person ( @cast ) {
        print $person->last_name;
        print $person->character;
    }

    # filmography of a person
    my @filmography = $m->filmography( 'paul-newman' );
    foreach my $movie ( @filmography ) {
        print $movie->display_title;
        print $movie->character;
    }

I<Please note: This module is still in early development and subject to change.>

=head1 METHODS

=head2 new( $args )

Creates a new WWW::Moviepilot instance.

    my $m = WWW::Moviepilot->new( $args );

C<$args> must be a hash reference, you should supply an API key:

    $args->{api_key} = ...;

To get a valid API key you should read L<http://wiki.github.com/moviepilot/moviepilot-API/>.

Further optional arguments:

=over 4

=item * C<host> (default: C<www.moviepilot.de>)

The host where the requests are sent to.

=item * C<ua> (default: C<< LWP::UserAgent->new >>)

A L<LWP::UserAgent> compatible user agent.

=back

=cut

sub new {
    my ($class, $args) = @_;
    my $self = bless {} => $class;
    $self->{api_key} = $args->{api_key} || croak "api_key is missing at " . __PACKAGE__ . "->new()";
    $self->{host}    = $args->{host}    || 'www.moviepilot.de';
    $self->{ua}      = $args->{ua}      || LWP::UserAgent->new;

    $self->{host} = 'http://' . $self->{host} unless $self->{host} =~ m{^http://};

    return $self;
}

=head2 movie( $name ) | movie( $source => $id )

Retrieve a movie as L<WWW::Moviepilot::Movie> object.
There are two ways to specify which movie to retrieve.
First, you can provide the name of the movie (this name is some kind of normalised,
I'm not sure how exactly):

    my $movie = $m->movie( 'matrix' );

The second form is to provide an alternate ID:

    my $movie = $m->movie( imdb => '133093' );
    my $movie = $m->movie( amazon => 'B00004R80K' );

=cut

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

    my $url = $self->host . '/movies/';
    if ( @args > 2 ) {
        croak "invalid usage of " . __PACKAGE__ . "->movie()";
    }
    $url .= join '-id-', map { uri_escape($_) } @args;
    $url .= '.json';

    my $uri = URI->new( $url );
    $uri->query_form( api_key => $self->api_key );
    my $res = $self->ua->get( $uri->as_string );

    if( $res->is_error ) {
        croak $res->status_line;
    }

    my $json = JSON::Any->from_json( $res->decoded_content );

    my $movie = WWW::Moviepilot::Movie->new({ m => $self });
    $movie->populate({ data => { movie => $json } });
    return $movie;
}

=head2 search_movie( $query )

Searches for a movie and returns a list with results:

    my @movielist = $m->search_movie( 'matrix' );
    if ( @movielist == 0 ) {
        print 'no movies found';
    }
    else {
        # each $movie is a WWW::Moviepilot::Movie object

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 7.240 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )