From 3074a8166ff91ab5e6a3cd3af293b9328eea923c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 5 Jan 2023 21:57:26 +0100 Subject: xml2json: optionally only add re-appeared stations if they have departures --- share/xml2json | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/share/xml2json b/share/xml2json index e30d48b..54a86be 100755 --- a/share/xml2json +++ b/share/xml2json @@ -10,7 +10,12 @@ use List::Util qw(any); use XML::LibXML; # old / new / renamed / reappeared / missing -my $mode = shift; +my $mode = shift; +my $extra = shift // q{}; + +if ( $extra eq 'if-active' ) { + use Travel::Status::DE::IRIS; +} my $json = JSON->new->utf8; my $json_str = read_file('stations.json'); @@ -109,6 +114,21 @@ sub delete_old_station { } @{$old_stations}; } +sub has_departures { + my ($eva) = @_; + + my $status = Travel::Status::DE::IRIS->new( + station => $eva, + lookbehind => 120, + lookahead => 240 + ); + + if ( $status->results ) { + return 1; + } + return; +} + # TODO falls ein eintrag aus old_stations im XML auftaucht sollte er aus old_stations raus und wieder in stations rein if ( -e 'missing.txt' ) { @@ -205,15 +225,22 @@ for my $station ( $tree->findnodes('//station') ) { { printf( "%30s has re-appeared as %s %d\n", $name, $ds100, $eva ); if ( not $mode or $mode eq 'reappeared' ) { - if ( ref($old) eq 'ARRAY' ) { - for my $o ( @{$old} ) { - delete_old_station( $o->{name}, $o->{ds100}, $o->{eva} ); + if ( $extra ne 'if-active' or has_departures($eva) ) { + if ( ref($old) eq 'ARRAY' ) { + for my $o ( @{$old} ) { + delete_old_station( $o->{name}, $o->{ds100}, + $o->{eva} ); + } } + else { + delete_old_station( $old->{name}, $old->{ds100}, + $old->{eva} ); + } + add_station( $name, $ds100, $eva ); } - else { - delete_old_station( $old->{name}, $old->{ds100}, $old->{eva} ); + if ( $extra eq 'if-active' ) { + sleep(1); } - add_station( $name, $ds100, $eva ); } } -- cgit v1.2.3