Alex 'AdUser' Z
10 years ago
commit
a42e487be8
1 changed files with 73 additions and 0 deletions
@ -0,0 +1,73 @@
|
||||
package Playlist::PLS; |
||||
|
||||
use strict; |
||||
use warnings; |
||||
|
||||
our $VERSION = 0.1; |
||||
|
||||
sub new { |
||||
my ($class) = @_; |
||||
|
||||
return bless({ items => [] }, $class); |
||||
} |
||||
|
||||
sub items { |
||||
my ($self) = @_; |
||||
|
||||
return wantarray ? @{$self->{items}} : $self->{items}; |
||||
} |
||||
|
||||
sub add { |
||||
my ($self, %args) = @_; |
||||
|
||||
foreach my $key (qw(file title)) { |
||||
next if $args{$key}; |
||||
return "Missing '$key' parameter"; |
||||
} |
||||
|
||||
$self->{items} //= []; |
||||
push @{$self->{items}}, { |
||||
file => $args{file}, |
||||
title => $args{title}, |
||||
length => $args{length} || "-1", |
||||
}; |
||||
|
||||
return; |
||||
} |
||||
|
||||
sub load { |
||||
my ($self, $file) = @_; |
||||
|
||||
$self->{items} = []; |
||||
|
||||
open(my $FH, "<", $file) or return $!; |
||||
my @lines = <$FH>; |
||||
close($FH); |
||||
chomp for @lines; |
||||
|
||||
# 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; |
||||
$self->{items}->[$num] //= {}; |
||||
$self->{items}->[$num]->{$key} = $value; |
||||
next; |
||||
} |
||||
if ($line =~ m/numberofentries\s*=\s*(\d+)/oi) { |
||||
$count = $1; |
||||
next; |
||||
} |
||||
} |
||||
|
||||
warn "Number of entries not matches parsed items" |
||||
if ($count != scalar @{$self->{items}}); |
||||
|
||||
return $self->items; |
||||
} |
||||
|
||||
1; |
Loading…
Reference in new issue