commit bbbfdebe0e9351f7033f2f8f2b68caf2d92ec76c
parent 8e695434a2b683e58b6d9ede2c9cce1297c1ba39
Author: René Wagner <rwagner@rw-net.de>
Date: Thu, 10 Sep 2020 20:44:31 +0200
implement alias replacing when ~/phosta.alias exists
allow "file" field to be selected for output
Diffstat:
2 files changed, 45 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
@@ -10,8 +10,10 @@ It is mainly a selfcontained [SQlite](https://sqlite.org) database that stores t
## features
- loading of EXIF data from media files to a self-contained database
+- apply aliases to unify names of bodies or lenses
- querying different statistics from the gathered data
- available fields
+ - file
- camera maker
- camera model (body)
- lens model
diff --git a/phosta.pl b/phosta.pl
@@ -21,6 +21,7 @@ my $VERSION = '1.0';
my $PROGRAM = 'Photo Stats';
my $configfile = catfile(File::HomeDir->my_home, '.phosta.conf');
+my $aliasfile = catfile(File::HomeDir->my_home, '.phosta.alias');
# read commandline switches
our $opt_E='jpg,jpeg';
@@ -37,7 +38,7 @@ our $opt_r=0;
our $opt_f=undef;
our $opt_e=0;
-getconfig($configfile);
+getconfig();
getopts('vcrp:n:g:s:t:D:E:o:f:e') or die "Invalid parameters provided! See 'phosta --help' for more details.";
validate() or die "Invalid parameters provided! See 'phosta --help' for more details.";
@@ -46,7 +47,7 @@ my $dsn = "DBI:SQLite:dbname=$opt_D";
if ($opt_c) { unlink $opt_D; }
if ( !-e $opt_D ) { create_db($opt_D) or die 'database could not be created'; }
-writeconfig($configfile);
+writeconfig();
if ($opt_p ne '') {
populate();
@@ -62,7 +63,7 @@ sub validate
my @group_params = ('year', 'month', 'week', 'hour');
!defined($opt_g) || $opt_g ~~ @group_params or return 0;
- my @select_params = ('maker', 'model', 'lensmake', 'lens', 'aperture', 'exposuretime', 'iso', 'focallength', 'focallength35mm', '');
+ my @select_params = ('file', 'maker', 'model', 'lensmake', 'lens', 'aperture', 'exposuretime', 'iso', 'focallength', 'focallength35mm', '');
if (defined($opt_s))
{
foreach (split (/,/, $opt_s))
@@ -91,19 +92,43 @@ sub validate
sub getconfig
{
- my ( $config_filename ) = @_;
- if ( -f $config_filename ) { do $config_filename; }
+ if ( -f $configfile ) { do $configfile; }
}
sub writeconfig
{
- my ( $config_filename ) = @_;
- open(my $filehandle, '>', $config_filename) or die "Could not open file '$config_filename': $!";
- say $filehandle '$opt_D="'. $opt_D .'";';
+ open(my $filehandle, '>', $configfile) or die "Could not open file '$configfile': $!";
+ say $filehandle '$opt_D="'. $opt_D .'";';
say $filehandle '$opt_E="'. $opt_E .'";';
close $filehandle;
}
+sub getaliases
+{
+ my @aliases;
+ if ( -f $aliasfile )
+ {
+ open(FH, '<', $aliasfile) or die "Could not read file '$aliasfile': $!";
+
+ while(<FH>)
+ {
+ chomp $_;
+ my @line = split /\t{1,}/, $_;
+ push @aliases, \@line;
+ }
+
+ close(FH);
+ }
+
+ if ( $opt_v )
+ {
+ say '### Aliases found';
+ foreach (@aliases) { say join(' -> ', @$_) }
+ }
+
+ return @aliases;
+}
+
sub create_db
{
my ($dbfile) = @_;
@@ -120,7 +145,8 @@ sub populate
say "Scanning $opt_p images...this may take a while...";
my $extensions = ' -ext ' . join(' -ext ', split(/\,/, $opt_E)) .' ';
-
+ my @aliases = getaliases();
+
my $cmd = "exiftool -fast2 -r -m -f -p '\$filepath##\$make##\$model##\$lensmake##\$lens##\$lensmodel##\$focallength##\$focallengthin35mmformat##\$aperture##\$exposuretime##\$shutterspeed##\$iso##\$flash##\$datetimeoriginal' -d \"%Y-%m-%d %H:%M:%S\" " . $extensions . $opt_p;
!$opt_v or say "### Exiftool command: $cmd";
my @lines = `$cmd`;
@@ -144,6 +170,13 @@ sub populate
$exposuretime = (looks_like_number($exposuretime) && $exposuretime < 1 && $exposuretime != 0) ? "1/". POSIX::lround(1/$exposuretime) : $exposuretime;
$datetimeoriginal =~ /\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}$/ or $datetimeoriginal = '-';
+ foreach (@aliases)
+ {
+ $lens =~ s/@$_[0]/@$_[1]/;
+ $maker =~ s/@$_[0]/@$_[1]/;
+ $model =~ s/@$_[0]/@$_[1]/;
+ }
+
my $stmt = "INSERT OR REPLACE INTO photos (file, maker, model, lensmake, lens, focallength, focallength35mm, aperture, exposuretime, iso, flash, datetimeoriginal)
VALUES ('$file', '$maker', '$model', '$lensmake', '$lens', '$fl', '$fl35', '$apert', '$exposuretime', '$iso', '$flash', '$datetimeoriginal')";
my $rv = $dbh->do($stmt) or $errorcount++;
@@ -370,7 +403,7 @@ sub main::HELP_MESSAGE
say ' -g <period> : group by a time period, defaults to total (which means no grouping by period)';
say ' allowed values: year, month, week, hour';
say ' -s <fields> : specify the information you want to select, defaults to none (just show number of images)';
- say ' allowed values: maker, model, lensmake, lens, aperture, exposuretime, iso, focallength, focallength35mm';
+ say ' allowed values: file, maker, model, lensmake, lens, aperture, exposuretime, iso, focallength, focallength35mm';
say ' multiple fields should be listed comma-separated without whitespaces';
say ' -t <range> : only take images into account which have been taken in the given timerange';
say ' <range> must be specified as \'YYYYMMDD-YYYYMMDD\', you can omit the first or the later value';