lib/Mac/iTunes/Library.pm

filter: Statementstmt 13.5% 23 / 170 Branchbran 0.0% 0 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 12.8% 5 / 39 Podpod 91.3% 21 / 23 totaltotal 16.4% 49 / 298 CC 34 SCAR 28.9
CC
34
Cov
10%
add
90.0
_item
30.0
_time
12.0
CRAP 18.0
↑ Summary blib/lib/Mac/iTunes/Library/Item.pm »
1 package Mac::iTunes::Library;
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 require Exporter;
9 2 our @ISA = qw(Exporter);
10 2 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
11 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
12 2 our @EXPORT = qw( );
13  
14 2 our $VERSION = '1.0';
15  
16 =head1 NAME
17  
18 Mac::iTunes::Library - Perl extension for representing an iTunes library
19  
20 =head1 SYNOPSIS
21  
22 use Mac::iTunes::Library;
23  
24 my $library = Mac::iTunes::Library->new();
25 my $item = Mac::iTunes::Library::Item->new(
26 'Track ID' => 1,
27 'Name' => 'The Fooiest Song',
28 'Artist' => 'The Bar Band',
29 );
30 $library->add($item);
31 print "This library has only " . $library->num() . "item.\n";
32  
33 =head1 DESCRIPTION
34  
35 A data structure for representing an iTunes library.
36  
37 The library keeps track of the number of tracks by each artist
38 (a hash of Artist => num_tracks) and the number of songs in each genre
39 (Genre => num_tracks). Additionally, the total playcounts for each artist
40 (Artist => playcount_of_all_songs) and genre (Genre => playcount_of_all_songs)
41 are tallied. Finally, all of the items in the library are available, sorted
42 by artist.
43  
44 =head1 EXPORT
45  
46 None by default.
47  
48 =head1 METHODS
49  
50 =head2 new()
51  
52 Creates a new Mac::iTunes::Library object that can store
53 Mac::iTunes::Library::Item objects.
54  
55 =cut
56  
57 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
58 1 my $class = shift;
59 1 my $self = {
60 Num => 0, # Number of tracks in this library
61 Size => 0, # Size of all tracks
62 Time => 0, # Length of all tracks in milliseconds
63 Artist => {}, # Artist counts in tracks
64 PArtists => {}, # Artists counts by playcount
65 AlbumArtist => {}, # Album Artist counts in tracks
66 PAlbumArtists => {},# Album Artists counts by playcount
67 Genre => {}, # Genre counts by tracks
68 PGenre => {}, # Genre counts by playcount
69 Type => {}, # Track types, file or URL
70 Items => {},
71 Playlists => {},
72 };
73  
74 1 bless $self, $class;
75 1 return $self;
76 } #new
77  
78 # Clean up
79 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
80 # Nothing to do.
81 } #DESTROY
82  
83 =head2 version()
84  
85 Get/set the plist version number.
86  
87 =cut
88  
89 0 ▶sub version {
Subroutine version: not called CC=2 SCAR=17.9
Pod documented
90 0 my $self = shift;
91  
92 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
93 0 $self->{'plist'}{'version'} = shift;
94 } else {
95 0 return $self->{'plist'}{'version'};
96 }
97 } #version
98  
99 =head2 majorVersion()
100  
101 Get/set the Major Version number
102  
103 =cut
104  
105 0 ▶sub majorVersion {
Subroutine majorVersion: not called CC=2 SCAR=17.9
Pod documented
106 0 my $self = shift;
107  
108 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
109 0 $self->{'Major Version'} = shift;
110 } else {
111 0 return $self->{'Major Version'};
112 }
113 } #majorVersion
114  
115 =head2 minorVersion()
116  
117 Get/set the Minor Version number
118  
119 =cut
120  
121 0 ▶sub minorVersion {
Subroutine minorVersion: not called CC=2 SCAR=17.9
Pod documented
122 0 my $self = shift;
123  
124 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
125 0 $self->{'Minor Version'} = shift;
126 } else {
127 0 return $self->{'Minor Version'};
128 }
129 } #minorVersion
130  
131 =head2 applicationVersion()
132  
133 Get/set the Application Version number
134  
135 =cut
136  
137 0 ▶sub applicationVersion {
Subroutine applicationVersion: not called CC=2 SCAR=17.9
Pod documented
138 0 my $self = shift;
139  
140 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
141 0 $self->{'Application Version'} = shift;
142 } else {
143 0 return $self->{'Application Version'};
144 }
145 } #applicationVersion
146  
147 =head2 features()
148  
149 Get/set the Features attribute
150  
151 =cut
152  
153 0 ▶sub features {
Subroutine features: not called CC=2 SCAR=17.9
Pod documented
154 0 my $self = shift;
155  
156 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
157 0 $self->{'Features'} = shift;
158 } else {
159 0 return $self->{'Features'};
160 }
161 } #features
162  
163 =head2 showContentRatings()
164  
165 Get/set the Show Content Ratings attribute
166  
167 =cut
168  
169 0 ▶sub showContentRatings {
Subroutine showContentRatings: not called CC=2 SCAR=17.9
Pod documented
170 0 my $self = shift;
171  
172 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
173 0 $self->{'Show Content Ratings'} = shift;
174 } else {
175 0 return $self->{'Show Content Ratings'};
176 }
177 } #showContentRatings
178  
179 =head2 musicFolder()
180  
181 Get/set the Music Folder attribute
182  
183 =cut
184  
185 0 ▶sub musicFolder {
Subroutine musicFolder: not called CC=2 SCAR=17.9
Pod documented
186 0 my $self = shift;
187  
188 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
189 0 $self->{'Music Folder'} = shift;
190 } else {
191 0 return $self->{'Music Folder'};
192 }
193 } #musicFolder
194  
195 =head2 libraryPersistentID()
196  
197 Get/set the Library Persistent ID
198  
199 =cut
200  
201 0 ▶sub libraryPersistentID {
Subroutine libraryPersistentID: not called CC=2 SCAR=17.9
Pod documented
202 0 my $self = shift;
203  
204 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
205 0 $self->{'Library Persistent ID'} = shift;
206 } else {
207 0 return $self->{'Library Persistent ID'};
208 }
209 } #libraryPersistentID
210  
211 =head2 num()
212  
213 Get the number of tracks in the library
214  
215 =cut
216  
217 0 ▶sub num {
Subroutine num: not called CC=1 SCAR=6.9
Pod documented
218 0 my $self = shift;
219 0 return $self->{'Num'};
220 } #num
221  
222 # Increment the number of tracks in the library
223 0 ▶sub _num {
Subroutine _num: not called CC=1 SCAR=6.9
224 0 my $self = shift;
225 0 $self->{'Num'} += 1;
226 } #_num
227  
228 =head2 size()
229  
230 Get the total size of the library
231  
232 =cut
233  
234 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
235 0 my $self = shift;
236 0 return $self->{'Size'};
237 } #size
238  
239 # Add to the library's total size
240 0 ▶sub _size {
Subroutine _size: not called CC=2 SCAR=17.9
241 0 my $self = shift;
242 0 my $num = shift;
243 0 ▶ return unless ( defined $num );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 0 0
244 0 $self->{'Size'} += $num;
245 } #_size
246  
247 =head2 time()
248  
249 Get the total time of the library
250  
251 =cut
252  
253 0 ▶sub time {
Subroutine time: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 return $self->{'Time'};
256 } #time
257  
258 # Add to the library's total time
259 0 ▶sub _time {
Subroutine _time: not called CC=3 SCAR=24.8
260 0 my $self = shift;
261 0 my $time = shift;
262 0 ▶ return unless ( defined $time );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
263 0 ▶ $self->{'Time'} += $time if (defined $time);
Branch: 1 branch
BranchTrueFalseTotal
if defined $time 0 0 0
264 } #_time
265  
266 =head2 artist()
267  
268 Get the hash of the number of tracks for each artist.
269  
270 =cut
271  
272 0 ▶sub artist {
Subroutine artist: not called CC=1 SCAR=6.9
Pod documented
273 0 my $self = shift;
274 0 return %{$self->{'Artist'}};
275 } #artist
276  
277 # Increment the track count for the given artist
278 # ($artist)
279 0 ▶sub _artist {
Subroutine _artist: not called CC=2 SCAR=17.9
280 0 my $self = shift;
281 0 my $artist = shift;
282 0 ▶ return unless ( defined $artist );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $artist 0 0 0
283 0 $self->{'Artist'}{ $artist } += 1;
284 } #_artist
285  
286 =head2 partist()
287  
288 Get the hash of the number of plays (playcount) for each artist.
289  
290 =cut
291  
292 0 ▶sub partist {
Subroutine partist: not called CC=1 SCAR=6.9
Pod documented
293 0 my $self = shift;
294 0 return %{$self->{'PArtists'}};
295 } #partist
296  
297 # Increment the playcount for the given artist by a given amount
298 # ($artist, $num)
299 0 ▶sub _partist {
Subroutine _partist: not called CC=3 SCAR=24.8
300 0 my $self = shift;
301 0 my ($artist, $num) = @_;
302 0 ▶ return unless ( defined $artist );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $artist 0 0 0
303 0 ▶ return unless ( defined $num );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 0 0
304 0 $self->{'PArtists'}{ $artist } += $num;
305 } #_partist
306  
307 =head2 albumArtist()
308  
309 Get the hash of the number of tracks for each albumArtist.
310  
311 =cut
312  
313 0 ▶sub albumArtist {
Subroutine albumArtist: not called CC=1 SCAR=6.9
Pod documented
314 0 my $self = shift;
315 0 return %{$self->{'AlbumArtist'}};
316 } #albumArtist
317  
318 # Increment the track count for the given albumArtist
319 # ($albumArtist)
320 0 ▶sub _albumArtist {
Subroutine _albumArtist: not called CC=2 SCAR=17.9
321 0 my $self = shift;
322 0 my $albumArtist = shift;
323 0 ▶ return unless ( defined $albumArtist );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $albumArtist 0 0 0
324 0 $self->{'AlbumArtist'}{ $albumArtist } += 1;
325 } #_albumArtist
326  
327 =head2 palbumArtist()
328  
329 Get the hash of the number of plays (playcount) for each albumArtist.
330  
331 =cut
332  
333 0 ▶sub palbumArtist {
Subroutine palbumArtist: not called CC=1 SCAR=6.9
Pod documented
334 0 my $self = shift;
335 0 return %{$self->{'PAlbumArtists'}};
336 } #partist
337  
338 # Increment the playcount for the given albumArtist by a given amount
339 # ($albumArtist, $num)
340 0 ▶sub _palbumArtist {
Subroutine _palbumArtist: not called CC=3 SCAR=24.8
341 0 my $self = shift;
342 0 my ($albumArtist, $num) = @_;
343 0 ▶ return unless ( defined $albumArtist );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $albumArtist 0 0 0
344 0 ▶ return unless ( defined $num );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 0 0
345 0 $self->{'PAlbumArtists'}{ $albumArtist } += $num;
346 } #_palbumArtist
347  
348 =head2 genre()
349  
350 Get the hash of the number of tracks in each genre.
351  
352 =cut
353  
354 0 ▶sub genre {
Subroutine genre: not called CC=1 SCAR=6.9
Pod documented
355 0 my $self = shift;
356 0 return %{$self->{'Genre'}};
357 } #genre
358  
359 # Incrment the track count for the given genre
360 # ($genre)
361 0 ▶sub _genre {
Subroutine _genre: not called CC=2 SCAR=17.9
362 0 my $self = shift;
363 0 my $genre = shift;
364 0 ▶ return unless ( defined $genre );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $genre 0 0 0
365 0 $self->{'Genre'}{ $genre } += 1;
366 } #_genre
367  
368 =head2 pgenre()
369  
370 Get the hash of the number of plays (playcount) for each genre.
371  
372 =cut
373  
374 0 ▶sub pgenre {
Subroutine pgenre: not called CC=1 SCAR=6.9
Pod documented
375 0 my $self = shift;
376 0 return %{$self->{'PGenre'}};
377 } #pgenre
378  
379 # Increment the playcount for the given genre by a given amount
380 # ($genre, $playcount)
381 0 ▶sub _pgenre {
Subroutine _pgenre: not called CC=3 SCAR=24.8
382 0 my $self = shift;
383 0 my ($genre, $num) = @_;
384 0 ▶ return unless ( defined $genre );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $genre 0 0 0
385 0 ▶ return unless ( defined $num );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 0 0
386 0 $self->{'PGenre'}{ $genre } += $num;
387 } #_pgenre
388  
389 =head2 type()
390  
391 Get the hash of item types in the library
392  
393 =cut
394  
395 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
396 0 my $self = shift;
397 0 return %{$self->{'Type'}};
398 } #type
399  
400 # Increment the count of items of the given type
401 0 ▶sub _type {
Subroutine _type: not called CC=2 SCAR=17.9
402 0 my $self = shift;
403 0 my $type = shift;
404 0 ▶ return unless ( defined $type );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
405 0 $self->{'Type'}{ $type } += 1;
406 } #_type
407  
408 =head2 items()
409  
410 Get the hash of Items (Artist->Name->[item, item]) contained in the library;
411 artist names are the top level keys; accessing one gives you a hash-ref with
412 keys of song names and array-refs as values. Those array-refs contain
413 Mac::iTunes::Library::Item objects.
414  
415 =over 4
416  
417 =item Example traversal:
418  
419 # Assuming a previously created library
420 %items = $library->items();
421 while (($artist, $artistSongs) = each %items) {
422 while (($songName, artistSongItems) = each %$artistSongs) {
423 foreach my $item (@$artistSongItems) {
424 # Do something here to every item in the library
425 print $song->name() . "\n";
426 }
427 }
428 }
429  
430 =back
431  
432 =cut
433  
434 0 ▶sub items {
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
435 0 my $self = shift;
436 0 return %{$self->{'Items'}};
437 } #items
438  
439 # Add an item to our collection
440 0 ▶sub _item {
Subroutine _item: not called CC=5 SCAR=34.0
441 0 my $self = shift;
442 0 my $item = shift;
443  
444 0 my $artist = $item->artist();
445 0 my $name = $item->name();
446 0 ▶ $artist = 'Unknown' unless (defined $artist);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $artist 0 0 0
447 0 ▶ $name = 'Unknown' unless (defined $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
448  
449 # Finally, add it to our collection of items
450 0 ▶ if (exists $self->{'Items'}{$artist}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'Items'}{$artist}) { } 0 0 0
451 0 ▶ if (exists $self->{'Items'}{$artist}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'Items'}{$artist}{$name}) { } 0 0 0
452 0 push @{$self->{'Items'}{$artist}{$name}}, $item;
453 } else {
454 # First occurrence of this title
455 0 $self->{'Items'}{$artist}{$name} = [$item];
456 }
457 } else {
458 # First occurrence of this artist
459 0 $self->{'Items'}{$artist}{$name} = [$item];
460 }
461  
462 # print "creating item: " . $item->trackID . "\n";
463 0 $self->{'ItemsById'}{$item->trackID} = \$item; # TODO: this should be a ref
464  
465 } #_item
466  
467 =head2 add( Mac::iTunes::Library::Item )
468  
469 Add an item to the library
470  
471 =cut
472  
473 0 ▶sub add {
Subroutine add: not called CC=9 SCAR=45.0
Pod documented
474 0 my $self = shift;
475 0 my $item = shift;
476 0 ▶ return carp "Not enough arguments." unless (defined $item);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 0 0
477 0 ▶ return carp "Argument isn't a Mac::iTunes::Library::Item."
Branch: 1 branch
BranchTrueFalseTotal
unless $item->isa("Mac::iTunes::Library::Item") 0 0 0
478 unless ($item->isa("Mac::iTunes::Library::Item"));
479  
480 0 my $artist = $item->artist();
481 0 my $albumArtist = $item->albumArtist();
482 0 my $name = $item->name();
483 0 my $genre = $item->genre();
484 0 my $playCount = $item->playCount();
485  
486 # Deal with possible null values
487 0 ▶ $artist = 'Unknown' unless (defined $artist);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $artist 0 0 0
488 0 ▶ $albumArtist = 'Unknown' unless (defined $albumArtist);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $albumArtist 0 0 0
489 0 ▶ $name = 'Unknown' unless (defined $name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
490 0 ▶ $genre = 'Unknown' unless (defined $genre);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $genre 0 0 0
491 0 ▶ $playCount = 0 unless (defined $playCount);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $playCount 0 0 0
492  
493 # Tally up the item's data
494 0 $self->_artist($artist);
495 0 $self->_albumArtist($albumArtist);
496 0 $self->_genre($genre);
497 0 $self->_num();
498 0 ▶ $self->_size($item->size()) if (defined $item->size()); # Streams = null
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->size 0 0 0
499 0 $self->_time($item->totalTime());
500 0 $self->_partist($item->artist(), $item->playCount());
501 0 $self->_palbumArtist($item->albumArtist(), $item->playCount());
502 0 $self->_pgenre($item->genre(), $item->playCount());
503 0 $self->_type($item->trackType());
504 0 $self->_item($item);
505 } #add
506  
507 0 ▶sub addPlaylist {
Subroutine addPlaylist: not called CC=1 SCAR=6.9
Pod undocumented
508 0 my $self = shift;
509 0 my $playlist = shift;
510  
511 0 $self->{'Playlists'}->{ $playlist->{'Playlist ID'} } = $playlist;
512 }
513  
514 0 ▶sub playlists {
Subroutine playlists: not called CC=1 SCAR=6.9
Pod undocumented
515 0 my $self = shift;
516 0 return %{$self->{'Playlists'}};
517 }
518  
519 2 1;
520  
521 =head1 SEE ALSO
522  
523 L<Mac::iTunes::Library::XML>, L<Mac::iTunes::Library::Item>,
524 L<Mac::iTunes::Library::Playlist>
525  
526 =head1 AUTHOR
527  
528 Drew Stephens <drew@dinomite.net>, http://dinomite.net
529  
530 =head1 CONTRIBUTORS
531  
532 =over 4
533  
534 =item *
535  
536 Mark Grimes <mgrimes@cpan.org>, (L<http://www.peculiarities.com>)
537  
538 =item *
539  
540 Scott Lawrence (L<http://linkedin.com/in/scottdlawrence>)
541  
542 =item *
543  
544 Garrett Scott <garrett@gothik.org>, (L<http://www.gothik.org>)
545  
546 =item *
547  
548 Mark Allen <mrallen1@yahoo.com>
549  
550 =back
551  
552 =head1 SOURCE REPOSITORY
553  
554 http://mac-itunes.googlecode.com
555  
556 =head1 SVN INFO
557  
558 $Revision: 90 $
559  
560 =head1 COPYRIGHT AND LICENSE
561  
562 Copyright (C) 2007-2008 by Drew Stephens
563  
564 This library is free software; you can redistribute it and/or modify
565 it under the same terms as Perl itself, either Perl version 5.8.8 or,
566 at your option, any later version of Perl 5 you may have available.
567  
568 =cut
569 __END__
↑ Summary blib/lib/Mac/iTunes/Library/Item.pm »