Skip to content

Instantly share code, notes, and snippets.

@tabletick
Created September 4, 2012 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabletick/3623961 to your computer and use it in GitHub Desktop.
Save tabletick/3623961 to your computer and use it in GitHub Desktop.
Logging played songs in a logfile with MOC
#!/usr/bin/perl
# log-played_song.pl
# 2012-04-19
# Syntax: see parameter '-h'
#
# Script used for [MOC](http://moc.daper.net/) for logging the played songs into logfile.
# ==================
# Handling parameters
use Getopt::Long;
Getopt::Long::Configure ('bundling');
# Time
use POSIX qw(strftime);
# Get the script parameters
$pathlogfile='';
$result = GetOptions (
'a|artist=s' => \$songartist,
't|title=s' => \$songtitle,
'D|duration=i' => \$songduration,
'd|date' => \$enabledate,
'f|filename=s' => \$songfilename,
'l|logfile=s' => \$pathlogfile,
'p|playlist' => \$enableplaylist,
'h|help' => \$enablehelp
);
#
# print help
if ($enablehelp eq 1) {
print "\nScript: $0\n";
print "Syntax:
[-a|artist artistname][-t|title songtitle][-D|duration songdurationinseconds][-d|date][-f|filename songfile][-l|logfile /path/to/log][-h|help]\n
-a|artist : Name of the artist
-t|title : Title of the song
-D|duration : Duration of the song in seconds
-d|date : Enable logging of current date and time
Format: yyyy-mm-dd
-f|filename : path and filename of the song
-l|logfile : logfile for the output. If not specified output
goes to the connected console instead.
-p|playlist : Create playlist output format instead (parameter -atDf necessary, use -l to specify output file)
-h|help : Shows this help and disabled all other parameters
\n";
exit 0;
}
# Set timestamp values is necessary
if ($enabledate eq 1) {
my $get_cur_ts = time;
$timestamp = "\"".strftime "%Y-%m-%d %H:%M:%S\"\;", localtime;
} else {
$timestamp = '';
}
if ( $enableplaylist eq 1 ) { # Playlist output selected
if ( ! -e "${pathlogfile}" && ! ($pathlogfile eq '') ) { # Create file if necessary
print "logfile doesn't exist. Creating logfile.\n";
open (LOGFILE, '>:encoding(utf8)',"${pathlogfile}");
print LOGFILE "\#EXTM3U";
close (LOGFILE);
} else {
if ( ${songfilename} eq '' ) { # No filename for the output found.
print "Error: Filename isn't specified. Use parameter '-f' to specify the file.\n";
exit 1;
}
}
# File exist or isn't specified (= console out), just add your stuff
$OUTPUTSTRING = "\n\#EXTINF:${songduration},${songartist} - ${songtitle}\n${songfilename}";
} else { # Standard output CSV format
# Format additional strings if necessary
unless ($songartist eq '') { $songartist = "\"".${songartist}."\"\;"; }
unless ($songtitle eq '') { ${songtitle} = "\"".${songtitle}."\"\;"; }
unless ($songduration eq '') { ${songduration} = "\"".${songduration}."\"\;"; }
unless ($songfilename eq '') { ${songfilename} = "\"".${songfilename}."\"\;"; }
$OUTPUTSTRING = "${timestamp}${songartist}${songtitle}${songduration}${songfilename}";
chop $OUTPUTSTRING; # Remove trailing ';'
$OUTPUTSTRING .= "\n";
}
# print to console if not logfile has been specified
if ($pathlogfile eq ''){
print ${OUTPUTSTRING};
} else {
open (LOGFILE, '>>:encoding(utf8)',"${pathlogfile}");
print LOGFILE "${OUTPUTSTRING}";
close (LOGFILE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment