You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.5 KiB

10 years ago
package Text::Playlist::XSPF;
10 years ago
use strict;
use warnings;
use feature qw(switch);
use utf8;
10 years ago
use base 'Text::Playlist';
10 years ago
use XML::XPath;
our $VERSION = "v0.1.0";
10 years ago
sub new {
my ($class) = @_;
10 years ago
return bless({ version => 0 }, $class);
10 years ago
}
10 years ago
sub parse {
my ($self, $text) = @_;
my @items = ();
10 years ago
10 years ago
my $xp = XML::XPath->new(xml => $text);
10 years ago
if (my $vers = $xp->find('/playlist/@version')) {
$self->{version} = $vers;
} else {
return "Can't find playlist version";
}
my $tracks = $xp->find('/playlist/trackList/track');
foreach my $track ($tracks->get_nodelist) {
my $item = {};
foreach my $child ($track->getChildNodes) {
next unless (ref $child eq 'XML::XPath::Node::Element');
my $value = $child->string_value();
given ($child->getName()) {
when (m/title/oi) { $item->{title} = $value; }
when (m/location/oi) { $item->{location} = $value; }
default { warn "garbage inside <track> element: $_ -> $value\n"; }
}
}
unless ($item->{title} and $item->{location}) {
warn "Missing mandatory 'title' or 'location' field\n";
next;
}
push @items, $item;
10 years ago
}
return wantarray ? @items : [ @items ];
10 years ago
}
10 years ago
sub dump { die("dump() unimplemented for XSPF format\n"); }
10 years ago
1;
__END__
=pod
10 years ago
=head1 NAME
Text::Playlist::XSPF - parser for 'xspf' format
=head1 SYNOPSIS
my $xspf = Text::Playlist::XSPF->new;
my @items = $xspf->load('/path/to/playlist.xspf');
foreach my $item (@items) {
<work with playlist items>
}
10 years ago
=head1 DESCRIPTION
Fast-and-dirty parser for xspf playlist.
Will be usefull if you're just want to read playlist or simple want
to convert such playlist to more common alternative.
10 years ago
=head2 C<load>
=head2 C<parse>
=head2 C<save>
=head2 C<dump>
For description of these methods see description in base class L<Text::Playlist>
=head1 Item format
Each parsed item has the following keys in hashref:
* location -- path or url, required. Following formats acceptable:
- Windows Path : file:///C:/music/foo.mp3
- Linux Path : file:///media/music/foo.mp3
- Relative Path : music/foo.mp3
- External URL : http://www.example.com/music/bar.ogg
* title -- title for given item, required
=head1 TODO
C<dump()> method still not implemented
=head1 SEE ALSO
L<XML::Toolkit> -- contains complete implementation for work with XSPF format
10 years ago
=head1 LINKS
L<https://en.wikipedia.org/wiki/XML_Shareable_Playlist_Format>
10 years ago
=head1 AUTHORS
Alex 'AdUser' Z <aduser@cpan.org>
10 years ago
10 years ago
=cut