commit a42e487be81f7a90123f7a830a5df7a6ac8b1140 Author: Alex 'AdUser' Z Date: Fri Dec 26 17:00:17 2014 +1000 + Playlist::PLS diff --git a/lib/Playlist/PLS.pm b/lib/Playlist/PLS.pm new file mode 100644 index 0000000..0f4afe1 --- /dev/null +++ b/lib/Playlist/PLS.pm @@ -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;