-
Notifications
You must be signed in to change notification settings - Fork 0
/
booklister
executable file
·77 lines (70 loc) · 2.02 KB
/
booklister
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
#
# booklister -- show book stacks
#
use strict;
use warnings;
use Const::Fast;
use English qw( -no_match_vars );
use open qw(:std :utf8);
use Time::Piece;
use Time::Seconds;
use version; our $VERSION = qv('v2022.04.15');
my $now = localtime;
my $stackfile = "$ENV{'HOME'}/var/bookstacks.pl";
#
# read stack data...
#
our @STACKS;
if ( !( my $return = do $stackfile ) ) {
die "Couldn't parse $stackfile $EVAL_ERROR\n" if $EVAL_ERROR;
die "Couldn't do $stackfile $ERRNO\n" if !defined $return;
die "Couldn't run $stackfile\n" if !$return;
}
#
# and list everything...
#
my $tot_wt = 0;
my @eligible_stacks = ();
for my $stack (@STACKS) {
my $lastpicked_lt
= Time::Piece->strptime( $stack->{lastpicked}, '%Y-%m-%d' );
my $stack_age = $now - $lastpicked_lt;
my $nbooks = scalar @{ $stack->{books} };
my $eligible
= ( $stack_age->days >= $stack->{minage}
&& $nbooks > 0
&& $stack->{books}->[0]{owned} );
if ($eligible) {
my $wt = int( $nbooks * $stack_age->days );
push @eligible_stacks,
{
name => $stack->{name},
weight => $wt,
};
$tot_wt += $wt;
}
printf
"Name: '%s'; Last Picked: %s; MinAge: %dd; Current Age: %dd %s\n",
$stack->{name}, $stack->{lastpicked}, $stack->{minage},
$stack_age->days, $eligible ? '(Eligible)' : '(Ineligible)';
if ( $nbooks > 0 ) {
for my $book ( @{ $stack->{books} } ) {
printf " %s by %s (%s,%s)\n", $book->{title},
$book->{author},
$book->{ASIN},
$book->{owned} ? 'Owned' : 'Unowned';
}
}
else {
print " [Empty]\n";
}
}
const my $CENTUM => 100;
if ( $tot_wt > 0 ) {
print "\nEligible Stacks:\n";
for my $estk (@eligible_stacks) {
printf "%-25s Weight: %5d; Probability %4.1f%%\n", $estk->{'name'},
$estk->{'weight'}, $CENTUM * $estk->{'weight'} / $tot_wt;
}
}