Import Amarok stats into Rhythmbox

LTjake on 2008-04-26T02:00:28

I've been using Ubuntu since Dapper was released. As Hardy is the next LTS release, I decided now would be a good time to blow the whole thing away and start fresh. At the same time, I thought I could give rid of some KDE-based software I've been using and stick to a strictly Gnome environment.

I've been using Amarok as my media player, but, as stated above, that doesn't jive with a pure Gnome setup. By default Rhythmbox is installed. I can import all of my tunes in no problem, but I'm missing some play stats.

Given my old Amarok database, which is just an SQLite database, and a Rhythmbox database, which is a simple XML file, with freshly imported tunes I was able to write a script to pull out some of my old data including: rating, import date, last play date and play count. NB: Rhythmbox ratings don't understand half-star ratings, though it doesn't complain.

Usage: import.pl rhythmdb.xml collection.db

use strict;
use warnings;

use XML::Simple;
use DBI;
use URI;

my $xml = shift;
my $data = XMLin( $xml, KeepRoot => 1, ForceContent => 1 );

my $dbh = DBI->connect( 'dbi:SQLite:dbname=' . shift, undef, undef );
my $sth = $dbh->prepare( 'select rating, playcounter, createdate, accessdate from statistics where url = ?;' );

for my $row ( @{ $data->{ rhythmdb }->{ entry } } ) {
    my $mp3 = URI->new( $row->{ location }->{ content } );

    next unless $mp3->scheme eq 'file';

    $sth->execute( '.' . $mp3->file );

    my $dbrow = $sth->fetchrow_hashref;
    $row->{ rating }->{ content } = $dbrow->{ rating } / 2;
    $row->{ 'play-count'}->{ content } =  ( $row->{ 'play-count'}->{ content } || 0 ) + $dbrow->{ 'playcounter' };
    $row->{ 'first-seen' }->{ content } = $dbrow->{ 'createdate' };
    $row->{ 'last-seen' }->{ content } = $dbrow->{ 'accessdate' };
}

XMLout( $data, KeepRoot => 1, XMLDecl => 1, OutputFile => $xml );