Audio-Scrobbler

 view release on metacpan or  search on metacpan

bin/scrobbler-helper  view on Meta::CPAN

  fix_track_name=yes

=cut

use Config::IniFiles;
use Encode;
use Getopt::Std;

use Audio::Scrobbler;

sub is_true($);

my %infovars = (
	'cmdopts'	=> [ qw/P V/ ],
	'cmdline'	=> [ qw/title artist album year comment genre length/ ],
	'global'	=> [ qw/username password/ ],
	'global_nc'	=> [ qw/default_encoding fix_track_name/ ],
);
my $verbose = 0;

MAIN:

bin/scrobbler-helper  view on Meta::CPAN

	$scrob = new Audio::Scrobbler('cfg' => \%info) or
	    die "Could not create an Audio::Scrobbler object\n";
	$scrob->handshake() or
	    die "Scrobbler: ".$scrob->err()."\n";
	print "RDBG handshake successful, it seems\n" if $verbose;
	$scrob->submit(\%info) or
	    die "Scrobbler submit: ".$scrob->err()."\n";
	print "RDBG submision successful, it seems\n" if $verbose;
}

sub is_true($)
{
	my $s = lc $_[0];

	return ($s eq '1' || $s eq 'on' || $s =~ /^[ty]/);
}

=head1 TODO

=over 4

lib/Audio/Scrobbler.pm  view on Meta::CPAN


=cut

use Digest::MD5 qw/md5_hex/;
use LWP::UserAgent;

our @ISA = qw();

our $VERSION = '0.01';

sub err($ $);
sub handshake($);

sub get_ua($);

sub URLEncode($);
sub URLDecode($);

=head1 METHODS

The C<Audio::Scrobbler> class defines the following methods:

=over 4

=item * new ( cfg => { ... } )

Create a new C<Audio::Scrobbler> object and initialize it with

lib/Audio/Scrobbler.pm  view on Meta::CPAN

	return $self;
}

=item * err (message)

Retrieves or sets the description of the last error encountered in
the operation of this C<Audio::Scrobbler> object.

=cut

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

	$self->{'err'} = $err if $err;
	return $self->{'err'};
}

=item * handshake ()

Perfors a handshake with the AudioScrobbler API via a request to

lib/Audio/Scrobbler.pm  view on Meta::CPAN


If the B<fake> configuration parameter is set, the L<handshake> method
does not actually perform the handshake with the AudioScrobbler API,
just simulates a successful handshake and returns a true value.

If the B<verbose> configuration parameter is set, the L<handshake>
method reports its progress with diagnostic messages to the standard output.

=cut

sub handshake($)
{
	my ($self) = @_;
	my ($ua, $req, $resp, $c, $s);
	my (@lines);

	delete $self->{'nexturl'};
	delete $self->{'md5ch'};

	$ua = $self->get_ua() or return undef;
	$s = 'hs=true&p=1.1&c='.

lib/Audio/Scrobbler.pm  view on Meta::CPAN


If the B<fake> configuration parameter is set, the L<submit> method
does not actually submit the track information to the AudioScrobbler API,
just simulates a successful submission and returns a true value.

If the B<verbose> configuration parameter is set, the L<submit>
method reports its progress with diagnostic messages to the standard output.

=cut

sub submit($ \%)
{
	my ($self, $info) = @_;
	my ($ua, $req, $resp, $s, $c, $datestr, $md5resp);
	my (@t, @lines);

	# A couple of sanity checks - those never hurt
	if (!defined($self->{'nexturl'}) || !defined($self->{'md5ch'})) {
		$self->err('Cannot submit without a successful handshake');
		return undef;
	}

lib/Audio/Scrobbler.pm  view on Meta::CPAN


=over 4

=item * get_ua ()

Creates or returns the cached C<LWP::UserAgent> object used by
the C<Audio::Scrobbler> class for access to the AudioScrobbler API.

=cut

sub get_ua($)
{
	my ($self) = @_;
	my ($ua);

	$self->{'ua'} ||= new LWP::UserAgent();
	if (!$self->{'ua'}) {
		$self->err('Could not create a LWP UserAgent object');
		return undef;
	}
	$self->{'ua'}->agent('scrobbler-helper/1.0pre1 '.

lib/Audio/Scrobbler.pm  view on Meta::CPAN

}

=item * URLDecode (string)

Decode a URL-encoded string.

Obtained from http://glennf.com/writing/hexadecimal.url.encoding.html

=cut

sub URLDecode($) {
	my $theURL = $_[0];
	$theURL =~ tr/+/ /;
	$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
	$theURL =~ s/<!--(.|\n)*-->//g;
	return $theURL;
}

=item * URLEncode (string)

Return the URL-encoded representation of a string.

Obtained from http://glennf.com/writing/hexadecimal.url.encoding.html

=cut

sub URLEncode($) {
	my $theURL = $_[0];
	$theURL =~ s/([^a-zA-Z0-9_])/'%' . uc(sprintf("%2.2x",ord($1)));/eg;
	return $theURL;
}

=back

=head1 TODO

=over 4



( run in 0.651 second using v1.01-cache-2.11-cpan-65fba6d93b7 )