1######################################################################
2package Net::Amazon::Property::Music;
3######################################################################
4use warnings;
5use strict;
6use base qw(Net::Amazon::Property);
7
8__PACKAGE__->make_accessor($_) for qw(album label media nummedia upc
9  ean studio publisher release_date binding);
10__PACKAGE__->make_array_accessor($_) for qw(artists tracks);
11
12##################################################
13sub new {
14##################################################
15    my($class, %options) = @_;
16
17    my $self = $class->SUPER::new(%options);
18    bless $self, $class; # Bless into this class
19
20    if(exists $options{xmlref}) {
21        $self->init_via_xmlref($options{xmlref});
22    }
23
24    return $self;
25}
26
27##################################################
28sub artist {
29##################################################
30    my($self, $nameref) = @_;
31
32    # Only return the first artist
33    return ($self->artists($nameref))[0];
34}
35
36##################################################
37sub init_via_xmlref {
38##################################################
39    my($self, $xmlref) = @_;
40
41    $self->SUPER::init_via_xmlref($xmlref);
42
43    my $ref = $xmlref->{ItemAttributes};
44
45    # It could either be a Creator (and?)/or an Artist
46    my @artists;
47    for my $artist (@{$ref->{Creator}}) {
48        push @artists, $artist->{content} if $artist->{Role} eq 'Performer';
49    }
50
51    for my $artist (@{$ref->{Artist}}) {
52        push @artists, $artist;
53    }
54    $self->artists(\@artists);
55
56    $self->album($ref->{Title});
57    $self->ean($ref->{EAN});
58    $self->label($ref->{Label});
59    $self->media($ref->{Binding});
60    $self->binding($ref->{Binding});
61    $self->nummedia($ref->{NumberOfDiscs});
62    $self->publisher($ref->{Publisher});
63    $self->release_date($ref->{ReleaseDate});
64    $self->studio($ref->{Studio});
65    $self->upc($ref->{UPC});
66
67    $self->NumMedia($ref->{NumberOfDiscs});
68
69    my @tracks;
70    for my $disc (@{$xmlref->{Tracks}->{Disc}}) {
71        for my $track (@{$disc->{Track}}) {
72            push @tracks, $track->{content};
73        }
74    }
75    $self->tracks(\@tracks);
76
77    my $year = 0;
78    if (defined $ref->{ReleaseDate}) {
79        $year =  (split(/\-/, $ref->{ReleaseDate}))[0];
80    }
81    $self->year($year);
82}
83
84##################################################
85sub as_string {
86##################################################
87    my($self) = @_;
88
89    return join('/', $self->artists) . ", " .
90           '"' . $self->album . '"' . ", " .
91           $self->year . ", " .
92           $self->_best_effort_price() . ", " .
93           $self->Asin;
94}
95
961;
97
98__END__
99
100=head1 NAME
101
102Net::Amazon::Property::Music - Class for pop CDs on amazon.com
103
104=head1 SYNOPSIS
105
106  use Net::Amazon;
107
108  # ...
109
110  if($resp->is_success()) {
111      for my $prop ($resp->properties) {
112          print join("/", $_->artists(), " ",
113                $_->album(), " ",
114                $_->label(), " ",
115                $_->year(), " ";
116                $_->upc(), " ";
117                $_->media(), " ";
118                $_->nummedia(), "\n";
119  }
120
121=head1 DESCRIPTION
122
123C<Net::Amazon::Property::Music> is derived from
124C<Net::Amazon::Property> and on top of the all-purpose
125methods the base class provides, it offers specialized accessors for
126popular music CD parameters.
127
128=head2 METHODS
129
130=over 4
131
132=item artists()
133
134Returns a list of the CD's artists. There's also a C<artist()> method
135which just returns the first artist.
136
137=item tracks()
138
139Returns a list of the CD's track titles.  Tracks are ordered as they appear on
140the media.  Track one is at offset zero in the tracks() list.  If there are
141multiple media then tracks are appended to the same list.  There is currently
142no way to determine which track belongs to which media.  (Amazon returns these
143data, but it is not used by Net::Amazon.)
144
145=item label()
146
147Returns the music label as a string.
148
149=item album()
150
151Returns the CD's title as a string.
152
153=item upc()
154
155Returns the CD's UPC as a string.
156
157=item media()
158
159Returns the CD's media type as a string.
160
161=item nummedia()
162
163Returns the CD's number of media (number of discs) as a string.
164Amazon doesn't always send this back, so if you get undef assume it
165is 1.
166
167=item new(xmlref => $xmlref)
168
169Initializes an object by passing a hash of hashes structure containing
170the XML data returned from the service. Usually, this is just used by
171C<Net::Amazon> internally to initialize objects for on backcoming
172data.
173
174=back
175
176Check out L<Net::Amazon::Property> for all-purpose accessors, like
177C<year>, C<OurPrice>, C<ListPrice>, etc.
178
179=head1 AUTHOR
180
181Mike Schilli, E<lt>m@perlmeister.comE<gt>
182
183=head1 THANKS
184
185Thanks to Padraic Renaghan E<lt>padraic@renaghan.com<gt> for adding
186the upc/media/nummedia fields.
187
188=head1 COPYRIGHT AND LICENSE
189
190Copyright 2003 by Mike Schilli E<lt>m@perlmeister.comE<gt>
191
192This library is free software; you can redistribute it and/or modify
193it under the same terms as Perl itself.
194
195=cut
196
197