| 1 | package Net::LastFM; | ||
| 2 | $Net::LastFM::VERSION = '0.36'; | ||
| 3 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use MooseX::StrictConstructor; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Digest::MD5 qw(md5_hex); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use JSON::XS::VersionOneAndTwo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use URI::QueryParam; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | has 'api_key' => ( | ||
| 11 | is => 'rw', | ||
| 12 | isa => 'Str', | ||
| 13 | required => 1, | ||
| 14 | ); | ||
| 15 | |||
| 16 | has 'api_secret' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => 'Str', | ||
| 19 | required => 1, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has 'ua' => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'LWP::UserAgent', | ||
| 25 | required => 0, | ||
| 26 | default => sub { | ||
| 27 | my $ua = LWP::UserAgent->new; | ||
| 28 | $ua->agent( 'Net::LastFM/' . $VERSION ); | ||
| 29 | $ua->env_proxy; | ||
| 30 | return $ua; | ||
| 31 | } | ||
| 32 | ); | ||
| 33 | |||
| 34 | my $ROOT = 'http://ws.audioscrobbler.com/2.0/'; | ||
| 35 | |||
| 36 | sub create_http_request { | ||
| 37 | my ( $self, %conf ) = @_; | ||
| 38 | |||
| 39 | $conf{api_key} = $self->api_key; | ||
| 40 | my $uri = URI->new('http://ws.audioscrobbler.com/2.0/'); | ||
| 41 | |||
| 42 | foreach my $key ( keys %conf ) { | ||
| 43 | my $value = $conf{$key}; | ||
| 44 | $uri->query_param( $key, $value ); | ||
| 45 | } | ||
| 46 | $uri->query_param( 'format', 'json' ); | ||
| 47 | |||
| 48 | return HTTP::Request->new( 'POST', $uri ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub create_http_request_signed { | ||
| 52 | my ( $self, %conf ) = @_; | ||
| 53 | $conf{api_key} = $self->api_key; | ||
| 54 | |||
| 55 | my $to_hash = ''; | ||
| 56 | foreach my $key ( sort keys %conf ) { | ||
| 57 | my $value = $conf{$key}; | ||
| 58 | $to_hash .= $key . $value; | ||
| 59 | } | ||
| 60 | $to_hash .= $self->api_secret; | ||
| 61 | $conf{api_sig} = md5_hex($to_hash); | ||
| 62 | |||
| 63 | return $self->create_http_request(%conf); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub request { | ||
| 67 | my ( $self, %conf ) = @_; | ||
| 68 | my $request = $self->create_http_request(%conf); | ||
| 69 | return $self->_make_request($request); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub request_signed { | ||
| 73 | my ( $self, %conf ) = @_; | ||
| 74 | my $request = $self->create_http_request_signed(%conf); | ||
| 75 | return $self->_make_request($request); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _make_request { | ||
| 79 | my ( $self, $request ) = @_; | ||
| 80 | my $ua = $self->ua; | ||
| 81 | |||
| 82 | my $response = $ua->request($request); | ||
| 83 | my $data = from_json( $response->content ); | ||
| 84 | |||
| 85 | if ( defined $data->{error} ) { | ||
| 86 | my $code = $data->{error}; | ||
| 87 | my $message = $data->{message}; | ||
| 88 | confess "$code: $message"; | ||
| 89 | } else { | ||
| 90 | return $data; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ | ||
| 97 | |||
| 98 | =head1 NAME | ||
| 99 | |||
| 100 | Net::LastFM - A simple interface to the Last.fm API | ||
| 101 | |||
| 102 | =head1 SYNOPSIS | ||
| 103 | |||
| 104 | my $lastfm = Net::LastFM->new( | ||
| 105 | api_key => 'XXX', | ||
| 106 | api_secret => 'YYY', | ||
| 107 | ); | ||
| 108 | my $data = $lastfm->request_signed( | ||
| 109 | method => 'user.getRecentTracks', | ||
| 110 | user => 'lglb', | ||
| 111 | ); | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | The module provides a simple interface to the Last.fm API. To use | ||
| 116 | this module, you must first sign up at L<http://www.last.fm/api> | ||
| 117 | to receive an API key and secret. | ||
| 118 | |||
| 119 | You can then make requests on the API - most of the requests | ||
| 120 | are signed. You pass in a hash of paramters and a data structure | ||
| 121 | mirroring the response is returned. | ||
| 122 | |||
| 123 | This module confesses if there is an error. | ||
| 124 | |||
| 125 | =head1 METHODS | ||
| 126 | |||
| 127 | =head2 request | ||
| 128 | |||
| 129 | This makes an unsigned request: | ||
| 130 | |||
| 131 | my $data = $lastfm->request( method => 'auth.gettoken' ); | ||
| 132 | |||
| 133 | =head2 request_signed | ||
| 134 | |||
| 135 | This makes a signed request: | ||
| 136 | |||
| 137 | my $data = $lastfm->request_signed( | ||
| 138 | method => 'user.getRecentTracks', | ||
| 139 | user => 'lglb', | ||
| 140 | ); | ||
| 141 | |||
| 142 | =head2 create_http_request | ||
| 143 | |||
| 144 | If you want to integrate this module into another HTTP framework, this | ||
| 145 | method will simple create an unsigned L<HTTP::Request> object: | ||
| 146 | |||
| 147 | my $http_request = $lastfm->create_http_request( | ||
| 148 | method => 'auth.gettoken' | ||
| 149 | ); | ||
| 150 | |||
| 151 | =head2 create_http_request_signed | ||
| 152 | |||
| 153 | If you want to integrate this module into another HTTP framework, this | ||
| 154 | method will simple create a signed L<HTTP::Request> object: | ||
| 155 | |||
| 156 | my $http_request = $lastfm->create_http_request_signed( | ||
| 157 | method => 'user.getRecentTracks', | ||
| 158 | user => 'lglb', | ||
| 159 | ); | ||
| 160 | |||
| 161 | =head1 AUTHOR | ||
| 162 | |||
| 163 | Leon Brocard <acme@astray.com> | ||
| 164 | |||
| 165 | =head1 COPYRIGHT | ||
| 166 | |||
| 167 | Copyright (C) 2008-9, Leon Brocard. | ||
| 168 | |||
| 169 | =head1 LICENSE | ||
| 170 | |||
| 171 | This module is free software; you can redistribute it or | ||
| 172 | modify it under the same terms as Perl itself. | ||