Permalink
Browse files

default to using JSON

  • Loading branch information...
1 parent 0c3cdcb commit fb19e7ef7ae57379371474beb3b28cc86d4ed68e @st3vil committed Nov 24, 2011
Showing with 26 additions and 16 deletions.
  1. +2 −1 bin/cmd.pl
  2. +1 −0 bin/portablog-scrobbler.pl
  3. +1 −0 bin/scrobble.pl
  4. +22 −15 lib/Net/LastFMAPI.pm
View
3 bin/cmd.pl
@@ -7,6 +7,7 @@
use lib "$Bin/../lib";
use Net::LastFMAPI;
use JSON::XS;
+use YAML::Syck;
die "usage: $0 user.whatEver something=nothing nothing=Some Things etc=etc\n" unless @ARGV;
if (exists $Net::LastFMAPI::methods->{lc($ARGV[0])}) {
@@ -18,7 +19,7 @@
}
my $res = lastfm($method, %params);
if (ref $res eq "HASH") {
- $res = encode_json($res);
+ $res = Dump($res);
}
say $res
}
View
1 bin/portablog-scrobbler.pl
@@ -39,6 +39,7 @@
sub submat {
my $res = lastfm(
"track.scrobble",
+ format => "xml",
@set,
);
my $n = @set;
View
1 bin/scrobble.pl
@@ -21,6 +21,7 @@
my $res = lastfm(
"track.scrobble",
%params,
+ format => "xml",
timestamp => scalar(time()),
);
unless ($res =~ /accepted="1"/) {
View
37 lib/Net/LastFMAPI.pm
@@ -19,8 +19,7 @@ our $secret = 'd004c86dcfa8ef4c3977b04f558535f2';
our $session_key; # see load_save_sessionkey()
our $ua = new LWP::UserAgent(agent => "Net::LastFMAPI/$VERSION");
our $username; # not important
-
-our $json = 0;
+our $xml = 0;
our $cache = 0;
our $cache_dir = "$ENV{HOME}/.net-lastfmapi-cache/";
@@ -217,7 +216,7 @@ sub lastfm {
}
$params{method} = $method;
$params{api_key} = $api_key;
- $params{format} ||= "json" if $json;
+ $params{format} = "json" unless $params{format} || $xml;
delete $params{format} if $params{format} && $params{format} eq "xml";
unless (exists $methods->{$method}) {
@@ -345,10 +344,7 @@ Net::LastFMAPI - LastFM API 2.0
=head1 SYNOPSIS
use Net::LastFMAPI;
- my $xml = lastfm("artist.getSimilar", artist => "Robbie Basho");
-
- $Net::LastFMAPI::json = 1;
- my $data = lastfm(...); # decodes it for you
+ my $perl_data = lastfm("artist.getSimilar", artist => "Robbie Basho");
# sets up a session/gets authorisation when needed for write actions:
my $res = lastfm(
@@ -357,7 +353,16 @@ Net::LastFMAPI - LastFM API 2.0
track => "Wounded Knee Soliloquy",
timestamp => time(),
);
- $success = $res =~ m{<scrobbles accepted="1"};
+ $success = $res->{scrobbles}->{'@attr'}->{accepted} == 1;
+
+ my $xml = lastfm(...); # with config value: xml => 1
+ my $xml = lastfm(..., format => "xml");
+ $success = $xml =~ m{<scrobbles accepted="1"};
+
+ # see also:
+ # bin/cmd.pl
+ # bin/scrobble.pl
+ # bin/portablog-scrobbler.pl
=head1 DESCRIPTION
@@ -367,6 +372,10 @@ Takes care of POSTing to write methods, doing authorisation when needed.
Dies if something went obviously wrong.
+Can return xml if you like, defaults to returning perl data/requesting json.
+Not all methods support JSON. Beware of "@attr" and empty elements turned into
+whitespace strings instead of empty arrays.
+
=head1 THE SESSION KEY
$Net::LastFMAPI::session_key = "secret"
@@ -382,15 +391,13 @@ probably fine.
Consider altering the subroutines B<talk_authentication>, B<load_save_sessionkey>,
or simply setting the B<$Net::LastFMAPI::session_key> before needing it.
-=head1 RETURN PERL DATA
+=head1 RETURN XML
- $Net::LastFMAPI::json = 1
-
-This will automatically add B<format =E<gt> "json"> to every request B<and decode
-the result> into perl data for you.
+ $Net::LastFMAPI::xml = 1
-Not all methods support JSON. Beware of "@attr" and empty elements turned into
-whitespace strings instead of empty arrays.
+This will return an xml string to you. You can also set B<format =E<gt> "xml">
+for a particular request. Apparently, not all methods support JSON. For casual
+hacking, though, getting perl data is much more convenient.
=head1 CACHING

0 comments on commit fb19e7e

Please sign in to comment.