From 232a02548e314b8cb69d31fd98fe2395ca5520cb Mon Sep 17 00:00:00 2001 From: lshprung Date: Mon, 13 Sep 2021 11:03:52 -0700 Subject: Basic options for build_playlists --- build_playlists.plx | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/build_playlists.plx b/build_playlists.plx index 433c0a1..dd8ec9d 100755 --- a/build_playlists.plx +++ b/build_playlists.plx @@ -9,15 +9,22 @@ require "./shared.pl"; # Variables to be set by the user our $dbname = File::HomeDir->my_home . "/Music/library.db"; -our $output_dir; # By default, output in current directory our $table_name = "LIBRARY"; +our @tags_of_interest; # Record TAG arguments +our $output_pattern; # Record OUTPUT_PATTERN argument +our $statement_arg; # Record SQL_STATEMENT argument + +# Keep track of options that have been set +our %options = ( + sql => 0 +); my $statement; #Hold statements for sqlite # Write to an m3u file to create a playlist # @_[0] -> m3u file path -# @_[1] -> array of file paths +# @_[1] -> array of audio file paths sub append_to_m3u { open FH, ">> $_[0]" or die $!; @@ -28,6 +35,56 @@ sub append_to_m3u { close FH; } +# Print a help message +sub print_help { + print +"Usage: + $0 [OPTION]... TAG [OUTPUT_PATTERN] + $0 [OPTION]... --sql SQL_STATEMENT [OUTPUT] + +Generate m3u playlist(s) for audio files in a database (by default ~/Music/library.db). +Playlists can be generated based on columns (TAGs) in the database or based on a SQL statement output. +Multiple tags can be specified. They must be comma-separated. +If tags are specified, an output pattern can also be specified (see Examples). + +Options: + -h, --help display this help and exit + --sql SQL_STATEMENT generate a single playlist based on output of some SQL statement + +Examples: + $0 ALBUM,ALBUMARTIST ~/Music/playlists/{ALBUMARTIST}-{ALBUM}.m3u Generate a playlist for every combination of ALBUM and ALBUMARTIST in the database, with the output file pattern ALBUMARTIST-ALBUM.m3u + $0 --sql \"SELECT PATH FROM LIBRARY WHERE ARTIST='Steely Dan';\" steely_dan.m3u Generate a playlist based on the output of this SQL statement + $0 --sql \"ARTIST='Steely Dan';\" steely_dan.m3u If an incomplete SQL statement is received, the \"SELECT PATH FROM LIBRARY WHERE \" part of the SQL statement is assumed to be implied +"; +} + + +# parse flags and arguments +for (my $i = 0; $i <= $#ARGV; $i++){ + if ($ARGV[$i] =~ /-h|--help/){ + print_help(); + exit; + } + + elsif ($ARGV[$i] =~ /--sql/){ + $i++; + $statement_arg = "$ARGV[$i]"; + $options{sql} = 1; + } + + elsif ($ARGV[$i] =~ /^[^-]/){ + # This arg should contain the list of tags + if (!$options{sql} and !scalar(@tags_of_interest)){ + @tags_of_interest = split(',', "$ARGV[$i]"); + } + # This arg should contain the output_pattern + else { + $output_pattern = "$ARGV[$i]"; + last; + } + } +} + my $dbh = DBI->connect("DBI:SQLite:dbname=$dbname", "", "", { RaiseError => 1}) or die $DBI::errstr; # DEBUG print "Opened database successfully\n"; -- cgit