#!/usr/local/bin/perl -w
###########################################
# user-playlists
# Mike Schilli, 2014 (cpan@perlmeister.com)
###########################################
use strict;
use OAuth::Cmdline::Spotify;
use LWP::UserAgent;
use JSON qw( from_json );

my( $user ) = @ARGV;
die "usage: $0 user" if !defined $user;

my $oauth = OAuth::Cmdline::Spotify->new( );

my $ua = LWP::UserAgent->new();
$ua->default_header( 
    $oauth->authorization_headers );

my $resp = $ua->get( 
    "https://api.spotify.com/v1" .
    "/users/$user/playlists" );

if( $resp->is_error ) {
    die "Error: ", $resp->message();
}

my $result = from_json( $resp->content() );

for my $item ( @{ $result->{ items } } ) {
    print "$item->{ name }\n";
}

__END__

=head1 NAME

    spotify-user-playlists

=head1 SYNOPSIS

    spotify-user-playlists username

=head1 DESCRIPTION

Ask the Spotify web service for a user's playlists and print them.

Requires that you create a ~/.spotify.yml file first by running 
eg/spotify-token-init after acquiring a client ID and secret as
described in the Spotify authorization guide:

    https://developer.spotify.com/web-api/authorization-guide/

=head1 LEGALESE

Copyright 2014 by Mike Schilli, all rights reserved.
This program is free software, you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 AUTHOR

2014, Mike Schilli <cpan@perlmeister.com>
