diff options
author | Daniel Friesel <derf@finalrewind.org> | 2021-01-13 19:50:59 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2021-01-13 19:50:59 +0100 |
commit | c23e5a643e6424e704edcdd5fb15b07bce378813 (patch) | |
tree | 0b4325f794e1953056994a48f81429e953515945 /bin/extract-zugbildung-for-today | |
parent | 54a8828d8c490ecc5926e6e36aea10a6b112c48c (diff) |
move daily extraction to a separate script
Diffstat (limited to 'bin/extract-zugbildung-for-today')
-rwxr-xr-x | bin/extract-zugbildung-for-today | 124 |
1 files changed, 124 insertions, 0 deletions
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); |