From eff476d3b7c5b8864f4a7646f001803b838365bb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 18 Mar 2012 18:06:54 -0500 Subject: initial implementation --- bin/lastfm_export | 51 ++++++++++++++++++++++++++++++++++++++ dist.ini | 3 ++- lib/LastFM/Export.pm | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 bin/lastfm_export diff --git a/bin/lastfm_export b/bin/lastfm_export new file mode 100644 index 0000000..bd0fab8 --- /dev/null +++ b/bin/lastfm_export @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; +# PODNAME: lastfm_export +# ABSTRACT: data exporter for last.fm + +use DBI; +use Getopt::Long qw(:config pass_through); +use LastFM::Export; +use Term::ProgressBar; + +my $dsn; +GetOptions( + 'dsn=s' => \$dsn, +); +die "--dsn is required" unless $dsn; + +my $dbh = DBI->connect($dsn, '', '', { RaiseError => 1, AutoCommit => 0 }); +$dbh->do(<<''); +CREATE TABLE `tracks` ( + artist varchar(1024) NOT NULL, + album varchar(1024) DEFAULT NULL, + name varchar(1024) NOT NULL, + timestamp integer(11) NOT NULL +); + +my $exporter = LastFM::Export->new_with_options; +my $progress = Term::ProgressBar->new({ + count => $exporter->track_count, + ETA => 'linear', +}); + +my $sth = $dbh->prepare( + 'INSERT INTO tracks (artist, album, name, timestamp) VALUES (?, ?, ?, ?)' +); + +my $count = 1; +my $s = $exporter->tracks; +while (my $block = $s->next) { + for my $item (@$block) { + $sth->execute( + $item->{artist}{'#text'}, + $item->{album}{'#text'}, + $item->{name}, + $item->{date}{uts}, + ); + $progress->update($count++); + } + $dbh->commit; + sleep 1; +} diff --git a/dist.ini b/dist.ini index 65da424..a38663c 100644 --- a/dist.ini +++ b/dist.ini @@ -5,5 +5,6 @@ copyright_holder = Jesse Luehrs [@DOY] dist = LastFM-Export +repository = github -[Prereqs] +[AutoPrereqs] diff --git a/lib/LastFM/Export.pm b/lib/LastFM/Export.pm index e69de29..c36f65f 100644 --- a/lib/LastFM/Export.pm +++ b/lib/LastFM/Export.pm @@ -0,0 +1,69 @@ +package LastFM::Export; +use Moose; +# ABSTRACT: data exporter for last.fm + +use Data::Stream::Bulk::Callback; +use Net::LastFM; + +with 'MooseX::Getopt'; + +has user => ( + is => 'ro', + isa => 'Str', + required => 1, +); + +has api_key => ( + is => 'ro', + isa => 'Str', + default => '30b55f2e2e78056b16dbb15cb0899c2d', +); + +has lastfm => ( + is => 'ro', + isa => 'Net::LastFM', + lazy => 1, + default => sub { + my $self = shift; + Net::LastFM->new( + api_key => $self->api_key, + api_secret => '', + ); + }, +); + +sub track_count { + my $self = shift; + + return $self->lastfm->request( + method => 'user.getRecentTracks', + user => $self->user, + limit => 1, + )->{recenttracks}{'@attr'}{total}; +} + +sub tracks { + my $self = shift; + my (%params) = @_; + + $params{method} = 'user.getRecentTracks'; + $params{user} = $self->user; + $params{limit} ||= 200; + $params{page} ||= 1; + + return Data::Stream::Bulk::Callback->new( + callback => sub { + my $data = $self->lastfm->request(%params); + + return if $params{page} > $data->{recenttracks}{'@attr'}{totalPages}; + $params{page}++; + + return $data->{recenttracks}{track}; + }, + ); +} + +__PACKAGE__->meta->make_immutable; +no Moose; + +1; -- cgit v1.2.3-54-g00ecf