| 1 | package Audio::DB::Parse::iTunes; | ||
| 2 | use strict; | ||
| 3 | use vars qw/@ISA/; | ||
| 4 | |||
| 5 | #@ISA = qw/Audio::DB::Build/; | ||
| 6 | @ISA; | ||
| 7 | |||
| 8 | # Subroutines for parsing iTunes XML-ified libraries | ||
| 9 | |||
| 10 | sub parse_library { | ||
| 11 | my ($name,$self,$library) = @_; | ||
| 12 | -e $library or die "$library does not exist: $!\n"; | ||
| 13 | open (XML,"<$library") or die "$library could not be opened: $!."; | ||
| 14 | $/ = "<dict>"; | ||
| 15 | while (<XML>) { | ||
| 16 | if (/Artist/i || /Album/i) { | ||
| 17 | my @lines = split("\n"); | ||
| 18 | my %data; | ||
| 19 | foreach (@lines) { | ||
| 20 | my ($key) = $_ =~ m|<key>(.*)</key>|; | ||
| 21 | my $value; | ||
| 22 | if ($key eq 'Compilation') { | ||
| 23 | $value = 'true'; | ||
| 24 | } else { | ||
| 25 | ($value) = $_ =~ /<key>.*<\/key><.*>(.*)<\/.*>/; | ||
| 26 | } | ||
| 27 | $data{$key} = $value; | ||
| 28 | } | ||
| 29 | next if $data{Artist} eq "Griffin Technology"; # itrip and other peripherals. | ||
| 30 | |||
| 31 | my $ph = '\N'; | ||
| 32 | my $tag = {}; | ||
| 33 | # Mimic handling of the get_tags data structure | ||
| 34 | $tag = { | ||
| 35 | title => $data{Name} || $ph, | ||
| 36 | artist => $data{Artist} || $ph, | ||
| 37 | duration => $data{Time} || $ph, | ||
| 38 | genre => $data{Genre} || $ph, | ||
| 39 | album => $data{Album} || $ph, | ||
| 40 | comment => $data{Comment} || $ph, | ||
| 41 | year => $data{Year} || $ph, | ||
| 42 | min => $data{'Total Time'} || $ph, | ||
| 43 | sec => $data{'Total Time'} || $ph, | ||
| 44 | seconds => $data{'Total Time'} || $ph, | ||
| 45 | lyrics => $data{Lyrics} || $ph, | ||
| 46 | track => $data{'Track Number'} || $ph, | ||
| 47 | total_tracks => $data{'Track Count'} || $ph, | ||
| 48 | bitrate => $data{'Bit Rate'} || $ph, | ||
| 49 | samplerate => $data{'Sample Rate'} || $ph, | ||
| 50 | composer => $data{Composer} || $ph, | ||
| 51 | discnumber => $data{'Disc Number'} || $ph, | ||
| 52 | disccount => $data{'Disc Count'} || $ph, | ||
| 53 | dateadded => $data{'Date Added'} || $ph, | ||
| 54 | datemodified => $data{'Date Modified'}|| $ph, | ||
| 55 | compilation => $data{Compilation} || $ph, | ||
| 56 | filename => $data{Location} || $ph, | ||
| 57 | filepath => $data{Location} || $ph, | ||
| 58 | filesize => $data{Size} || $ph, | ||
| 59 | tagtypes => $ph, | ||
| 60 | fileformat => $data{Kind} || $ph, | ||
| 61 | channels => $ph, | ||
| 62 | vbr => $ph, | ||
| 63 | rating => $data{Rating} || $ph, | ||
| 64 | playcount => $data{'Play Count'} || $ph, | ||
| 65 | playdate => $data{'Play Date'} || $ph, | ||
| 66 | }; | ||
| 67 | $self->cache_song($tag); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | ######### PURGING | ||
| 76 | |||
| 77 | sub aggregate_stats { | ||
| 78 | my ($self,$type,$hashref,$library,$album) = @_; | ||
| 79 | warn "$type -" . join('-',keys %$hashref) . scalar (keys %$hashref); | ||
| 80 | if ((scalar keys %$hashref > 1) && $type ne 'track') { | ||
| 81 | $self->{libraries}->{$library}->{albums}->{$album}->{$type} = "multiple $type" . 's assigned'; | ||
| 82 | } else { | ||
| 83 | my @temp = map {$_} keys %$hashref; | ||
| 84 | $self->{libraries}->{$library}->{albums}->{$album}->{$type} = $temp[0]; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | ############## | ||
| 88 | |||
| 89 | # Filter albums based on user-supplied params This type of approach | ||
| 90 | # lends itself well to finding artists with multiple genres | ||
| 91 | sub filter_albums { | ||
| 92 | my ($self,$lib,$requested_formats,$bitrate_minimum,$uniques) = @_; | ||
| 93 | my @rows; | ||
| 94 | |||
| 95 | $self->aggregate_songs_into_albums($lib,$uniques); | ||
| 96 | my @albums = $self->albums($lib); | ||
| 97 | foreach my $album_key (@albums) { | ||
| 98 | my $artist = $self->{libraries}->{$lib}->{albums}->{$album_key}->{artist}; | ||
| 99 | my $bitrate = $self->{libraries}->{$lib}->{albums}->{$album_key}->{bitrate}; | ||
| 100 | my $album = $self->{libraries}->{$lib}->{albums}->{$album_key}->{album}; | ||
| 101 | my $genre = $self->{libraries}->{$lib}->{albums}->{$album_key}->{genre}; | ||
| 102 | my $year = $self->{libraries}->{$lib}->{albums}->{$album_key}->{year}; | ||
| 103 | my $kind = $self->{libraries}->{$lib}->{albums}->{$album_key}->{kind}; | ||
| 104 | my $tracks = $self->{libraries}->{$lib}->{albums}->{$album_key}->{track}; | ||
| 105 | |||
| 106 | $artist = ($artist =~ /multiple\sartist/i) ? 'Various Artists' : $artist; | ||
| 107 | |||
| 108 | next unless (defined $requested_formats->{$kind}); # Ignore unless user has requested this format | ||
| 109 | next if ($bitrate < $bitrate_minimum || $bitrate =~ /multiple/); # Ignore if we are below the bitrate minimum | ||
| 110 | # Only save it if the total songs seen matches the track count | ||
| 111 | next unless ($tracks == scalar @{$self->{libraries}->{$lib}->{albums}->{$album_key}->{songs}}); | ||
| 112 | # Do we pass all the appropriate criteria? Create a row in the table | ||
| 113 | push @rows,[$artist,$album,$tracks,$bitrate,$kind,$year,$genre]; | ||
| 114 | } | ||
| 115 | return @rows; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | # If called with an opposite_library name, we are through comparing | ||
| 121 | # libraries and are simply calculating what is left. | ||
| 122 | sub summarize_by_song { | ||
| 123 | my ($self,$library,$status,$opposite_library) = @_; | ||
| 124 | $library ||= $self->get_name(); | ||
| 125 | $status ||= 'total'; | ||
| 126 | my (%unique_artists,%unique_albums); | ||
| 127 | foreach my $song_key ($self->songs($library)) { | ||
| 128 | my $song = $self->song($library,$song_key); | ||
| 129 | |||
| 130 | # Full collection aggregates | ||
| 131 | $self->{libraries}->{$library}->{stats}->{$status . '_songs'}++; | ||
| 132 | $self->{libraries}->{$library}->{stats}->{$status . '_size'} += $song->{Size}; | ||
| 133 | $self->{libraries}->{$library}->{stats}->{$status . '_time'} += $song->{'Total Time'}; | ||
| 134 | my $album = lc($song->{Album}); | ||
| 135 | my $artist = lc($song->{Artist}); | ||
| 136 | if ($opposite_library) { | ||
| 137 | $unique_artists{$artist}++ if (!defined $self->{libraries}->{$opposite_library}->{all_artists}->{$artist}); | ||
| 138 | $unique_albums{$album}++ if (!defined $self->{libraries}->{$opposite_library}->{all_albums}->{$album}); | ||
| 139 | } else { | ||
| 140 | $unique_artists{$artist}++; | ||
| 141 | $unique_albums{$album}++; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | # my @songs = $self->songs($); | ||
| 146 | # warn $name . ' ' . (scalar @songs) . ' ' . (scalar keys %unique_artists); | ||
| 147 | # warn $name . ' ' . (scalar keys %unique_albums); | ||
| 148 | # Add some various full collection totals | ||
| 149 | $self->{libraries}->{$library}->{stats}->{$status . '_albums'} = keys %unique_albums; | ||
| 150 | $self->{libraries}->{$library}->{stats}->{$status . '_artists'} = keys %unique_artists; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | # If parsing by songs, we might want to aggregate into albums | ||
| 156 | # Can optionally pass a list of songs (ie unique songs) instead | ||
| 157 | # of processing the entire list | ||
| 158 | sub aggregate_songs_into_albums { | ||
| 159 | my ($self,$library,$songs) = @_; | ||
| 160 | $library ||= $self->get_name(); | ||
| 161 | if ($songs) { | ||
| 162 | foreach my $song (@$songs) { | ||
| 163 | my $album_key = $self->create_album_key($song); | ||
| 164 | push(@{$self->{libraries}->{$library}->{albums}->{$album_key}->{songs}},$song); | ||
| 165 | # delete $self->{songs}->{$song_key}; # To save on some memory | ||
| 166 | } | ||
| 167 | } else { | ||
| 168 | $songs = $self->songs($library); | ||
| 169 | foreach my $song_key (@$songs) { | ||
| 170 | my $song = $self->song($library,$song_key); | ||
| 171 | my $album_key = $self->create_album_key($song); | ||
| 172 | push(@{$self->{libraries}->{$library}->{albums}->{$album_key}->{songs}},$song); | ||
| 173 | # delete $self->{songs}->{$song_key}; # To save on some memory | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | # Create some aggregate, per album stats | ||
| 178 | foreach my $album ($self->albums($library)) { | ||
| 179 | my (%bitrates,%genres,%years,%artists,%albums,%kinds,%tracks); | ||
| 180 | foreach my $song (@{$self->{libraries}->{$library}->{albums}->{$album}->{songs}}) { | ||
| 181 | # Per album aggregates | ||
| 182 | # Track total number of times songs on this album have been played | ||
| 183 | $self->{libraries}->{$library}->{albums}->{$album}->{total_play_count} += $song->{'Play Count'} if (defined $song->{'Play Count'}); | ||
| 184 | $self->{libraries}->{$library}->{albums}->{$album}->{total_size} += $song->{'Size'} if (defined $song->{Size}); | ||
| 185 | $self->{libraries}->{$library}->{albums}->{$album}->{total_time} += $song->{'Total Time'} if (defined $song->{'Total Time'}); | ||
| 186 | |||
| 187 | # Aggregate bitrates, genres, years | ||
| 188 | $bitrates{$song->{'Bit Rate'}}++; | ||
| 189 | $genres{$song->{Genre}}++; | ||
| 190 | $years{$song->{Year}}++; | ||
| 191 | $artists{$song->{Artist}}++; | ||
| 192 | $albums{$song->{Album}}++; | ||
| 193 | $kinds{$song->{Kind}}++; | ||
| 194 | $tracks{$song->{'Track Count'}}++; | ||
| 195 | } | ||
| 196 | $self->aggregate_stats('bitrate',\%bitrates,$library,$album); | ||
| 197 | $self->aggregate_stats('genre',\%genres,$library,$album); | ||
| 198 | $self->aggregate_stats('year',\%years,$library,$album); | ||
| 199 | $self->aggregate_stats('artist',\%artists,$library,$album); | ||
| 200 | $self->aggregate_stats('album',\%albums,$library,$album); | ||
| 201 | $self->aggregate_stats('kind',\%kinds,$library,$album); | ||
| 202 | $self->aggregate_stats('track',\%tracks,$library,$album); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | # Try to create a unique key for the album | ||
| 207 | sub create_album_key { | ||
| 208 | my ($self,$song) = @_; | ||
| 209 | my $album = $song->{Album}; | ||
| 210 | my $artist = eval {$song->{Compilation} } ? 'various_artists' : $song->{Artist}; | ||
| 211 | my $key = $album . '-' . $artist; | ||
| 212 | return $key; | ||
| 213 | } | ||
| 214 | |||
| 215 | # Create a song key that is highly likely to be unique. This will be | ||
| 216 | # track number - name - album We are not using artist becasue of | ||
| 217 | # possible differences in naming schemes (First Last versus Last, | ||
| 218 | # First) Even if an album has two songs of the same name, they will | ||
| 219 | # rarely be the same track | ||
| 220 | |||
| 221 | # This approach means that I will lose some songs by default in the analysis | ||
| 222 | sub create_song_key { | ||
| 223 | my ($self,$song) = @_; | ||
| 224 | my $album = lc($song->{Album}); | ||
| 225 | my $title = lc($song->{Name}); | ||
| 226 | # my $artist = lc($song->{Artist}); | ||
| 227 | my $track = eval { $song->{'Track Number'} } || '0'; | ||
| 228 | my $key = join('',$track,$title,$album); | ||
| 229 | # $key =~ s/[\s\t\(\)\.\,\\\/\*\?\.\!\-]//g; # Get rid of as many weird characters as possible. | ||
| 230 | $key =~ s/[\r\n\t\s\[\]\(\)\-\=\,\.\"\'\\\/\+\$\*\!\?]//g; | ||
| 231 | return $key; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub artist_genres { | ||
| 235 | my ($self,$library_name) = @_; | ||
| 236 | return $self->{libraries}->{$library_name}->{artist_genres}; | ||
| 237 | } | ||
| 238 | |||
| 239 | # Accessors | ||
| 240 | sub albums { | ||
| 241 | my ($self,$library) = @_; | ||
| 242 | my @albums = keys %{$self->{libraries}->{$library}->{albums}}; | ||
| 243 | return @albums; | ||
| 244 | } | ||
| 245 | |||
| 246 | # deprecated / not converted | ||
| 247 | # Return the songs of the album sorted by their track number | ||
| 248 | #sub songs_from_album { | ||
| 249 | # my ($self,$album) = @_; | ||
| 250 | # my @songs = sort {eval { $a->{'Track Number'}} <=> eval {$b->{'Track Number'}} } | ||
| 251 | # @{$self->{albums}->{$album}->{songs}}; | ||
| 252 | # return @songs; | ||
| 253 | #} | ||
| 254 | |||
| 255 | # Retrieve possible duplicates from this single library | ||
| 256 | sub single_library_duplicates { | ||
| 257 | my ($self,$library) = @_; | ||
| 258 | my @dups = @{$self->{libraries}->{$library}->{duplicates}}; | ||
| 259 | return @dups; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub songs { | ||
| 264 | my ($self,$library) = @_; | ||
| 265 | $library ||= $self->get_name(); | ||
| 266 | my @songs = keys %{$self->{libraries}->{$library}->{songs}}; | ||
| 267 | return @songs; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub song { | ||
| 271 | my ($self,$library,$songkey) = @_; | ||
| 272 | $library ||= $self->get_name(); | ||
| 273 | return $self->{libraries}->{$library}->{songs}->{$songkey}; | ||
| 274 | } | ||
| 275 | |||
| 276 | # use the all albums entry to get the total track count for the given album | ||
| 277 | # (I use this as a measure of whether a song is a single or not) | ||
| 278 | sub album_track_count { | ||
| 279 | my ($self,$library,$album) = @_; | ||
| 280 | return ($self->{libraries}->{$library}->{all_albums}->{$album}); | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | =pod | ||
| 285 | |||
| 286 | =head1 Audio::DB::Parse::iTunes.pm | ||
| 287 | |||
| 288 | Glean information on music files from an iTunes XML library file. | ||
| 289 | |||
| 290 | =head1 DESCRIPTION | ||
| 291 | |||
| 292 | Audio::DB::Parse::iTunes.pm is used internally by Audio::DB. It's | ||
| 293 | internal, private methods will be called when trying to create or update | ||
| 294 | a music database using the 'itunes' option: | ||
| 295 | |||
| 296 | $mp3->load_database(-dirs =>['/path/to/iTunes Music Library.xml/'], | ||
| 297 | -verbose => 100); | ||
| 298 | |||
| 299 | All methods of Audio::DB::Parse::iTunes are private (for now). You | ||
| 300 | will never need to interact with Audio::DB::Parse::iTunes objects | ||
| 301 | directly. | ||
| 302 | |||
| 303 | =head1 REQUIRES | ||
| 304 | |||
| 305 | =head1 EXPORTS | ||
| 306 | |||
| 307 | No methods are exported. | ||
| 308 | |||
| 309 | =head1 METHODS | ||
| 310 | |||
| 311 | No public methods available. | ||
| 312 | |||
| 313 | =head1 PRIVATE METHODS | ||
| 314 | |||
| 315 | TO BE COMPLETED SOON, I PROMISE! | ||
| 316 | |||
| 317 | =head1 AUTHOR | ||
| 318 | |||
| 319 | Copyright 2002-2004, Todd W. Harris <harris@cshl.org>. | ||
| 320 | |||
| 321 | This module is distributed under the same terms as Perl itself. Feel | ||
| 322 | free to use, modify and redistribute it as long as you retain the | ||
| 323 | correct attribution. | ||
| 324 | |||
| 325 | =head1 SEE ALSO | ||
| 326 | |||
| 327 | L<Audio::DB> | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | 1; |