Commits

Carey Tilden committed 9261fe4

Separated Library module into two parts, one that just reads the library data and one that does stuff with it

  • Participants
  • Parent commits 3f89d38

Comments (0)

Files changed (10)

File bin/check-library-consistency.pl

 use strict;
 use warnings;
 use FindBin;
-use List::Util 'max';
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'load_aliases', 'make_key', 'resolve_alias';
+use Library::Read;
 
 $| = 1;
 
 my %track_counts_of;
 my %tracks_from;
 
-Library->load_aliases('data/aliases.txt');
+load_aliases('data/aliases.txt');
 
 for (@ARGV) {
-    my $lib = Library->new(dbfile => $_);
+    my $lib = Library::Read->new(dbfile => $_);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
         next unless defined $track_number;
 
         my $album_key = join "; ",
-            Library->make_key(Library->resolve_alias(album_artist => $album_artist)),
-            Library->make_key(Library->resolve_alias(album        => $album       ));
+            make_key(resolve_alias(album_artist => $album_artist)),
+            make_key(resolve_alias(album        => $album       ));
         $track_number = sprintf "%02d", $track_number;
 
         if (exists $tracks_from{$album_key}{$track_number}) {

File bin/find-equals.pl

 use FindBin;
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'load_aliases', 'make_key', 'resolve_alias';
+use Library::Read;
 
 usage() unless @ARGV >= 2;
 
 my %info_for;
 my %paths_with;
 
-Library->load_aliases('data/aliases.txt');
+load_aliases('data/aliases.txt');
 
 for my $source (@sources) {
-    my $lib = Library->new(dbfile => $source);
+    my $lib = Library::Read->new(dbfile => $source);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
     $lib->db_close();
 }
 
-my $lib = Library->new(dbfile => $destination);
+my $lib = Library::Read->new(dbfile => $destination);
 $lib->db_open();
 
 while (my $item = $lib->fetch_item()) {
 
     for (qw(artist album name)) {
         next unless defined $item->{$_};
-        $value_of{$_} = Library->make_key(Library->resolve_alias($_, $item->{$_}));
+        $value_of{$_} = make_key(resolve_alias($_, $item->{$_}));
     }
 
     if (defined $item->track_number) {

File bin/find-tracks.pl

 use Getopt::Long;
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'load_aliases', 'make_key', 'resolve_alias';
+use Library::Read;
 
 my $DEBUG = 0;
 my $COMPARE = 0;
 my %looking_for;
 my %info_for;
 
-Library->load_aliases('data/aliases.txt');
+load_aliases('data/aliases.txt');
 
 my %source_item_with;
 
-my $lib = Library->new(dbfile => $destination);
+my $lib = Library::Read->new(dbfile => $destination);
 $lib->db_open();
 
 while (my $item = $lib->fetch_item()) {
 my %looked_at;
 
 for my $source (@sources) {
-    my $lib = Library->new(dbfile => $source);
+    my $lib = Library::Read->new(dbfile => $source);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
         return;
     }
 
-    my $item_key   = join ";", map { Library->make_key(Library->resolve_alias(@$_)) } @item_key_values;
-    my $sought_key = join ";", map { Library->make_key(Library->resolve_alias(@$_)) } @sought_key_values;
+    my $item_key   = join ";", map { make_key(resolve_alias(@$_)) } @item_key_values;
+    my $sought_key = join ";", map { make_key(resolve_alias(@$_)) } @sought_key_values;
     my $item_desc  = join "; ", map { $_->[1] } @item_key_values;
 
     return $item_key, $sought_key, $item_desc;

File bin/print-common-artists.pl

 use Getopt::Long;
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'make_key';
+use Library::Read;
 
 usage() unless @ARGV == 2;
 
 my ($lib1file, $lib2file) = @ARGV;
 
+load_aliases('data/aliases.txt');
+
 my %lib1_has;
 
-my $lib1 = Library->new(dbfile => $lib1file);
+my $lib1 = Library::Read->new(dbfile => $lib1file);
 $lib1->db_open();
 
 while (my $item = $lib1->fetch_item()) {
     next unless defined $item->artist;
     my $artist = $item->artist;
-    my $artist_key = Library->make_key($artist);
+    my $artist_key = make_key($artist);
     $lib1_has{$artist_key} = $artist;
 }
 
 $lib1->db_close();
 
-my $lib2 = Library->new(dbfile => $lib2file);
+my $lib2 = Library::Read->new(dbfile => $lib2file);
 $lib2->db_open();
 
 while (my $item = $lib2->fetch_item()) {
     next unless defined $item->artist;
     my $artist = $item->artist;
-    my $artist_key = Library->make_key($artist);
+    my $artist_key = make_key($artist);
     if ($lib1_has{$artist_key}) {
         my $artist = delete $lib1_has{$artist_key};
         print "$artist\n";

File bin/print-conflicts.pl

 use Getopt::Long;
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'make_key';
+use Library::Read;
 
 my $OTHERFIELD = "album";
 GetOptions(
 my %paths_of;
 
 for (@ARGV) {
-    my $lib = Library->new(dbfile => $_);
+    my $lib = Library::Read->new(dbfile => $_);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
         next unless defined $item->artist;
         next unless defined $item->{$OTHERFIELD};
 
-        my $full_key = join ":",   map { Library->make_key($item->{$_}) } 'artist', $OTHERFIELD;
+        my $full_key = join ":",   map { make_key($item->{$_}) } 'artist', $OTHERFIELD;
         my $label    = join " - ", map {                   $item->{$_}  } 'artist', $OTHERFIELD;
 
         $labels_of{$full_key}{$label}++;

File bin/print-duplicates.pl

 use Getopt::Long;
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'make_key';
+use Library::Read;
 
 my @OTHERFIELDS = ('album', 'name');
 
 my %paths_of;
 
 for (@ARGV) {
-    my $lib = Library->new(dbfile => $_);
+    my $lib = Library::Read->new(dbfile => $_);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
 
         no warnings 'uninitialized';
 
-        my $full_key = join ":",   map { Library->make_key($item->{$_}) } 'artist', @OTHERFIELDS;
+        my $full_key = join ":",   map { make_key($item->{$_}) } 'artist', @OTHERFIELDS;
         my $label    = join " - ", map {                   $item->{$_}  } 'artist', @OTHERFIELDS;
 
         use warnings;

File bin/print-incompletes.pl

 use List::Util 'max';
 
 use lib "$FindBin::RealBin/../lib";
-use Library;
+use Library::Analyze 'make_key';
+use Library::Read;
 
 $| = 1;
 
 my %tracks_from;
 
 for (@ARGV) {
-    my $lib = Library->new(dbfile => $_);
+    my $lib = Library::Read->new(dbfile => $_);
     $lib->db_open();
 
     while (my $item = $lib->fetch_item()) {
         next unless defined $track_count;
         next unless defined $track_number;
 
-        my $album_key = join "; ", Library->make_key($artist), Library->make_key($album);
+        my $album_key = join "; ", make_key($artist), make_key($album);
         $track_number = sprintf "%02d", $track_number;
 
         $display_name_of{$album_key} = "$artist; $album";

File lib/Library.pm

-package Library;
-use strict;
-use warnings;
-use DBI;
-use Data::AsObject 'dao';
-use Moose;
-
-has 'dbfile' => (is => 'ro', isa => 'Str', required => 1);
-has 'dbh'    => (is => 'rw', isa => 'DBI::db');
-has 'items'  => (is => 'rw', isa => 'DBI::st');
-
-sub db_open {
-    my ($self) = @_;
-
-    my $dbfile = $self->dbfile;
-
-    $self->dbh(
-        DBI->connect(
-            "dbi:SQLite:dbname=$dbfile", "", "",
-            { RaiseError => 1, AutoCommit => 0 }
-        )
-    );
-
-    $self->items(
-        $self->dbh->prepare(q{
-            SELECT * FROM itunes_library
-            ORDER BY COALESCE(album_artist, artist)
-                   , album
-                   , track_number
-                   , artist
-                   , name
-        })
-    );
-
-    $self->items->execute();
-}
-
-sub db_close {
-    my ($self) = @_;
-    $self->items->finish();
-    $self->dbh->disconnect();
-}
-
-sub fetch_item {
-    my ($self) = @_;
-
-    my $row = $self->items->fetchrow_hashref();
-
-    return unless $row;
-
-    for ($row->{location}) {
-        s{%20}{ }g;
-        s{file://localhost}{};
-        #s{/Users/Carey/Music/iTunes/iTunes Music}{[Carey]};
-        #s{/Volumes/Tropicalia/iTunes}{[Jason]};
-    }
-
-    if (defined $row->{total_time}) {
-        $row->{time_seconds} = $row->{total_time} / 1000;
-    }
-
-    return dao { %$row };
-}
-
-### Class methods ###
-
-my %value_of;
-
-sub make_key {
-    my ($class, $input) = @_;
-
-    return unless defined $input;
-
-    my $output = lc $input;
-    $output =~ s{\s+}{}g;
-    $output =~ s{[^a-z0-9]}{.}g;
-
-    return $output;
-}
-
-sub load_aliases {
-    my ($class, $file) = @_;
-
-    open my $fh, "<", $file
-        or die "$!: reading $file\n";
-
-    while (<$fh>) {
-        chomp;
-
-        my ($fields, $alias, $value) = split /\t/, $_, 3;
-
-        for my $field (split ',', $fields) {
-            if (exists $value_of{$field}{$alias}) {
-                warn "Redefined alias $alias from $value_of{$field}{$alias} to $value\n";
-            }
-
-            $value_of{$field}{$alias} = $value;
-        }
-    }
-}
-
-sub resolve_alias {
-    my ($class, $field, $alias) = @_;
-
-    if (exists $value_of{$field}{$alias}) {
-        return $value_of{$field}{$alias};
-    }
-    else {
-        return $alias;
-    }
-}
-
-1;

File lib/Library/Analyze.pm

+package Library::Analyze;
+use strict;
+use warnings;
+
+use base 'Exporter';
+our @EXPORT_OK = qw(
+    load_aliases
+    make_key
+    resolve_alias
+);
+
+my %value_of;
+
+sub make_key {
+    my ($input) = @_;
+
+    return unless defined $input;
+
+    my $output = lc $input;
+    $output =~ s{\s+}{}g;
+    $output =~ s{[^a-z0-9]}{.}g;
+
+    return $output;
+}
+
+sub load_aliases {
+    my ($file) = @_;
+
+    open my $fh, "<", $file
+        or die "$!: reading $file\n";
+
+    while (<$fh>) {
+        chomp;
+
+        my ($fields, $alias, $value) = split /\t/, $_, 3;
+
+        for my $field (split ',', $fields) {
+            if (exists $value_of{$field}{$alias}) {
+                warn "Redefined alias $alias from $value_of{$field}{$alias} to $value\n";
+            }
+
+            $value_of{$field}{$alias} = $value;
+        }
+    }
+}
+
+sub resolve_alias {
+    my ($field, $alias) = @_;
+
+    if (exists $value_of{$field}{$alias}) {
+        return $value_of{$field}{$alias};
+    }
+    else {
+        return $alias;
+    }
+}
+
+1;

File lib/Library/Read.pm

+package Library::Read;
+use strict;
+use warnings;
+use DBI;
+use Data::AsObject 'dao';
+use Moose;
+
+has 'dbfile' => (is => 'ro', isa => 'Str', required => 1);
+has 'dbh'    => (is => 'rw', isa => 'DBI::db');
+has 'items'  => (is => 'rw', isa => 'DBI::st');
+
+sub db_open {
+    my ($self) = @_;
+
+    my $dbfile = $self->dbfile;
+
+    $self->dbh(
+        DBI->connect(
+            "dbi:SQLite:dbname=$dbfile", "", "",
+            { RaiseError => 1, AutoCommit => 0 }
+        )
+    );
+
+    $self->items(
+        $self->dbh->prepare(q{
+            SELECT * FROM itunes_library
+            ORDER BY COALESCE(album_artist, artist)
+                   , album
+                   , track_number
+                   , artist
+                   , name
+        })
+    );
+
+    $self->items->execute();
+}
+
+sub db_close {
+    my ($self) = @_;
+    $self->items->finish();
+    $self->dbh->disconnect();
+}
+
+sub fetch_item {
+    my ($self) = @_;
+
+    my $row = $self->items->fetchrow_hashref();
+
+    return unless $row;
+
+    for ($row->{location}) {
+        s{%20}{ }g;
+        s{file://localhost}{};
+        #s{/Users/Carey/Music/iTunes/iTunes Music}{[Carey]};
+        #s{/Volumes/Tropicalia/iTunes}{[Jason]};
+    }
+
+    if (defined $row->{total_time}) {
+        $row->{time_seconds} = $row->{total_time} / 1000;
+    }
+
+    return dao { %$row };
+}
+
+1;