Browse Source

* change load()/dump() signature

master
Alex 'AdUser' Z 9 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 { sub new {
my ($class) = @_; my ($class) = @_;
return bless({ items => [], attrs => {}, }, $class); return bless({ attrs => {}, }, $class);
} }
sub parse { sub parse {
my ($self, $text) = @_; my ($self, $text) = @_;
my @lines = split /\r?\n/, $text; my @lines = split /\r?\n/, $text;
$self->{items} = []; my @items = ();
# safeguard # safeguard
return "Not looks like playlist" return "Not looks like playlist"
@ -50,12 +50,12 @@ sub parse {
# path / url # path / url
if ($line) { if ($line) {
$item->{file} = $line; $item->{file} = $line;
push @{$self->{items}}, $item; push @items, $item;
undef $item; undef $item;
} }
} }
return $self->items; return wantarray ? @items : [ @items ];
} }
sub _parse_attrs { sub _parse_attrs {
@ -83,11 +83,11 @@ sub _dump_attrs {
} }
sub dump { sub dump {
my ($self) = @_; my ($self, @items) = @_;
my @lines = (); my @lines = ();
push @lines, sprintf('#EXTM3U%s', $self->_dump_attrs($self->{attrs})); 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}, push @lines, sprintf("#EXTINF:%s%s,%s", $item->{duration},
$self->_dump_attrs($item->{attrs}), $item->{title}); $self->_dump_attrs($item->{attrs}), $item->{title});
push @lines, $item->{file}; push @lines, $item->{file};

14
lib/Text/Playlist/PLS.pm

@ -35,7 +35,7 @@ sub parse {
my ($self, $text) = @_; my ($self, $text) = @_;
my @lines = split /\r?\n/, $text; my @lines = split /\r?\n/, $text;
$self->{items} = []; my @items = ();
# safeguard # safeguard
return "Not looks like playlist" return "Not looks like playlist"
@ -46,8 +46,8 @@ sub parse {
if ($line =~ m/(File|Title|Length)(\d+)\s*=\s*(.*)/oi) { if ($line =~ m/(File|Title|Length)(\d+)\s*=\s*(.*)/oi) {
my ($key, $num, $value) = (lc($1), $2 - 1, $3); my ($key, $num, $value) = (lc($1), $2 - 1, $3);
$value =~ s/(^\s*|\s*$)//og; $value =~ s/(^\s*|\s*$)//og;
$self->{items}->[$num] //= {}; $items[$num] //= {};
$self->{items}->[$num]->{$key} = $value; $items[$num]->{$key} = $value;
next; next;
} }
if ($line =~ m/numberofentries\s*=\s*(\d+)/oi) { if ($line =~ m/numberofentries\s*=\s*(\d+)/oi) {
@ -57,17 +57,17 @@ sub parse {
} }
warn "Number of entries not matches parsed items" 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 { sub dump {
my ($self) = @_; my ($self, @items) = @_;
my $count = 0; my $count = 0;
my @lines = ('[playlist]'); my @lines = ('[playlist]');
foreach my $item ($self->items) { foreach my $item (@items) {
$count += 1; $count += 1;
foreach my $key (qw(file title length)) { foreach my $key (qw(file title length)) {
push @lines, sprintf("%s%d=%s", ucfirst($key), $count, $item->{$key}); 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 { sub parse {
my ($self, $text) = @_; my ($self, $text) = @_;
my @items = ();
my $xp = XML::XPath->new(xml => $text); my $xp = XML::XPath->new(xml => $text);
if (my $vers = $xp->find('/playlist/@version')) { if (my $vers = $xp->find('/playlist/@version')) {
@ -60,10 +61,10 @@ sub parse {
warn "Missing mandatory 'title' or 'location' field\n"; warn "Missing mandatory 'title' or 'location' field\n";
next; next;
} }
push @{ $self->{items} }, $item; push @items, $item;
} }
return $self->items; return wantarray ? @items : [ @items ];
} }
sub dump { die("dump() unimplemented for XSPF format\n"); } sub dump { die("dump() unimplemented for XSPF format\n"); }

5
t/m3u.t

@ -36,9 +36,10 @@ my $out = [{
duration => '-1', 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; unlink $path;
is($text, $pls->dump); is($text, $pls->dump(@items));
exit 0; exit 0;

5
t/pls.t

@ -28,7 +28,8 @@ my $out = [{
title => '(#1 - 1/1) Radio Broadcast', title => '(#1 - 1/1) Radio Broadcast',
length => '-1', 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; unlink $path;
@ -41,6 +42,6 @@ Length1=-1
Version=2 Version=2
PLS PLS
is($pls->dump, $text); is($pls->dump(@items), $text);
exit 0; exit 0;

Loading…
Cancel
Save