summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-01-13 19:50:59 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-01-13 19:50:59 +0100
commitc23e5a643e6424e704edcdd5fb15b07bce378813 (patch)
tree0b4325f794e1953056994a48f81429e953515945 /bin
parent54a8828d8c490ecc5926e6e36aea10a6b112c48c (diff)
move daily extraction to a separate script
Diffstat (limited to 'bin')
-rwxr-xr-xbin/db-zugbildung-to-json129
-rwxr-xr-xbin/extract-zugbildung-for-today124
2 files changed, 132 insertions, 121 deletions
diff --git a/bin/db-zugbildung-to-json b/bin/db-zugbildung-to-json
index af22818..3b90050 100755
--- a/bin/db-zugbildung-to-json
+++ b/bin/db-zugbildung-to-json
@@ -8,10 +8,7 @@ use warnings;
use 5.020;
use utf8;
-use DateTime;
-use DateTime::Format::Strptime;
use Encode qw(decode);
-use File::Slurp qw(write_file);
use IPC::Run3;
use JSON;
use List::Util qw(uniq);
@@ -27,38 +24,6 @@ sub show_usage {
exit $exit_code;
}
-my $strp = DateTime::Format::Strptime->new(
- pattern => '%F',
- time_zone => 'Europe/Berlin'
-);
-my $now = DateTime->now( time_zone => 'Europe/Berlin' );
-
-sub range_is_today {
- my ($range) = @_;
-
- if ( $range =~ m{^(.*)/(.*)$} ) {
- my $dt1 = $strp->parse_datetime($1);
- my $dt2 = $strp->parse_datetime($2);
- if ( $dt1 and $dt2 and $dt1->epoch < $now->epoch < $dt2->epoch ) {
- return 1;
- }
- }
- else {
- if ( my $dt = $strp->parse_datetime($range) ) {
- if ( $dt->ymd eq $now->ymd ) {
- return 1;
- }
- }
- }
- return;
-}
-
-sub weekday_is_today {
- my ($weekdys) = @_;
-
- return ( ${ $weekdys->[ $now->day_of_week - 1 ] } == 1 );
-}
-
my @weekdays = (qw(Mo Di Mi Do Fr Sa So));
my @months = (qw(0 I II III IV V VI VII VIII IX X XI XII));
my %weekday = map { ( $weekdays[$_] => $_ ) } ( 0 .. $#weekdays );
@@ -761,90 +726,12 @@ for my $bork (qw(104 1588 1700 77689 78112 939 2310)) {
}
}
-my %smap;
-for my $train_no ( keys %map ) {
- if ( @{ $map{$train_no} } == 1 ) {
- $smap{$train_no} = $map{$train_no}[0];
- }
- else {
- my $latest_valid;
- my $valid_count = 0;
- for my $train ( @{ $map{$train_no} } ) {
- my $is_valid = 0;
- for my $schedule ( @{ $train->{schedules} // [] } ) {
- for my $valid ( @{ $schedule->{valid} // [] } ) {
- if ( range_is_today($valid) ) {
- $is_valid = 1;
- }
- }
-
- # invalid may override valid for certain days
- for my $invalid ( @{ $schedule->{invalid} // [] } ) {
- if ( range_is_today($invalid) ) {
- $is_valid = 0;
- }
- }
- }
- if ($is_valid) {
- $latest_valid = $train;
- $valid_count++;
- }
- }
- if ( $valid_count == 1 ) {
- $smap{$train_no} = $latest_valid;
- }
- else {
- $valid_count = 0;
- $latest_valid = undef;
- for my $train ( @{ $map{$train_no} } ) {
- my $is_valid = 0;
- for my $schedule ( @{ $train->{schedules} // [] } ) {
- for my $valid ( @{ $schedule->{valid} // [] } ) {
- if ( range_is_today($valid)
- and weekday_is_today( $schedule->{weekdays} ) )
- {
- $is_valid = 1;
- }
- }
-
- # invalid may override valid for certain days
- for my $invalid ( @{ $schedule->{invalid} // [] } ) {
- if ( range_is_today($invalid) ) {
- $is_valid = 0;
- }
- }
- }
- if ($is_valid) {
- $latest_valid = $train;
- $valid_count++;
- }
- }
- if ( $valid_count == 1 ) {
- $smap{$train_no} = $latest_valid;
- }
- }
- }
-}
-
# use canonical output (i.e., sort hash keys) to allow for easy diffing.
-if ( $ENV{WITH_VARIANTS} ) {
- say JSON->new->utf8->canonical->encode(
- {
- deprecated => \0,
- source => $wr_name,
- train => {%smap},
- train_variants => {%map},
- valid => $valid,
- }
- );
-}
-else {
- say JSON->new->utf8->canonical->encode(
- {
- deprecated => \0,
- source => $wr_name,
- train => {%smap},
- valid => $valid,
- }
- );
-}
+say JSON->new->utf8->canonical->encode(
+ {
+ deprecated => \0,
+ source => $wr_name,
+ train_variants => {%map},
+ valid => $valid,
+ }
+);
diff --git a/bin/extract-zugbildung-for-today b/bin/extract-zugbildung-for-today
new file mode 100755
index 0000000..43922e4
--- /dev/null
+++ b/bin/extract-zugbildung-for-today
@@ -0,0 +1,124 @@
+#!/usr/bin/env perl
+# Copyright (C) 2020 Daniel Friesel
+#
+# SPDX-License-Identifier: BSD-2-Clause
+
+use strict;
+use warnings;
+use 5.020;
+use utf8;
+
+use DateTime;
+use DateTime::Format::Strptime;
+use File::Slurp qw(read_file);
+use JSON;
+
+my $strp = DateTime::Format::Strptime->new(
+ pattern => '%F',
+ time_zone => 'Europe/Berlin'
+);
+my $now = DateTime->now( time_zone => 'Europe/Berlin' );
+
+sub range_is_today {
+ my ($range) = @_;
+
+ if ( $range =~ m{^(.*)/(.*)$} ) {
+ my $dt1 = $strp->parse_datetime($1);
+ my $dt2 = $strp->parse_datetime($2);
+ if ( $dt1 and $dt2 and $dt1->epoch < $now->epoch < $dt2->epoch ) {
+ return 1;
+ }
+ }
+ else {
+ if ( my $dt = $strp->parse_datetime($range) ) {
+ if ( $dt->ymd eq $now->ymd ) {
+ return 1;
+ }
+ }
+ }
+ return;
+}
+
+sub weekday_is_today {
+ my ($weekdys) = @_;
+
+ return ( ${ $weekdys->[ $now->day_of_week - 1 ] } == 1 );
+}
+
+if ( @ARGV == 0 ) {
+ exit 1;
+}
+
+my $data = JSON->new->utf8->decode( read_file( $ARGV[0] ) );
+
+my %map = %{ $data->{train_variants} };
+my %smap;
+
+for my $train_no ( keys %map ) {
+ if ( @{ $map{$train_no} } == 1 ) {
+ $smap{$train_no} = $map{$train_no}[0];
+ }
+ else {
+ my $latest_valid;
+ my $valid_count = 0;
+ for my $train ( @{ $map{$train_no} } ) {
+ my $is_valid = 0;
+ for my $schedule ( @{ $train->{schedules} // [] } ) {
+ for my $valid ( @{ $schedule->{valid} // [] } ) {
+ if ( range_is_today($valid) ) {
+ $is_valid = 1;
+ }
+ }
+
+ # invalid may override valid for certain days
+ for my $invalid ( @{ $schedule->{invalid} // [] } ) {
+ if ( range_is_today($invalid) ) {
+ $is_valid = 0;
+ }
+ }
+ }
+ if ($is_valid) {
+ $latest_valid = $train;
+ $valid_count++;
+ }
+ }
+ if ( $valid_count == 1 ) {
+ $smap{$train_no} = $latest_valid;
+ }
+ else {
+ $valid_count = 0;
+ $latest_valid = undef;
+ for my $train ( @{ $map{$train_no} } ) {
+ my $is_valid = 0;
+ for my $schedule ( @{ $train->{schedules} // [] } ) {
+ for my $valid ( @{ $schedule->{valid} // [] } ) {
+ if ( range_is_today($valid)
+ and weekday_is_today( $schedule->{weekdays} ) )
+ {
+ $is_valid = 1;
+ }
+ }
+
+ # invalid may override valid for certain days
+ for my $invalid ( @{ $schedule->{invalid} // [] } ) {
+ if ( range_is_today($invalid) ) {
+ $is_valid = 0;
+ }
+ }
+ }
+ if ($is_valid) {
+ $latest_valid = $train;
+ $valid_count++;
+ }
+ }
+ if ( $valid_count == 1 ) {
+ $smap{$train_no} = $latest_valid;
+ }
+ }
+ }
+}
+
+delete $data->{train_variants};
+$data->{train} = {%smap};
+
+say JSON->new->utf8->canonical->encode($data);