Commit 375f02ad authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Fix occasionally incorrect TripIDs (and thus incorrect polylines)

Until now, tripIDs were cached based on station and departure timestamp.
These are identical for any two trains departing at the same time at the same
station, leading to one of those getting being assigned a wrong tripID.

From now on, only the JSON reported by transport.rest is cached -- tripIDs
are always recomputed based on it.
parent eeec5faa
Loading
Loading
Loading
Loading
+30 −21
Original line number Diff line number Diff line
@@ -137,27 +137,10 @@ sub get_hafas_trip_id {
		  = "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts";
	}

	if ( my $content = $cache->get($url) ) {
		return $content;
	}

	$ua->request_timeout(2);
	my $res = eval {
		$ua->get(
			$url => { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } )
		  ->result;
	};
	if ($@) {
		return;
	}
	if ( $res->is_error ) {
		return;
	}
	my $json = hafas_rest_req( $ua, $cache, $url );

	my $json = decode_json( $res->body );

	#say "looking for " . $train->train_no;
	for my $result ( @{$json} ) {
	#say "looking for " . $train->train_no . " in $url";
	for my $result ( @{ $json // [] } ) {
		my $trip_id = $result->{tripId};
		my $fahrt   = $result->{line}{fahrtNr};

@@ -165,7 +148,6 @@ sub get_hafas_trip_id {
		if ( $result->{line} and $result->{line}{fahrtNr} == $train->train_no )
		{
			#say "Trip ID is $trip_id";
			$cache->set( $url, $trip_id );
			return $trip_id;
		}
		else {
@@ -201,6 +183,33 @@ sub check_wagonorder {
	}
}

sub hafas_rest_req {
	my ( $ua, $cache, $url ) = @_;

	if ( my $content = $cache->thaw($url) ) {
		return $content;
	}

	my $res = eval {
		$ua->get(
			$url => { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } )
		  ->result;
	};

	if ($@) {
		return;
	}
	if ( $res->is_error ) {
		return;
	}

	my $json = decode_json( $res->body );

	$cache->freeze( $url, $json );

	return $json;
}

sub hafas_json_req {
	my ( $ua, $cache, $url ) = @_;