-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgset.pl
109 lines (90 loc) · 2.68 KB
/
gset.pl
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env perl
# script for generating index of galleries; this scans either current or
# user-supplied directory for subdirectories, checking if they contain
# 'info.json' file; the list of directories with some additional info is then
# saved into 'gset.json', which can then be used by custom frontend code
use warnings;
use strict;
use feature qw(say);
use Path::Tiny qw(path cwd);
use Mojo::JSON qw(decode_json encode_json true);
# variables
my (@dirs, %dirinfo);
my $total_images = 0;
my $total_videos = 0;
# directory to be processed, either supplied on the commandline or current
# directory if none
my $p = path($ARGV[0] // cwd);
say 'Base path is ', $p;
# iterate over directories
my $iter = $p->iterator;
while (my $r = $iter->()) {
my %data;
# skip anything that isn't a directory with info.json in it
next unless $r->is_dir;
my $info_file = $r->child('info.json');
next unless $info_file->is_file;
my $dir = $r->basename;
push(@dirs, $dir);
# read and parse the file
my $info = decode_json($info_file->slurp_raw);
$data{title} = $info->{title} // $dir;
$data{date} = $info->{date} // undef;
# get number of images/videos
$data{images} = $r->children(qr/^(?!thumb).*\.jpg\z/i);
$data{videos} = $r->children(qr/\.mp4\z/i);
$total_images += $data{images} // 0;
$total_videos += $data{videos} // 0;
# thumbnails
foreach my $thumb_file (qw(thumb thumb.2x thumb.1x)) {
if($r->child("$thumb_file.jpg")->is_file) {
$data{thumb}{src} = "$dir/$thumb_file.jpg";
last;
}
}
die "No thumbnail in $dir" unless $data{thumb}{src};
# save data
$dirinfo{$dir} = \%data;
}
# print report
printf("Processed %d directories\n", scalar(@dirs));
printf("Found %d images and %d videos\n", $total_images, $total_videos);
# sort the directories
@dirs = sort { $b cmp $a } @dirs;
# save gset
$p->child('gset.json')->spew_raw(
encode_json({
"dirs_order" => \@dirs,
"dirs" => \%dirinfo
})
);
say 'Saved global gset';
# iterate over every directory and set chaining fields
my $prev;
foreach my $dir (@dirs) {
# chaining links
if(defined $prev) {
$dirinfo{$prev}{prev} = $dir;
$dirinfo{$dir}{next} = $prev;
}
$prev = $dir;
}
# update individual info.json
foreach my $dir (@dirs) {
my $info_file = $p->child($dir)->child('info.json');
my $info = decode_json($info_file->slurp_raw);
# exit always enabled
if(!$info->{exit}) { $info->{exit} = true; }
if($dirinfo{$dir}{next}) {
$info->{next} = $dirinfo{$dir}{next};
} else {
delete $info->{next};
}
if($dirinfo{$dir}{prev}) {
$info->{prev} = $dirinfo{$dir}{prev};
} else {
delete $info->{prev}
}
# save modified info.json
$info_file->spew_raw(encode_json($info));
}