summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Controller/Stationboard.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DBInfoscreen/Controller/Stationboard.pm')
-rw-r--r--lib/DBInfoscreen/Controller/Stationboard.pm51
1 files changed, 30 insertions, 21 deletions
diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm
index e40fda3..8ac8101 100644
--- a/lib/DBInfoscreen/Controller/Stationboard.pm
+++ b/lib/DBInfoscreen/Controller/Stationboard.pm
@@ -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 ) = @_;