diff options
author | lshprung <lshprung@yahoo.com> | 2021-09-14 14:10:21 -0700 |
---|---|---|
committer | lshprung <lshprung@yahoo.com> | 2021-09-14 14:10:21 -0700 |
commit | f3b787f6970f5a739b91a3de1f425179aae7fddb (patch) | |
tree | 2c6ee4fafdd6fbf01590a756376c1c49cee93d47 | |
parent | d654b5c03c576bf8962960df9febf3ad37e29ea2 (diff) |
Functional for --sql flag
-rwxr-xr-x | build_db.plx | 4 | ||||
-rwxr-xr-x | build_playlists.plx | 28 | ||||
-rw-r--r-- | shared.pl | 42 |
3 files changed, 40 insertions, 34 deletions
diff --git a/build_db.plx b/build_db.plx index 141da03..7bb6072 100755 --- a/build_db.plx +++ b/build_db.plx @@ -219,7 +219,7 @@ if (!$options{append}){ else { for my $i (sort(keys %columns)){ $statement = "SELECT COUNT(*) AS CNTREC FROM pragma_table_info('$table_name') WHERE name='$i';"; - @db_output = array_handler(db_cmd($dbh, $statement)); + @db_output = flatten_array(db_cmd($dbh, $statement)); if (!$db_output[0]){ $statement = "ALTER TABLE $table_name ADD COLUMN \"$i\";"; db_cmd($dbh, $statement); @@ -251,7 +251,7 @@ for my $file (@file_list){ # If tag is an array, encode the array into semicolon-separated string if (ref($data{$i}) eq 'ARRAY'){ - for my $j (array_handler($data{$i})){ + for my $j (flatten_array($data{$i})){ $statement = $statement . "$j;"; } $statement =~ s/[;]+$//g; 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); } @@ -1,27 +1,6 @@ # File to hold shared subroutines -# Handle digging into non-scalar tags and other deep arrays -# @_[0] -> array tag/deep array -sub array_handler { - my @output; - - for my $i (@_){ - # If another array, recursively handle - if (ref($i) eq 'ARRAY'){ - push(@output, array_handler(@$i)); - } - - # If scalar, append to output normally - elsif (!ref($i)){ - push(@output, "$i"); - } - } - - return @output; -} - - # Wrapper to handle sqlite commands, return an array of returned lines from sqlite output # @_[0] -> database handle # @_[1] -> command/statement @@ -43,3 +22,24 @@ sub db_cmd { } 1; + +# Handle digging into non-scalar tags and other deep arrays +# @_[0] -> array tag/deep array +sub flatten_array { + my @output; + + for my $i (@_){ + # If another array, recursively handle + if (ref($i) eq 'ARRAY'){ + push(@output, flatten_array(@$i)); + } + + # If scalar, append to output normally + elsif (!ref($i)){ + push(@output, "$i"); + } + } + + return @output; +} + |