| 1 | package Mac::iTunes::Library::XML; | ||
| 2 | |||
| 3 | 2 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use Mac::iTunes::Library; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Mac::iTunes::Library::Item; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use Mac::iTunes::Library::Playlist; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use XML::Parser 2.36; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | require Exporter; | ||
| 14 | our @ISA = qw(Exporter); | ||
| 15 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | ||
| 16 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 17 | our @EXPORT = qw( ); | ||
| 18 | |||
| 19 | our $VERSION = '1.0'; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Mac::iTunes::Library::XML - Perl extension for parsing an iTunes XML library | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use Mac::iTunes::Library::XML; | ||
| 28 | |||
| 29 | my $library = Mac::iTunes::Library::XML->parse( 'iTunes Music Library.xml' ); | ||
| 30 | print "This library has only " . $library->num() . "item.\n"; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | A parser to read an iTunes XML library and create a Mac::iTunes::Library object. | ||
| 35 | |||
| 36 | =head2 NOTES ON iTUNES XML FORMAT | ||
| 37 | |||
| 38 | Whereas someone who understands how to use XML would write this: | ||
| 39 | |||
| 40 | <Playlists> | ||
| 41 | <Playlist> | ||
| 42 | <Name>Library</Name> | ||
| 43 | <Playlist ID>7</Playlist ID> | ||
| 44 | <Visible>false</Visible> | ||
| 45 | <Playlist Items> | ||
| 46 | <Track ID>14</Track ID> | ||
| 47 | <Track ID>21</Track ID> | ||
| 48 | <Track ID>28</Track ID> | ||
| 49 | </Playlist Items> | ||
| 50 | </Playlist> | ||
| 51 | </Playlists> | ||
| 52 | |||
| 53 | Instead, we get this from iTunes: | ||
| 54 | |||
| 55 | <key>Playlists</key> | ||
| 56 | <array> | ||
| 57 | <dict> | ||
| 58 | <key>Name</key><string>Library</string> | ||
| 59 | <key>Master</key><true/> | ||
| 60 | <key>Playlist ID</key><integer>201</integer> | ||
| 61 | <key>Playlist Persistent ID</key><string>707F6A2CE6E601F5</string> | ||
| 62 | <key>Visible</key><false/> | ||
| 63 | <key>All Items</key><true/> | ||
| 64 | <key>Playlist Items</key> | ||
| 65 | <array> | ||
| 66 | <dict> | ||
| 67 | <key>Track ID</key><integer>173</integer> | ||
| 68 | </dict> | ||
| 69 | <dict> | ||
| 70 | <key>Track ID</key><integer>175</integer> | ||
| 71 | </dict> | ||
| 72 | <dict> | ||
| 73 | <key>Track ID</key><integer>177</integer> | ||
| 74 | </dict> | ||
| 75 | </array> | ||
| 76 | </dict> | ||
| 77 | </array> | ||
| 78 | |||
| 79 | The iTunes XML format doesn't make it clear where the parser is in the library, | ||
| 80 | so to parser must keep track itself; this is done with the @stack array in | ||
| 81 | XML.pm, which is used to set $depth in each of the callback methods. | ||
| 82 | |||
| 83 | Here are the elements that can be found at any depth. The depths are indexed | ||
| 84 | with 0 being outside of any element (before the very first start_element call), | ||
| 85 | 1 would be within a single element (<plist> being the outermost of an iTunes | ||
| 86 | library file), 2 within the second element (<dict>), and so on. Note that | ||
| 87 | because the iTunes XML library format is so awesome, the name of a key | ||
| 88 | (contained within a <key> element, e.g. <key>Features</key>) occurs at the same | ||
| 89 | level as it's value (e.g. <integer>5</integer>). Those XML elements | ||
| 90 | (e.g. <key>, <integer) occur at some level n, but the data which we care about | ||
| 91 | (e.g. 'Features', 5) are contained at level n+1. | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =item * Zeroth | ||
| 96 | |||
| 97 | - <plist> element with version attribute | ||
| 98 | |||
| 99 | =item * First | ||
| 100 | |||
| 101 | - Outermost <dict> element | ||
| 102 | |||
| 103 | =item * Second | ||
| 104 | |||
| 105 | - <key> containing library metadata key name | ||
| 106 | - <integer> containing library metadata | ||
| 107 | - <string> containing library metadata | ||
| 108 | - <true /> containing library metadata | ||
| 109 | - <false /> containing library metadata | ||
| 110 | |||
| 111 | =item * Third | ||
| 112 | |||
| 113 | - Library metadata (major/minor version, application version, etc.) | ||
| 114 | - Tracks and Playlists keys | ||
| 115 | - <dict> containing library tracks | ||
| 116 | - <array> containing playlists | ||
| 117 | |||
| 118 | =item * Fourth | ||
| 119 | |||
| 120 | - <key> with track ID | ||
| 121 | - <dict> containing track data | ||
| 122 | |||
| 123 | =item * Fifth | ||
| 124 | |||
| 125 | - <key> containing track/playlist metdata key name | ||
| 126 | - <integer> containing track/playlist metdata key name | ||
| 127 | - <string> containing track/playlist metdata key name | ||
| 128 | - <date> containing track/playlist metdata key name | ||
| 129 | |||
| 130 | =item * Sixth | ||
| 131 | |||
| 132 | - <dict> containing a single playlist track | ||
| 133 | |||
| 134 | =item * Seventh | ||
| 135 | |||
| 136 | - <key> containing the string "Track ID" | ||
| 137 | - <integer> containing a track ID | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head1 EXPORT | ||
| 142 | |||
| 143 | None by default. | ||
| 144 | |||
| 145 | =head1 METHODS | ||
| 146 | |||
| 147 | =head2 parse( $libraryFile ) | ||
| 148 | |||
| 149 | Parses an iTunes XML library and returns a Mac::iTunes::Library object. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | # The current 'key' of an item information that we're in | ||
| 154 | my $curKey = undef; | ||
| 155 | # A Mac::iTunes::Library::Item that will be built and added to the library | ||
| 156 | my $item = undef; | ||
| 157 | # A Mac::iTunes::Library that will be built | ||
| 158 | my $library; | ||
| 159 | # Characters that we collect | ||
| 160 | my $characters = undef; | ||
| 161 | # Keep track of where we are; push on each element name as we hit it | ||
| 162 | my (@stack); | ||
| 163 | my ($inTracks, $inPlaylists, $inMajorVersion, $inMinorVersion, | ||
| 164 | $inApplicationVersion, $inFeatures, $inMusicFolder, | ||
| 165 | $inLibraryPersistentID) = undef; | ||
| 166 | |||
| 167 | sub parse { | ||
| 168 | my $self = shift; | ||
| 169 | my $xmlFile = shift; | ||
| 170 | $library = Mac::iTunes::Library->new(); | ||
| 171 | |||
| 172 | my $parser = XML::Parser->new( Handlers => { | ||
| 173 | Start => \&start_element, | ||
| 174 | End => \&end_element, | ||
| 175 | Char => \&characters, | ||
| 176 | }); | ||
| 177 | $parser->parsefile( $xmlFile ); | ||
| 178 | return $library; | ||
| 179 | } #parse | ||
| 180 | |||
| 181 | ### Parser start element | ||
| 182 | sub start_element { | ||
| 183 | my ($expat, $element, %attrs) = @_; | ||
| 184 | |||
| 185 | # Keep a trail of our depth | ||
| 186 | push @stack, $element; | ||
| 187 | # Note that our $depth is inside of the newly opened element | ||
| 188 | my $depth = scalar(@stack); | ||
| 189 | |||
| 190 | if ($depth == 0) { | ||
| 191 | } elsif ($depth == 1) { | ||
| 192 | # Hit the initial <plist version=""> tag | ||
| 193 | if (defined $attrs{'version'}) { | ||
| 194 | $library->version($attrs{'version'}); | ||
| 195 | } | ||
| 196 | } elsif ($depth == 2) { | ||
| 197 | } elsif ($depth == 3) { | ||
| 198 | if($inPlaylists) { | ||
| 199 | } else { | ||
| 200 | if (($element eq 'true') or ($element eq 'false')) { | ||
| 201 | $library->showContentRatings($element); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } elsif ($depth == 4) { | ||
| 205 | # We hit a new item in the XML; create a new object | ||
| 206 | if($inPlaylists) { | ||
| 207 | $item = Mac::iTunes::Library::Playlist->new() if ($element eq 'dict'); | ||
| 208 | } else { | ||
| 209 | $item = Mac::iTunes::Library::Item->new() if ($element eq 'dict'); | ||
| 210 | } | ||
| 211 | } elsif($depth == 5){ | ||
| 212 | } | ||
| 213 | } #start_element | ||
| 214 | |||
| 215 | ### Parser end element | ||
| 216 | sub end_element { | ||
| 217 | my ($expat, $element) = @_; | ||
| 218 | |||
| 219 | # Note that our $depth is still "inside" this ending element | ||
| 220 | my $depth = scalar(@stack); | ||
| 221 | # Hit a closing element; prune the trail | ||
| 222 | pop @stack; | ||
| 223 | |||
| 224 | if ($depth == 0) { # plist version | ||
| 225 | } elsif ($depth == 1) { # dict | ||
| 226 | } elsif ($depth == 2) { | ||
| 227 | } elsif ($depth == 3) { | ||
| 228 | # Exiting a major section | ||
| 229 | $inTracks = 0 if ($element eq 'dict'); | ||
| 230 | $inPlaylists = 0 if ($element eq 'array'); | ||
| 231 | |||
| 232 | if ($inMusicFolder and ($element eq 'string')) { | ||
| 233 | $library->musicFolder($characters); | ||
| 234 | $inMusicFolder = undef; | ||
| 235 | $curKey = undef; | ||
| 236 | $characters = undef; | ||
| 237 | } | ||
| 238 | } elsif ($depth == 4) { | ||
| 239 | # Ending an item; add it to the library and clean up | ||
| 240 | if( $inPlaylists ){ | ||
| 241 | if ($item) { | ||
| 242 | $library->addPlaylist($item); | ||
| 243 | } | ||
| 244 | } else { | ||
| 245 | if ($item) { | ||
| 246 | $library->add($item); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | $item = undef if ($element eq 'dict'); | ||
| 251 | } elsif ($depth == 5) { | ||
| 252 | # Set the attributes of the Mac::iTunes::Library::Item directly | ||
| 253 | if ( $element =~ /(integer|string|date|data)/ ) { | ||
| 254 | $item->{$curKey} = $characters; | ||
| 255 | $characters = undef; | ||
| 256 | $curKey = undef; | ||
| 257 | } elsif ( $element =~ /true/ ) { | ||
| 258 | $item->{$curKey} = 1; | ||
| 259 | $curKey = undef; | ||
| 260 | } elsif ( $element =~ /false/ ) { | ||
| 261 | $item->{$curKey} = 0; | ||
| 262 | $curKey = undef; | ||
| 263 | } | ||
| 264 | } elsif ($depth == 6){ | ||
| 265 | } elsif ($depth == 7){ | ||
| 266 | if ( $element =~ /(integer)/ ) { | ||
| 267 | # print "Adding $curKey => $characters\n"; | ||
| 268 | |||
| 269 | my $track = $library->{'ItemsById'}{$characters}; | ||
| 270 | if( ref $track and $$track ){ | ||
| 271 | $item->addItem( $$track ); | ||
| 272 | } else { | ||
| 273 | warn "Couldn't find track '$characters'\n"; | ||
| 274 | } | ||
| 275 | |||
| 276 | $curKey = undef; | ||
| 277 | $characters = undef; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } #end_element | ||
| 281 | |||
| 282 | ### Parser element contents | ||
| 283 | sub characters { | ||
| 284 | my ($expat, $string) = @_; | ||
| 285 | my $depth = scalar(@stack); | ||
| 286 | |||
| 287 | if ( $depth == 0 ) { # plist version | ||
| 288 | } elsif ( $depth == 1 ) { # dict | ||
| 289 | } elsif ( $depth == 2 ) { | ||
| 290 | } elsif ( $depth == 3 ) { | ||
| 291 | # Check the name of the element | ||
| 292 | if ( $stack[$#stack] eq 'key' ) { | ||
| 293 | # Lots of keys at this level | ||
| 294 | if ($string eq 'Major Version') { | ||
| 295 | $inMajorVersion = 1; | ||
| 296 | } elsif ( $string eq 'Minor Version' ) { | ||
| 297 | $inMinorVersion = 1; | ||
| 298 | } elsif ( $string eq 'Application Version' ) { | ||
| 299 | $inApplicationVersion = 1; | ||
| 300 | } elsif ( $string eq 'Features' ) { | ||
| 301 | $inFeatures = 1; | ||
| 302 | } elsif ( $string eq 'Music Folder' ) { | ||
| 303 | $inMusicFolder = 1; | ||
| 304 | } elsif ( $string eq 'Library Persistent ID' ) { | ||
| 305 | $inLibraryPersistentID = 1; | ||
| 306 | } elsif ( $string eq 'Tracks' ) { | ||
| 307 | $inTracks = 1; | ||
| 308 | } elsif ( $string eq 'Playlists' ) { | ||
| 309 | $inPlaylists = 1; | ||
| 310 | } | ||
| 311 | } elsif ( $stack[$#stack] =~ /(integer|string|true|false)/ ) { | ||
| 312 | # TODO This is assumes that each of these come as a single chunk | ||
| 313 | if ($inMajorVersion) { | ||
| 314 | $library->majorVersion($string); | ||
| 315 | $inMajorVersion = undef; | ||
| 316 | } elsif ($inMinorVersion) { | ||
| 317 | $library->minorVersion($string); | ||
| 318 | $inMinorVersion = undef; | ||
| 319 | } elsif ($inApplicationVersion) { | ||
| 320 | $library->applicationVersion($string); | ||
| 321 | $inApplicationVersion = undef; | ||
| 322 | } elsif ($inFeatures) { | ||
| 323 | $library->features($string); | ||
| 324 | $inFeatures = undef; | ||
| 325 | } elsif ($inMusicFolder) { | ||
| 326 | # The music folder could be long; buffer it. | ||
| 327 | $characters .= $string; | ||
| 328 | } elsif ($inLibraryPersistentID) { | ||
| 329 | $library->libraryPersistentID($string); | ||
| 330 | $inLibraryPersistentID = undef; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } elsif ( $depth == 4 ) { | ||
| 334 | } elsif ( $depth == 5 ) { | ||
| 335 | if ( $stack[$#stack] eq 'key' ) { | ||
| 336 | # Grab the key's name; Normally comes in a single chunk, but accept multiple chunks | ||
| 337 | $curKey .= $string; | ||
| 338 | } elsif ( $stack[$#stack] =~ /(integer|string|date|data)/ ) { | ||
| 339 | # Append it to the characters that we've gathered so far | ||
| 340 | $characters .= $string; | ||
| 341 | } | ||
| 342 | } elsif ( $depth == 6 ) { | ||
| 343 | } elsif ( $depth == 7 ) { | ||
| 344 | if ( $stack[$#stack] eq 'key' ) { | ||
| 345 | # Grab the key's name; Normally comes in a single chunk, but accept multiple chunks | ||
| 346 | $curKey .= $string; | ||
| 347 | } elsif ( $stack[$#stack] =~ /(integer|string|date)/ ) { | ||
| 348 | # Append it to the characters that we've gathered so far | ||
| 349 | $characters .= $string; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } #characters | ||
| 353 | |||
| 354 | # Clean up | ||
| 355 | sub DESTROY { | ||
| 356 | # Nothing to do. | ||
| 357 | } #DESTROY | ||
| 358 | |||
| 359 | 1; | ||
| 360 | |||
| 361 | =head1 SEE ALSO | ||
| 362 | |||
| 363 | L<Mac::iTunes::Library::Item>, L<Mac::iTunes::Library>, | ||
| 364 | L<Mac::iTunes::Library::Playlist> | ||
| 365 | |||
| 366 | =head1 AUTHOR | ||
| 367 | |||
| 368 | Drew Stephens <drew@dinomite.net>, http://dinomite.net | ||
| 369 | |||
| 370 | =head1 CONTRIBUTORS | ||
| 371 | |||
| 372 | =over4 | ||
| 373 | |||
| 374 | =item * | ||
| 375 | |||
| 376 | Mark Grimes <mgrimes@cpan.org>, L<http://www.peculiarities.com> | ||
| 377 | |||
| 378 | =item * | ||
| 379 | |||
| 380 | Garrett Scott <garrett@gothik.org>, L<http://www.gothik.org> | ||
| 381 | |||
| 382 | =back | ||
| 383 | |||
| 384 | =head1 SOURCE REPOSITORY | ||
| 385 | |||
| 386 | http://mac-itunes.googlecode.com | ||
| 387 | |||
| 388 | =head1 SVN INFO | ||
| 389 | |||
| 390 | $Revision: 90 $ | ||
| 391 | |||
| 392 | =head1 COPYRIGHT AND LICENSE | ||
| 393 | |||
| 394 | Copyright (C) 2007-2008 by Drew Stephens | ||
| 395 | |||
| 396 | This library is free software; you can redistribute it and/or modify | ||
| 397 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 398 | at your option, any later version of Perl 5 you may have available. | ||
| 399 | |||
| 400 | =cut | ||
| 401 | __END__ | ||