diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-03-18 18:06:54 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-03-18 18:06:54 -0500 |
commit | eff476d3b7c5b8864f4a7646f001803b838365bb (patch) | |
tree | 79373cd0dbfe6997b981dae7b9c7d1367ae5a5f0 /bin | |
parent | 3c8165e6c5ac568d52534de22d7ab1525aebfa57 (diff) | |
download | lastfm-export-eff476d3b7c5b8864f4a7646f001803b838365bb.tar.gz lastfm-export-eff476d3b7c5b8864f4a7646f001803b838365bb.zip |
initial implementation
Diffstat (limited to 'bin')
-rw-r--r-- | bin/lastfm_export | 51 |
1 files changed, 51 insertions, 0 deletions
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; +} |