App-Scrobble

 view release on metacpan or  search on metacpan

lib/App/Scrobble.pm  view on Meta::CPAN


has 'finder' => (
    is => 'rw',
    lazy_build => 1,
    traits => [ 'NoGetopt' ],
);

sub _build_finder {
    my $self = shift;

    return Module::PluginFinder->new(
       search_path => 'App::Scrobble::Service',

       filter => sub {
          my ( $module, $searchkey ) = @_;

          return 0 unless $module->can( "is_plugin_for" );
          return $module->is_plugin_for( $searchkey );
       },
    );
}

sub scrobble {
    my $self = shift;

    my $service = $self->finder->construct( $self->url, { url => $self->url } );

    my $tracks = $service->get_tracks;

    $self->_scrobble_tracks( $tracks );
}

sub _scrobble_tracks {
    my $self = shift;
    my $tracks = shift;

    my $lastfm = Net::LastFM::Submission->new(
        ua       => LWP::UserAgent->new('timeout' => 10, 'env_proxy' => 1),
        user     => $self->username,
        password => $self->password,
    );

    my $ret = $lastfm->handshake;
    print pp $ret if $self->debug;

    # Any errors?
    if ( exists $ret->{error} ) {
        warn "There was a problem authenticating with last.fm: "
            . $ret->{reason}||$ret->{error};
        exit(1);
    }

    my $time = time;
    my $count = 0;

    foreach my $track ( @{ $tracks } ) {

        my $artist = $track->{artist};
        my $track  = $track->{title};

        # XXX use open binmode to correctly encode/decode the output
        print "Scrobbling track: $track artist: $artist \n" if $self->verbose;

        ## no critic
        my $ret = $lastfm->submit({
            artist => $artist,
            title  => $track,
            time   => $time - ( $count *  3 * 60 ),
        }) unless $self->dry_run;
        print pp $ret if $self->debug;

        $count++;
    }
}

__PACKAGE__->meta->make_immutable;

1;


__END__
=pod

=head1 NAME

App::Scrobble - Command line scrobbling app

=head1 VERSION

version 0.03

=head1 DESCRIPTION

Main functionality of L<App::Scrobble>. Takes the command line arguments and
instantiates the correct plugin, if one supports the URL to scrobble.

Instructs the plugin to grab the track data and then submits each track to
L<LastFM|http://www.last.fm>. Makes some vaguely sensible made-up submission times
so all the tracks aren't submitted at exactly the same time.

Works behind a proxy if you set the C<http_proxy> env var.

=head1 METHODS

=head2 C<scrobble>

Main sub called by the script.

=head1 SEE ALSO

L<App::scrobble>

=head1 AUTHOR

Adam Taylor <ajct@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Adam Taylor.

This is free software; you can redistribute it and/or modify it under



( run in 0.665 second using v1.01-cache-2.11-cpan-2398b32b56e )