Browse Source

* change load()/dump() signature

master
Alex 'AdUser' Z 10 years ago
parent
commit
addb638b65
  1. 12
      lib/Text/Playlist/M3U.pm
  2. 14
      lib/Text/Playlist/PLS.pm
  3. 5
      lib/Text/Playlist/XSPF.pm
  4. 5
      t/m3u.t
  5. 5
      t/pls.t

12
lib/Text/Playlist/M3U.pm

@ -10,14 +10,14 @@ our $VERSION = 0.1;
sub new {
my ($class) = @_;
return bless({ items => [], attrs => {}, }, $class);
return bless({ attrs => {}, }, $class);
}
sub parse {
my ($self, $text) = @_;
my @lines = split /\r?\n/, $text;
$self->{items} = [];
my @items = ();
# safeguard
return "Not looks like playlist"
@ -50,12 +50,12 @@ sub parse {
# path / url
if ($line) {
$item->{file} = $line;
push @{$self->{items}}, $item;
push @items, $item;
undef $item;
}
}
return $self->items;
return wantarray ? @items : [ @items ];
}
sub _parse_attrs {
@ -83,11 +83,11 @@ sub _dump_attrs {
}
sub dump {
my ($self) = @_;
my ($self, @items) = @_;
my @lines = ();
push @lines, sprintf('#EXTM3U%s', $self->_dump_attrs($self->{attrs}));
foreach my $item ($self->items) {
foreach my $item (@items) {
push @lines, sprintf("#EXTINF:%s%s,%s", $item->{duration},
$self->_dump_attrs($item->{attrs}), $item->{title});
push @lines, $item->{file};

14
lib/Text/Playlist/PLS.pm

@ -35,7 +35,7 @@ sub parse {
my ($self, $text) = @_;
my @lines = split /\r?\n/, $text;
$self->{items} = [];
my @items = ();
# safeguard
return "Not looks like playlist"
@ -46,8 +46,8 @@ sub parse {
if ($line =~ m/(File|Title|Length)(\d+)\s*=\s*(.*)/oi) {
my ($key, $num, $value) = (lc($1), $2 - 1, $3);
$value =~ s/(^\s*|\s*$)//og;
$self->{items}->[$num] //= {};
$self->{items}->[$num]->{$key} = $value;
$items[$num] //= {};
$items[$num]->{$key} = $value;
next;
}
if ($line =~ m/numberofentries\s*=\s*(\d+)/oi) {
@ -57,17 +57,17 @@ sub parse {
}
warn "Number of entries not matches parsed items"
if ($count != scalar @{$self->{items}});
if ($count != scalar @items);
return $self->items;
return wantarray ? @items : [ @items ];
}
sub dump {
my ($self) = @_;
my ($self, @items) = @_;
my $count = 0;
my @lines = ('[playlist]');
foreach my $item ($self->items) {
foreach my $item (@items) {
$count += 1;
foreach my $key (qw(file title length)) {
push @lines, sprintf("%s%d=%s", ucfirst($key), $count, $item->{$key});

5
lib/Text/Playlist/XSPF.pm

@ -36,6 +36,7 @@ sub add {
sub parse {
my ($self, $text) = @_;
my @items = ();
my $xp = XML::XPath->new(xml => $text);
if (my $vers = $xp->find('/playlist/@version')) {
@ -60,10 +61,10 @@ sub parse {
warn "Missing mandatory 'title' or 'location' field\n";
next;
}
push @{ $self->{items} }, $item;
push @items, $item;
}
return $self->items;
return wantarray ? @items : [ @items ];
}
sub dump { die("dump() unimplemented for XSPF format\n"); }

5
t/m3u.t

@ -36,9 +36,10 @@ my $out = [{
duration => '-1',
}];
is_deeply(scalar $pls->load($path), $out, "Loading test playlist");
my @items = $pls->load($path);
is_deeply(\@items, $out, "Loading test playlist");
unlink $path;
is($text, $pls->dump);
is($text, $pls->dump(@items));
exit 0;

5
t/pls.t

@ -28,7 +28,8 @@ my $out = [{
title => '(#1 - 1/1) Radio Broadcast',
length => '-1',
}];
is_deeply(scalar $pls->load($path), $out, "Loading test playlist");
my @items = $pls->load($path);
is_deeply(\@items, $out, "Loading test playlist");
unlink $path;
@ -41,6 +42,6 @@ Length1=-1
Version=2
PLS
is($pls->dump, $text);
is($pls->dump(@items), $text);
exit 0;

Loading…
Cancel
Save