Unverified Commit 3074a816 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

xml2json: optionally only add re-appeared stations if they have departures

parent 616419c4
Loading
Loading
Loading
Loading
+34 −7
Original line number Original line Diff line number Diff line
@@ -10,7 +10,12 @@ use List::Util qw(any);
use XML::LibXML;
use XML::LibXML;


# old / new / renamed / reappeared / missing
# 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     = JSON->new->utf8;
my $json_str = read_file('stations.json');
my $json_str = read_file('stations.json');
@@ -109,6 +114,21 @@ sub delete_old_station {
	} @{$old_stations};
	} @{$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
# 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' ) {
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 );
		printf( "%30s has re-appeared as %s %d\n", $name, $ds100, $eva );
		if ( not $mode or $mode eq 'reappeared' ) {
		if ( not $mode or $mode eq 'reappeared' ) {
			if ( ref($old) eq 'ARRAY' ) {
			if ( $extra ne 'if-active' or has_departures($eva) ) {
				for my $o ( @{$old} ) {
				if ( ref($old) eq 'ARRAY' ) {
					delete_old_station( $o->{name}, $o->{ds100}, $o->{eva} );
					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 {
			if ( $extra eq 'if-active' ) {
				delete_old_station( $old->{name}, $old->{ds100}, $old->{eva} );
				sleep(1);
			}
			}
			add_station( $name, $ds100, $eva );
		}
		}
	}
	}