diff options
| -rw-r--r-- | .gitmodules | 3 | ||||
| m--------- | ext/dbdb | 0 | ||||
| -rwxr-xr-x | lib/Travelynx.pm | 76 | ||||
| -rw-r--r-- | lib/Travelynx/Command/database.pm | 89 | ||||
| -rw-r--r-- | lib/Travelynx/Helper/DBDB.pm | 76 | ||||
| -rw-r--r-- | lib/Travelynx/Model/Stations.pm | 15 |
6 files changed, 131 insertions, 128 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a144be7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ext/dbdb"] + path = ext/dbdb + url = https://github.com/derf/train-platform-directions.git diff --git a/ext/dbdb b/ext/dbdb new file mode 160000 +Subproject eacf5450dd79def235232e452ee8d9a1f700af9 diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 9fcba0c..4a1f021 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -21,7 +21,6 @@ use List::Util; use List::UtilsBy qw(uniq_by); use List::MoreUtils qw(first_index); use Travel::Status::DE::DBRIS::Formation; -use Travelynx::Helper::DBDB; use Travelynx::Helper::DBRIS; use Travelynx::Helper::EFA; use Travelynx::Helper::HAFAS; @@ -410,20 +409,6 @@ sub startup { ); $self->helper( - dbdb => sub { - my ($self) = @_; - state $dbdb = Travelynx::Helper::DBDB->new( - log => $self->app->log, - main_cache => $self->app->cache_iris_main, - realtime_cache => $self->app->cache_iris_rt, - root_url => $self->base_url_for('/')->to_abs, - user_agent => $self->ua, - version => $self->app->config->{version}, - ); - } - ); - - $self->helper( loc_handle => sub { my ($self) = @_; @@ -2157,47 +2142,36 @@ sub startup { my $db = $self->pg->db; if ($is_departure) { - $self->dbdb->get_stationinfo_p($dep_eva)->then( - sub { - my ($station_info) = @_; - my $data = { stationinfo_dep => $station_info }; + if ( my $si + = $self->stations->get_bahn_stationinfo( eva => $dep_eva ) ) + { + my $data = { stationinfo_dep => $si }; - $self->in_transit->update_data( - uid => $uid, - db => $db, - data => $data, - train_id => $train_id, - ); - return; - } - )->catch( - sub { - # no stationinfo? no problem. - return; - } - )->wait; + $self->in_transit->update_data( + uid => $uid, + db => $db, + data => $data, + train_id => $train_id, + ); + return; + } } if ( $arr_eva and not $is_departure ) { - $self->dbdb->get_stationinfo_p($arr_eva)->then( - sub { - my ($station_info) = @_; - my $data = { stationinfo_arr => $station_info }; + if ( my $si + = $self->stations->get_bahn_stationinfo( eva => $arr_eva ) ) + { - $self->in_transit->update_data( - uid => $uid, - db => $db, - data => $data, - train_id => $train_id, - ); - return; - } - )->catch( - sub { - # no stationinfo? no problem. - return; - } - )->wait; + my $data = { stationinfo_arr => $si }; + + $self->in_transit->update_data( + uid => $uid, + db => $db, + data => $data, + train_id => $train_id, + ); + return; + } } } ); diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm index 8222fdf..5792e5f 100644 --- a/lib/Travelynx/Command/database.pm +++ b/lib/Travelynx/Command/database.pm @@ -7,7 +7,7 @@ package Travelynx::Command::database; use Mojo::Base 'Mojolicious::Command'; use DateTime; -use File::Slurp qw(read_file); +use File::Slurp qw(read_dir read_file); use List::Util qw(); use JSON; use Travel::Status::DE::EFA; @@ -3535,8 +3535,95 @@ qq{select distinct checkout_station_id from in_transit where backend_id = 0;} } ); }, + + # v68 -> v69 + # Incorporate dbdb (entry/exit direction) data into travelynx + # This avoids having to make web requests to lib.finalrewind.org/dbdb, + # and allows for also showing the exit direction for intermediate stops. + sub { + my ($db) = @_; + $db->query( + qq{ + alter table schema_version + add column dbdb varchar(12); + create table bahn_platform_directions ( + eva integer primary key, + data jsonb not null + ); + } + ); + sync_dbdb($db); + $db->query( + qq{ + update schema_version set version = 69; + update schema_version set dbdb = '2025-10-27'; + } + ); + }, ); +sub sync_dbdb { + my ($db) = @_; + + my $json = JSON->new; + + for my $file ( read_dir( 'ext/dbdb/s', prefix => 1 ) ) { + if ( $file !~ m{\.txt$} ) { + next; + } + + my %station; + for my $line ( read_file( $file, { binmode => ':encoding(utf-8)' } ) ) { + if ( $line + =~ m{ ^ \s* (?<platform> \d+ ) \s+ (?<type> \S+ ) \s+ (?<direction> \S+ ) }x + ) + { + $station{ $+{platform} } = { + kopfgleis => $+{type} eq 'K' ? \1 : \0, + direction => $+{direction}, + }; + } + elsif ( $line + =~ m{ ^ @ \s* (?<stations> [^:]+ ) : \s* (?<platforms> .+ ) $ }x + ) + { + my $stations_raw = $+{stations}; + my $platforms_raw = $+{platforms}; + my @stations = split( qr{, }, $stations_raw ); + my @platforms = split( qr{, }, $platforms_raw ); + for my $platform (@platforms) { + my ( $number, $direction ) = split( qr{ }, $platform ); + for my $from_station (@stations) { + $station{$number}{direction_from}{$from_station} + = $direction; + } + } + } + } + my ($station_name) = ( $file =~ m{ s / ([^.]*) . txt $ }x ); + my ($station) + = Travel::Status::DE::IRIS::Stations::get_station($station_name); + if ( $station and $station->[0] eq $station_name ) { + $db->insert( + 'bahn_platform_directions', + { + eva => $station->[2], + data => $json->encode( \%station ) + }, + { on_conflict => \'(eva) do update set data = EXCLUDED.data' } + ); + } + elsif ( not $station ) { + say STDERR "DBDB import: unknown station: $station_name"; + } + else { + say STDERR +"DBDB import: station mismatch: wanted to import $station_name, but got " + . $station->[0]; + } + } +} + sub sync_stations { my ( $db, $iris_version ) = @_; diff --git a/lib/Travelynx/Helper/DBDB.pm b/lib/Travelynx/Helper/DBDB.pm deleted file mode 100644 index 0db6950..0000000 --- a/lib/Travelynx/Helper/DBDB.pm +++ /dev/null @@ -1,76 +0,0 @@ -package Travelynx::Helper::DBDB; - -# Copyright (C) 2020-2023 Birte Kristina Friesel -# -# SPDX-License-Identifier: AGPL-3.0-or-later - -use strict; -use warnings; -use 5.020; - -use Encode qw(decode); -use Mojo::Promise; -use Mojo::UserAgent; -use JSON; - -sub new { - my ( $class, %opt ) = @_; - - my $version = $opt{version}; - - $opt{header} - = { 'User-Agent' => -"travelynx/${version} on $opt{root_url} +https://finalrewind.org/projects/travelynx" - }; - - return bless( \%opt, $class ); - -} - -sub get_stationinfo_p { - my ( $self, $eva ) = @_; - - my $url = "https://lib.finalrewind.org/dbdb/s/${eva}.json"; - - my $cache = $self->{main_cache}; - my $promise = Mojo::Promise->new; - - if ( my $content = $cache->thaw($url) ) { - $self->{log}->debug("get_stationinfo_p(${eva}): (cached)"); - return $promise->resolve($content); - } - - $self->{user_agent}->request_timeout(5) - ->get_p( $url => $self->{header} ) - ->then( - sub { - my ($tx) = @_; - - if ( my $err = $tx->error ) { - $self->{log}->debug( -"get_stationinfo_p(${eva}): HTTP $err->{code} $err->{message}" - ); - $cache->freeze( $url, {} ); - $promise->reject("HTTP $err->{code} $err->{message}"); - return; - } - - my $json = $tx->result->json; - $self->{log}->debug("get_stationinfo_p(${eva}): success"); - $cache->freeze( $url, $json ); - $promise->resolve($json); - return; - } - )->catch( - sub { - my ($err) = @_; - $self->{log}->debug("get_stationinfo_p(${eva}): Error ${err}"); - $cache->freeze( $url, {} ); - $promise->reject($err); - return; - } - )->wait; - return $promise; -} - -1; diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm index 5316118..6c647ec 100644 --- a/lib/Travelynx/Model/Stations.pm +++ b/lib/Travelynx/Model/Stations.pm @@ -519,4 +519,19 @@ sub grep_unknown { return @unknown_stations; } +sub get_bahn_stationinfo { + my ( $self, %opt ) = @_; + $opt{db} //= $self->{pg}->db; + + my $res + = $opt{db} + ->select( 'bahn_platform_directions', ['data'], { eva => $opt{eva} } ) + ->expand->hash; + + if ($res) { + return $res->{data}; + } + return; +} + 1; |
