From 85bd8b45c3f3f1fd206d7331968a1ed65e832643 Mon Sep 17 00:00:00 2001 From: "https://id.koumbit.net/anarcat" Date: Sat, 4 Oct 2014 22:23:30 +0000 Subject: add usage and commandline parsing --- .../git-annex-xbmc-playcount.pl | 229 ++++++++++++++++++--- 1 file changed, 195 insertions(+), 34 deletions(-) (limited to 'doc/tips') diff --git a/doc/tips/dumb_metadata_extraction_from_xbmc/git-annex-xbmc-playcount.pl b/doc/tips/dumb_metadata_extraction_from_xbmc/git-annex-xbmc-playcount.pl index 3e2bd9bce..9df8db9fe 100644 --- a/doc/tips/dumb_metadata_extraction_from_xbmc/git-annex-xbmc-playcount.pl +++ b/doc/tips/dumb_metadata_extraction_from_xbmc/git-annex-xbmc-playcount.pl @@ -1,45 +1,206 @@ #! /usr/bin/perl -w -# we want to operate on relative links, so set this to the common prefix -# to the git annex repo -my $prefix="/home/media/video/"; -# this is the directory for the XBMC database -my $path = '/home/video/.xbmc/userdata/Database/'; +use Getopt::Long; +use Pod::Usage; -# no user-serviceable parts below +my $help = 0; +my $usage = 0; +my $dryrun = 0; +my $path = ''; +my $annex = ''; +my $home = $ENV{'HOME'}; + +sub main() { + checkargs(); + if (!$path) { + $path = $home . '/.xbmc/userdata/Database'; + } + $dbpath = finddb($path); + if (!$dbpath) { + pod2usage("$0: can't find a XBMC database in '$path'."); + } + checkdb(); +} # list videos database, find the latest one # modified version of # http://stackoverflow.com/questions/4651092/getting-the-list-of-files-sorted-by-modification-date-in-perl -opendir my($dirh), $path or die "can't opendir $path: $!"; -my @flist = sort { -M $a <=> -M $b } # Sort by modification time - map { "$path/$_" } # We need full paths for sorting - grep { /^MyVideos.*\.db$/ } - readdir $dirh; -closedir $dirh; - -my $dbpath=$flist[0]; - -my @lines = `echo 'SELECT playCount, path.strPath, files.strFileName FROM movie JOIN files ON files.idFile=movie.idFile JOIN path ON path.idPath=files.idPath;' | sqlite3 $dbpath`; -for (@lines) { - my ($count, $dir, $file) = split /\|/; - chomp $file; - # empty or non-numeric count is zero - if ($count !~ /[0-9]/) { - $count = 0; - } - if ($file =~ s#stack://##) { - for (split /,/, $file) { - s/$prefix//; - s/^ //; - s/ $//; - my @cmd = (qw(git annex metadata --set), "playCount=$count", $_); - system(@cmd); - } +sub finddb($) { + my $path = shift(@_); + opendir my($dirh), $path or die "can't opendir $path: $!"; + my @flist = sort { -M $a <=> -M $b } # Sort by modification time + map { "$path/$_" } # We need full paths for sorting + grep { /^MyVideos.*\.db$/ } + readdir $dirh; + closedir $dirh; + if ($#flist > 0) { + return $flist[0]; } else { - $dir =~ s/$prefix//; - my @cmd = (qw(git annex metadata --set), "playCount=$count", "$dir$file"); - system(@cmd); + return 0; + } +} + +sub checkargs() { + pod2usage(1) if $help; + pod2usage(-exitval => 0, -verbose => 2) if $usage; + + GetOptions('h|?' => \$help, + 'help|usage' => \$usage, + # we want to operate on relative links, so set this to + # the common annex to the git annex repo + 'annex=s' => \$annex, + 'path=s' => \$path, + 'home=s' => \$home, + 'dryrun|n' => \$dryrun, + ) + or die("Error parsing commandline\n"); +} + +sub checkdb() { + my @lines = `echo 'SELECT playCount, path.strPath, files.strFileName FROM movie JOIN files ON files.idFile=movie.idFile JOIN path ON path.idPath=files.idPath;' | sqlite3 $dbpath`; + for (@lines) { + my ($count, $dir, $file) = split /\|/; + chomp $file; + # empty or non-numeric count is zero + if ($count !~ /[0-9]/) { + $count = 0; + } + if ($file =~ s#stack://##) { + for (split /,/, $file) { + s/$annex//; + s/^ //; + s/ $//; + my @cmd = (qw(git annex metadata --set), "playCount=$count", $_); + if ($dryrun) { + print join(' ', @cmd) . "\n"; + } + else { + system(@cmd); + } + } + } + else { + $dir =~ s/$annex//; + my @cmd = (qw(git annex metadata --set), "playCount=$count", "$dir$file"); + if ($dryrun) { + print join(' ', @cmd) . "\n"; + } + else { + system(@cmd); + } + } } } + +main(); + +__END__ +=encoding utf8 + +=head1 NAME + +git-annex-xbmc-playcount - register XBMC playcounts as git-annex metadata + +=head1 SYNOPSIS + +git-annex-xbmc-playcount [--path .xbmc/userdata/Database] + + Options: + -h short usage + --help complete help + --dryrun, -n do nothing and show the commands that would be ran + --annex path to the git-annex repo + --home the home directory where the .xbmc directory is located + --path the location of the Database directory of XBMC, overrides --home + +=head1 DESCRIPTION + +This program will look into the XBMC database for the "playcount" +field to register that number as metadata in the git-annex repository. + +=head1 OPTIONS + +=over 8 + +=item B<--dryrun> + +Do nothing but show all the steps that would be ran. The output can be +piped through a POSIX shell after inspection. B<-n> is an alias of +this command. Example: + + git-annex-xbmc-playcount -n | tee runme + # inspect the output + sh < runme + +=item B<--annex> + +This option allows the user to specify the root of the git-annex +repository, which is then stripped off the paths found in the XBMC +database. + +=item B<--home> + +Home of the user running XBMC. If not specified, defaults to the $HOME +environment variables. The script will look into +B<$home/.xbmc/userdata/Database> for a file matching +B<^MyVideos.*\.db$> and will fail if none is found. + +=item B<--path> + +Manually specify the path to B<.xbmc/userdata/Database>. This +overrides B<--home>. + +Note that this doesn't point directly to the datbase itself, because +there are usually many database files and we want to automatically +find the latest. This may be a stupid limitation. + +=back + +=head1 EXAMPLES + +You have a git annex in B and XBMC is ran as the +B