WWW-Deezer

 view release on metacpan or  search on metacpan

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

package WWW::Deezer;

use strict;
use warnings;

use Carp();
use LWP::UserAgent;
use JSON;
use URI::Escape;

use WWW::Deezer::SearchResult;
use WWW::Deezer::Artist;

our $VERSION = '0.03';
our $API_VERSION = '2.0';

sub new {
    my ($class, $params) = @_;

#   Carp::croak("Options to WWW::Deezer should be in a hash reference")
#       if ref($params) ne ref {};

    my $self = {
        baseurl => "http://api.deezer.com/$API_VERSION/",
        ua      => LWP::UserAgent->new,
        json    => JSON->new->allow_nonref,
        debug   => 0,
    };

    $self->{ua}->agent("WWW::Deezer v".$VERSION);

    bless $self => $class;
    return $self;
}

sub album {
    my ($self, $p) = @_;
    my $uri = 'album';

    my $id = _is_hashref ($p) ? int ($p->{id}) : int ($p);

    my $res = $self->_get_url ({
        url     => $uri.'/'.$id,
        method  => 'GET'
    });

    $res = $self->{json}->decode ($res) unless _is_hashref ($res);
    $res->{deezer_obj} = $self;

    return WWW::Deezer::Album->new($res);
}

sub artist {
    my ($self, $p) = @_;
    my $uri = 'artist';

    my $id = _is_hashref ($p) ? int ($p->{id}) : int ($p);
    
    my $res = $self->_get_url ({
        url     => $uri.'/'.$id,
        method  => 'GET'
    });

    $res = $self->{json}->decode ($res) unless _is_hashref ($res);
    $res->{deezer_obj} = $self;
  
    return WWW::Deezer::Artist->new($res);
}

sub search { # http://developers.deezer.com/api/search
    # 2DO: limits? paging?
    my ($self, $p) = @_;

    my $uri = 'search';
    my ($q, $order, $index);

    if (_is_hashref ($p)) {
        $q = _to_string( $p->{q} );
        $order = $p->{order} || 'RANKING';
        $index = $p->{index} || 0;
    }
    else {
        $q = $p;
        $order = 'RANKING';
        $index = 0;
    }

    $q = uri_escape($q);

    my $res = $self->_get_url ({
        url     => $uri."?q=$q&order=$order&index=$index",
        method  => 'GET'

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

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