From f3b787f6970f5a739b91a3de1f425179aae7fddb Mon Sep 17 00:00:00 2001 From: lshprung Date: Tue, 14 Sep 2021 14:10:21 -0700 Subject: Functional for --sql flag --- build_playlists.plx | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'build_playlists.plx') diff --git a/build_playlists.plx b/build_playlists.plx index 1534fbe..595b569 100755 --- a/build_playlists.plx +++ b/build_playlists.plx @@ -19,20 +19,24 @@ our %options = ( sql => 0 ); +my @db_output; #Hold array containing output from a sql statement my $statement; #Hold statements for sqlite # Write to an m3u file to create a playlist -# @_[0] -> m3u file path +# @_[0] -> m3u file handle # @_[1] -> array of audio file paths -sub append_to_m3u { - open FH, ">> $_[0]" or die $!; +sub build_m3u { + my $filehandle = shift; - for my $line ($_[1]){ - print FH $line; - } + # Create m3u header + # TODO check if file is empty before adding the header + print $filehandle "#EXTM3U\n\n"; - close FH; + # TODO add support for EXTINF metadata (track runtime, Display name) + for my $line (@_){ + print $filehandle "$line\n"; + } } # Print a help message @@ -98,8 +102,10 @@ if (!db_cmd($dbh, $statement)){ # If sql mode is turned on, build a playlist based on a query if ($options{sql}){ - my @db_output = array_handler(db_cmd($dbh, $statement_arg, "SQL_STATEMENT returned successfully")); - for my $i (@db_output){ - print "$i\n"; - } + @db_output = flatten_array(db_cmd($dbh, $statement_arg, "SQL_STATEMENT returned successfully")); + + # TODO add switch for appending + # TODO alert user to overwrite + open FH, "> $output_pattern" or die $!; + build_m3u(*FH, @db_output); } -- cgit