summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-01-01 11:48:57 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-01-01 11:48:57 +0100
commit9c7c491d219a4d8c25694d908617b3bb66292fe5 (patch)
tree546bd4bed0398e0025393b92bca14644309520af
parentc579b75b08134700f960fcf3b8c01e7d33136fd0 (diff)
Export number of seats and wagon names to JSON
-rwxr-xr-xconvert.zsh8
-rwxr-xr-xlib/carriage-json.pl66
2 files changed, 71 insertions, 3 deletions
diff --git a/convert.zsh b/convert.zsh
index 0755c8d..9191585 100755
--- a/convert.zsh
+++ b/convert.zsh
@@ -253,12 +253,17 @@ function extract_wagons {
if (( doubledecker )); then
double_decker_export=upper lib/export-carriage.py svg/${target}.svg png/${target}_u.png png/${target}_u.svg
double_decker_export=lower lib/export-carriage.py svg/${target}.svg png/${target}_l.png png/${target}_l.svg
+ lib/carriage-json.pl png/wagons.json tmp-$(( start + i - 2 )).pdf ${target} ${target}_u ${target}_l
+ else
+ lib/carriage-json.pl png/wagons.json pdf/${target}.pdf ${target}
fi
done
}
pdfseparate Fahrzeuglexikon_2020.pdf tmp-%d.pdf
+echo '{}' > png/wagons.json
+
doubledecker=0
extract_wagons $ice1_offset $ice1_types
@@ -282,8 +287,5 @@ extract_wagons $ic2_sk_offset $ic2_sk_types
rm tmp-*
-perl -MJSON -E 'say JSON->new->canonical->encode({map {$_ => \1} @ARGV})' \
- png/*.png(:t:r) > png/wagons.json
-
chmod -R a+rX png
rsync -a --info=progress2 --delete png/ epicentre:web/org.finalrewind.lib/out/dbdb/db_wagen/
diff --git a/lib/carriage-json.pl b/lib/carriage-json.pl
new file mode 100755
index 0000000..d0d0417
--- /dev/null
+++ b/lib/carriage-json.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+# Copyright (C) 2020 Daniel Friesel
+#
+# SPDX-License-Identifier: BSD-2-Clause
+
+use strict;
+use warnings;
+use 5.020;
+
+use File::Slurp qw(read_file write_file);
+use IPC::Run3;
+use JSON;
+use utf8;
+
+my ($json_file, $pdf_file, $key, @extra_keys) = @ARGV;
+
+my $command = [ "pdftotext", "-layout", "-nopgbrk", $pdf_file, "/dev/stdout" ];
+my $wagon_txt;
+
+run3($command, \undef, \$wagon_txt, undef, { binmode_stdout => ':encoding(utf-8)' } );
+
+my @lines = split(qr{\n}, $wagon_txt);
+
+
+my $name = $lines[0];
+
+$name =~ s{^\s+}{};
+$name =~ s{\s+$}{};
+
+my $json = JSON->new->utf8->decode(read_file($json_file));
+
+my $data = {};
+
+if ($name =~ m{ \d{3} \. \S \) }x) {
+ $data->{name} = $name;
+}
+else {
+ for my $line (@lines) {
+ if ($line =~ m{ \( ( D? (WL)? (A|AB|B|W) R? D? [acdehimpuvw] \S* \s+ [0-9A-Z.]+ ) \) }x) {
+ $data->{name} = "Wagen $1";
+ last;
+ }
+ }
+}
+
+for my $line (@lines) {
+ if ($line =~ m{Anzahl Plätze im (?:Großraum|Wagen):\s*(\d+)}) {
+ $data->{seats_open} = $1;
+ }
+ if ($line =~ m{Anzahl Plätze i[nm] Abteil[^:]*:\s*(\d+)}) {
+ $data->{seats_comp} = $1;
+ }
+ if ($line =~ m{Anzahl WC:\s*(\d+)}) {
+ $data->{wc} = $1;
+ }
+}
+
+$data->{seats} = ($data->{seats_open} // 0) + ($data->{seats_comp} // 0);
+
+$json->{$key} = $data;
+
+for my $extra_key (@extra_keys) {
+ $json->{$extra_key} = {};
+}
+
+write_file($json_file, JSON->new->utf8->encode($json));