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.

116 lines
2.2 KiB

10 years ago
package Text::Playlist::PLS;
10 years ago
use strict;
use warnings;
10 years ago
use base 'Text::Playlist';
our $VERSION = '0.02';
10 years ago
sub new {
my ($class) = @_;
10 years ago
return bless({ }, $class);
10 years ago
}
10 years ago
sub parse {
my ($self, $text) = @_;
10 years ago
10 years ago
my @lines = split /\r?\n/, $text;
my @items = ();
10 years ago
# safeguard
return "Not looks like playlist"
unless grep { $_ eq "[playlist]" } @lines;
my $count = 0;
foreach my $line (@lines) {
if ($line =~ m/(File|Title|Length)(\d+)\s*=\s*(.*)/oi) {
my ($key, $num, $value) = (lc($1), $2 - 1, $3);
$value =~ s/(^\s*|\s*$)//og;
$items[$num] //= {};
$items[$num]->{$key} = $value;
10 years ago
next;
}
if ($line =~ m/numberofentries\s*=\s*(\d+)/oi) {
$count = $1;
next;
}
}
warn "Number of entries not matches parsed items"
if ($count != scalar @items);
10 years ago
return wantarray ? @items : [ @items ];
10 years ago
}
sub dump {
my ($self, @items) = @_;
my $count = 0;
my @lines = ('[playlist]');
foreach my $item (@items) {
$count += 1;
foreach my $key (qw(file title length)) {
push @lines, sprintf("%s%d=%s", ucfirst($key), $count, $item->{$key});
}
}
splice(@lines, 1, 0, sprintf("numberofentries=%d", $count));
push @lines, "Version=2", "";
return join("\n", @lines);
}
10 years ago
1;
10 years ago
__END__
=pod
=head1 NAME
Text::Playlist::PLS - parser for 'pls' format
=head1 SYNOPSIS
my $pls = Text::Playlist::PLS->new;
my @items = $pls->load('/path/to/playlist.pls');
foreach my $item (@items) {
# <work with playlist items>
}
$pls->save('/path/to/new-playlist.pls', @items);
=head1 DESCRIPTION
Lightweight parser and generator for pls playlists.
Will be usefull if you're just want to read playlist, or convert playlist
to another format, or change playlist items by some way.
10 years ago
=head1 Methods
=head2 C<load>
10 years ago
=head2 C<parse>
10 years ago
=head2 C<save>
10 years ago
=head2 C<dump>
For description of these methods see description in base class L<Text::Playlist>
10 years ago
=head1 Item format
Each parsed item has the following keys in hashref:
10 years ago
* file -- path or url, required
* title -- title for given item, required
* length -- item duration in seconds, or -1 if unknown
10 years ago
=head1 AUTHORS
10 years ago
* Alex 'AdUser' Z <aduser@cpan.org>
10 years ago
=cut