commit:efd91b4082873b2c85fc388b812a2d2d494614b9
author:Chip Black
committer:Chip Black
date:Thu May 29 03:54:55 2008 -0500
parents:5d03e0524f2bd9609d3d9a662f07c5e039607df4
Added db import
diff --git a/udb.pl b/udb.pl
line changes: +51/-3
index 04bdbcb..34a5839
--- a/udb.pl
+++ b/udb.pl
@@ -41,8 +41,24 @@ sub fullpath {
 	return "$pwd/$path";
 }
 
+my @blacklist;
+open BLACKLIST, "$uthome/blacklist";
+while (<BLACKLIST>) {
+	chomp;
+	push @blacklist, qr/^$_$/;
+}
+close BLACKLIST;
+
+sub blacklisted {
+	my $file = shift;
+	foreach my $i (@blacklist) {
+		return 1 if $file =~ $i;
+	}
+}
+
 sub db_add {
 	my $file = fullpath(shift);
+	return if blacklisted($file);
 	if (-l $file and !-e $file) {
 		print "$file seems to be a broken link.\n";
 		return;
@@ -118,9 +134,14 @@ sub db_rm {
 
 sub db_info {
 	my $file = fullpath(shift);
-	print "$file\n";
 	my $hash = Digest::MD5::md5_hex($file);
 	my $r = $dbh->selectrow_hashref(qq{SELECT * FROM tunes WHERE hash="$hash"});
+	if ($r) {
+		print "$file\n";
+	} else {
+		print "$file not in db\n";
+		return;
+	}
 	foreach my $k ('artist','title','album','genre','track','year','comment') {
 		print "\t$k\t\t$r->{$k}\n";
 	}
@@ -170,7 +191,7 @@ if ($ARGV[0] eq 'add') {
 	$sth->execute;
 	my $r;
 	while ($r = $sth->fetchrow_hashref()) {
-		if (! -e $r->{filename}) {
+		if (blacklisted($r->{filename}) || ! -e $r->{filename}) {
 			print "Purging $r->{filename}\n";
 			my $hash = $r->{hash};
 			$dbh->do(qq{DELETE FROM tunes WHERE hash="$hash"});
@@ -204,10 +225,37 @@ if ($ARGV[0] eq 'add') {
 		print scalar splice(@files, $n, 1), "\n";
 		last unless @files;
 	}
+} elsif ($ARGV[0] eq 'import') {
+	my $db = $ARGV[1];
+	unless ($db) {
+		print "You must specify a tunes db to import";
+		exit 1;
+	}
+	
+	my $insert = $dbh->prepare("INSERT INTO tunes (hash,filename,artist,title,album,genre,comment,track,year,length,bitrate,samplerate,channels) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
+	my $update = $dbh->prepare("UPDATE tunes SET filename=?, artist=?, title=?, album=?, genre=?, comment=?, track=?, year=?, length=?, bitrate=?, samplerate=?, channels=? WHERE hash=?");
+	my $import_dbh = DBI->connect("dbi:SQLite:dbname=$db","","");
+	my $import_sth = $import_dbh->prepare("select hash,filename,artist,title,album,genre,comment,track,year,length,bitrate,samplerate,channels from tunes");
+	$import_sth->execute;
+	$dbh->begin_work;
+	while (my @r = $import_sth->fetchrow_array) {
+		my ($c) = $dbh->selectrow_array("SELECT count(*) FROM tunes WHERE hash=?", undef, $r[0]);
+		#print "$c ",join('|', @r), "\n\n";
+		if ($c) {
+			$update->execute(@r[1..$#r], $r[0]);
+			print "updated $r[1]\n" || die "failed to update";
+		} else {
+			$insert->execute(@r) || die "failed to insert";
+			print "inserted $r[1]\n";
+		}
+	}
+	$import_sth->finish;
+	$import_dbh->disconnect;
+	$dbh->commit;
 } else {
 	my @where;
 	foreach (@ARGV) {
-		if (/^(artist|title|album|genre|comment)(==|=)(.*?)$/) {
+		if (/^(artist|title|album|genre|comment|filename)(==|=)(.*?)$/) {
 			my ($k, $op, $v) = ($1, $2, $3);
 			$v =~ s/"/""/g;
 			if ($op eq '==') {