diff options
Diffstat (limited to 'lib')
| -rwxr-xr-x | lib/Travelynx.pm | 36 | ||||
| -rw-r--r-- | lib/Travelynx/Command/database.pm | 154 | ||||
| -rw-r--r-- | lib/Travelynx/Command/maintenance.pm | 2 | ||||
| -rw-r--r-- | lib/Travelynx/Controller/Account.pm | 4 | ||||
| -rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 42 | ||||
| -rw-r--r-- | lib/Travelynx/Helper/DBDB.pm | 127 | ||||
| -rw-r--r-- | lib/Travelynx/Helper/DBRIS.pm | 104 |
7 files changed, 271 insertions, 198 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index a56dca0..9fcba0c 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -253,7 +253,8 @@ sub startup { state $dbris = Travelynx::Helper::DBRIS->new( log => $self->app->log, service_config => $self->app->config->{dbris}, - cache => $self->app->cache_iris_rt, + realtime_cache => $self->app->cache_iris_rt, + main_cache => $self->app->cache_iris_main, root_url => $self->base_url_for('/')->to_abs, user_agent => $self->ua, version => $self->app->config->{version}, @@ -1229,7 +1230,7 @@ sub startup { # mustn't be called during a transaction if ( not $opt{in_transaction} ) { $self->run_hook( $uid, 'checkin' ); - if ( $opt{hafas} eq 'DB' and $journey->class <= 16 ) { + if ( $opt{hafas} eq 'ÖBB' and $journey->class <= 16 ) { $self->add_wagonorder( uid => $uid, train_id => $journey->id, @@ -1885,31 +1886,20 @@ sub startup { my $db = $self->pg->db; if ( $datetime and $train_no ) { - $self->dbdb->has_wagonorder_p(%opt)->then( - sub { - return $self->dbdb->get_wagonorder_p(%opt); - } - )->then( + $self->dbris->get_wagonorder_p(%opt)->then( sub { - my ($wagonorder) = @_; + my ($status) = @_; + my $wr = $status->result; my $data = {}; my $user_data = {}; - my $wr; - eval { - $wr - = Travel::Status::DE::DBRIS::Formation->new( - json => $wagonorder ); - }; - if ( $opt{is_departure} - and $wr - and not exists $wagonorder->{error} ) + and $wr ) { my $dt = $opt{datetime}->clone->set_time_zone('UTC'); - $data->{wagonorder_dep} = $wagonorder; + $data->{wagonorder_dep} = $status->{raw_json}; $data->{wagonorder_param} = { time => $dt->rfc3339 =~ s{(?=Z)}{.000}r, number => $opt{train_no}, @@ -1963,10 +1953,8 @@ sub startup { train_id => $train_id, ); } - elsif ( $opt{is_arrival} - and not exists $wagonorder->{error} ) - { - $data->{wagonorder_arr} = $wagonorder; + elsif ( $opt{is_arrival} ) { + $data->{wagonorder_arr} = $status->{raw_json}; $self->in_transit->update_data( uid => $uid, db => $db, @@ -1978,6 +1966,10 @@ sub startup { } )->catch( sub { + my ($err) = @_; + $self->log->debug( + "add_wagonorder: promise rejected with ${err}"); + # no wagonorder? no problem. return; } diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm index 009da30..8222fdf 100644 --- a/lib/Travelynx/Command/database.pm +++ b/lib/Travelynx/Command/database.pm @@ -3381,6 +3381,160 @@ qq{select distinct checkout_station_id from in_transit where backend_id = 0;} } ); }, + + # v67 -> v68 + # Of course there are backends with stop names that are >64 chars long + sub { + my ($db) = @_; + $db->query( + qq{ + drop view stations_str; + drop view stations_with_external_ids; + drop view in_transit_str; + drop view journeys_str; + drop view follows_in_transit; + alter table stations alter column name type varchar(128); + create view stations_str as + select stations.name as name, + eva, lat, lon, + backends.name as backend, + dbris as is_dbris, + efa as is_efa, + iris as is_iris, + hafas as is_hafas, + motis as is_motis + from stations + left join backends + on source = backends.id; + create view stations_with_external_ids as select + stations.*, stations_external_ids.external_id + from stations + left join stations_external_ids on + stations.eva = stations_external_ids.eva and + stations.source = stations_external_ids.backend_id + ; + create view in_transit_str as select + user_id, + backend.iris as is_iris, backend.hafas as is_hafas, + backend.efa as is_efa, backend.dbris as is_dbris, + backend.motis as is_motis, + backend.name as backend_name, in_transit.backend_id as backend_id, + train_type, train_line, train_no, train_id, + extract(epoch from checkin_time) as checkin_ts, + extract(epoch from sched_departure) as sched_dep_ts, + extract(epoch from real_departure) as real_dep_ts, + checkin_station_id as dep_eva, + dep_station.ds100 as dep_ds100, + dep_station.name as dep_name, + dep_station.lat as dep_lat, + dep_station.lon as dep_lon, + dep_station_external_id.external_id as dep_external_id, + extract(epoch from checkout_time) as checkout_ts, + extract(epoch from sched_arrival) as sched_arr_ts, + extract(epoch from real_arrival) as real_arr_ts, + checkout_station_id as arr_eva, + arr_station.ds100 as arr_ds100, + arr_station.name as arr_name, + arr_station.lat as arr_lat, + arr_station.lon as arr_lon, + arr_station_external_id.external_id as arr_external_id, + polyline_id, + polylines.polyline as polyline, + visibility, + coalesce(visibility, users.public_level & 127) as effective_visibility, + cancelled, route, messages, user_data, + dep_platform, arr_platform, data + from in_transit + left join polylines on polylines.id = polyline_id + left join users on users.id = user_id + left join stations as dep_station on checkin_station_id = dep_station.eva and in_transit.backend_id = dep_station.source + left join stations as arr_station on checkout_station_id = arr_station.eva and in_transit.backend_id = arr_station.source + left join stations_external_ids as dep_station_external_id on checkin_station_id = dep_station_external_id.eva and in_transit.backend_id = dep_station_external_id.backend_id + left join stations_external_ids as arr_station_external_id on checkout_station_id = arr_station_external_id.eva and in_transit.backend_id = arr_station_external_id.backend_id + left join backends as backend on in_transit.backend_id = backend.id + ; + create view journeys_str as select + journeys.id as journey_id, user_id, + backend.iris as is_iris, backend.hafas as is_hafas, + backend.efa as is_efa, backend.dbris as is_dbris, + backend.motis as is_motis, + backend.name as backend_name, journeys.backend_id as backend_id, + train_type, train_line, train_no, train_id, + extract(epoch from checkin_time) as checkin_ts, + extract(epoch from sched_departure) as sched_dep_ts, + extract(epoch from real_departure) as real_dep_ts, + checkin_station_id as dep_eva, + dep_station.ds100 as dep_ds100, + dep_station.name as dep_name, + dep_station.lat as dep_lat, + dep_station.lon as dep_lon, + dep_station_external_id.external_id as dep_external_id, + extract(epoch from checkout_time) as checkout_ts, + extract(epoch from sched_arrival) as sched_arr_ts, + extract(epoch from real_arrival) as real_arr_ts, + checkout_station_id as arr_eva, + arr_station.ds100 as arr_ds100, + arr_station.name as arr_name, + arr_station.lat as arr_lat, + arr_station.lon as arr_lon, + arr_station_external_id.external_id as arr_external_id, + polylines.polyline as polyline, + visibility, + coalesce(visibility, users.public_level & 127) as effective_visibility, + cancelled, edited, route, messages, user_data, + dep_platform, arr_platform + from journeys + left join polylines on polylines.id = polyline_id + left join users on users.id = user_id + left join stations as dep_station on checkin_station_id = dep_station.eva and journeys.backend_id = dep_station.source + left join stations as arr_station on checkout_station_id = arr_station.eva and journeys.backend_id = arr_station.source + left join stations_external_ids as dep_station_external_id on checkin_station_id = dep_station_external_id.eva and journeys.backend_id = dep_station_external_id.backend_id + left join stations_external_ids as arr_station_external_id on checkout_station_id = arr_station_external_id.eva and journeys.backend_id = arr_station_external_id.backend_id + left join backends as backend on journeys.backend_id = backend.id + ; + create view follows_in_transit as select + r1.subject_id as follower_id, user_id as followee_id, + users.name as followee_name, + train_type, train_line, train_no, train_id, + backend.iris as is_iris, backend.hafas as is_hafas, + backend.efa as is_efa, backend.dbris as is_dbris, + backend.motis as is_motis, + backend.name as backend_name, in_transit.backend_id as backend_id, + extract(epoch from checkin_time) as checkin_ts, + extract(epoch from sched_departure) as sched_dep_ts, + extract(epoch from real_departure) as real_dep_ts, + checkin_station_id as dep_eva, + dep_station.ds100 as dep_ds100, + dep_station.name as dep_name, + dep_station.lat as dep_lat, + dep_station.lon as dep_lon, + extract(epoch from checkout_time) as checkout_ts, + extract(epoch from sched_arrival) as sched_arr_ts, + extract(epoch from real_arrival) as real_arr_ts, + checkout_station_id as arr_eva, + arr_station.ds100 as arr_ds100, + arr_station.name as arr_name, + arr_station.lat as arr_lat, + arr_station.lon as arr_lon, + polyline_id, + polylines.polyline as polyline, + visibility, + coalesce(visibility, users.public_level & 127) as effective_visibility, + cancelled, route, messages, user_data, + dep_platform, arr_platform, data + from in_transit + left join polylines on polylines.id = polyline_id + left join users on users.id = user_id + left join relations as r1 on r1.predicate = 1 and r1.object_id = user_id + left join stations as dep_station on checkin_station_id = dep_station.eva and in_transit.backend_id = dep_station.source + left join stations as arr_station on checkout_station_id = arr_station.eva and in_transit.backend_id = arr_station.source + left join backends as backend on in_transit.backend_id = backend.id + order by checkin_time desc + ; + update schema_version set version = 68; + } + ); + }, ); sub sync_stations { diff --git a/lib/Travelynx/Command/maintenance.pm b/lib/Travelynx/Command/maintenance.pm index 7baf762..7a8ae16 100644 --- a/lib/Travelynx/Command/maintenance.pm +++ b/lib/Travelynx/Command/maintenance.pm @@ -121,7 +121,7 @@ sub run { push( @uids_to_delete, $to_delete->arrays->map( sub { shift->[0] } )->each ); - if ( @uids_to_delete > 10 ) { + if ( @uids_to_delete > 60 ) { printf STDERR ( "About to delete %d accounts, which is quite a lot.\n", scalar @uids_to_delete diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm index 533e6eb..b0722f7 100644 --- a/lib/Travelynx/Controller/Account.pm +++ b/lib/Travelynx/Controller/Account.pm @@ -345,9 +345,9 @@ sub register { } if ( not $dt - or DateTime->now( time_zone => 'Europe/Berlin' )->epoch - $dt < 6 ) + or DateTime->now( time_zone => 'Europe/Berlin' )->epoch - $dt < 10 ) { - # a human user should take at least five seconds to fill out the form. + # a human user should take at least ten seconds to fill out the form. # Throw a CSRF error at presumed spammers. $self->render( 'bad_request', diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 0f31056..38a2fdf 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -594,13 +594,9 @@ sub geolocation { if ($dbris_service) { $self->render_later; - Travel::Status::DE::DBRIS->new_p( - promise => 'Mojo::Promise', - user_agent => Mojo::UserAgent->new, - geoSearch => { - latitude => $lat, - longitude => $lon - } + $self->dbris->geosearch_p( + latitude => $lat, + longitude => $lon )->then( sub { my ($dbris) = @_; @@ -627,8 +623,13 @@ sub geolocation { $self->render( json => { candidates => [], - warning => $err, - } + error => $err, + }, + + # The frontend JavaScript does not have an XHR error handler yet + # (and if it did, I do not know whether it would have access to our JSON body). + # So, for now, we do the bad thing™ and return HTTP 200 even though the request to the backend was not successful. + # status => 502, ); } )->wait; @@ -671,8 +672,11 @@ sub geolocation { $self->render( json => { candidates => [], - warning => $err, - } + error => $err, + }, + + # See above + # status => 502 ); } )->wait; @@ -722,8 +726,11 @@ sub geolocation { $self->render( json => { candidates => [], - warning => $err, - } + error => $err, + }, + + # See above + #status => 502 ); } )->wait; @@ -771,8 +778,11 @@ sub geolocation { $self->render( json => { candidates => [], - warning => $err, - } + error => $err, + }, + + # See above + #status => 502 ); } )->wait; @@ -2472,7 +2482,7 @@ sub edit_journey { uid => $uid, db => $db, id => $journey->{id}, - $key => $self->param($key) + $key => $self->param($key), ); if ($error) { last; diff --git a/lib/Travelynx/Helper/DBDB.pm b/lib/Travelynx/Helper/DBDB.pm index a310aa3..0db6950 100644 --- a/lib/Travelynx/Helper/DBDB.pm +++ b/lib/Travelynx/Helper/DBDB.pm @@ -10,6 +10,7 @@ use 5.020; use Encode qw(decode); use Mojo::Promise; +use Mojo::UserAgent; use JSON; sub new { @@ -26,132 +27,6 @@ sub new { } -sub has_wagonorder_p { - my ( $self, %opt ) = @_; - - $opt{train_type} //= q{}; - my $datetime = $opt{datetime}->clone->set_time_zone('UTC'); - my %param = ( - administrationId => 80, - category => $opt{train_type}, - date => $datetime->strftime('%Y-%m-%d'), - evaNumber => $opt{eva}, - number => $opt{train_no}, - time => $datetime->rfc3339 =~ s{(?=Z)}{.000}r - ); - - my $url = sprintf( '%s?%s', -'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence', - join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) ); - - my $promise = Mojo::Promise->new; - my $debug_prefix - = "has_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})"; - - if ( my $content = $self->{main_cache}->get("HEAD $url") - // $self->{realtime_cache}->get("HEAD $url") ) - { - if ( $content eq 'n' ) { - $self->{log}->debug("${debug_prefix}: n (cached)"); - return $promise->reject; - } - else { - $self->{log}->debug("${debug_prefix}: ${content} (cached)"); - return $promise->resolve($content); - } - } - - $self->{user_agent}->request_timeout(5) - ->get_p( $url => $self->{header} ) - ->then( - sub { - my ($tx) = @_; - if ( $tx->result->is_success ) { - $self->{log}->debug("${debug_prefix}: a"); - $self->{main_cache}->set( "HEAD $url", 'a' ); - my $body = decode( 'utf-8', $tx->res->body ); - my $json = JSON->new->decode($body); - $self->{main_cache}->freeze( $url, $json ); - $promise->resolve('a'); - } - else { - my $code = $tx->res->code; - $self->{log}->debug("${debug_prefix}: n (HTTP $code)"); - $self->{realtime_cache}->set( "HEAD $url", 'n' ); - $promise->reject; - } - return; - } - )->catch( - sub { - my ($err) = @_; - $self->{log}->debug("${debug_prefix}: n ($err)"); - $self->{realtime_cache}->set( "HEAD $url", 'n' ); - $promise->reject; - return; - } - )->wait; - return $promise; -} - -sub get_wagonorder_p { - my ( $self, %opt ) = @_; - - my $datetime = $opt{datetime}->clone->set_time_zone('UTC'); - my %param = ( - administrationId => 80, - category => $opt{train_type}, - date => $datetime->strftime('%Y-%m-%d'), - evaNumber => $opt{eva}, - number => $opt{train_no}, - time => $datetime->rfc3339 =~ s{(?=Z)}{.000}r - ); - - my $url = sprintf( '%s?%s', -'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence', - join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) ); - my $debug_prefix - = "get_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})"; - - my $promise = Mojo::Promise->new; - - if ( my $content = $self->{main_cache}->thaw($url) ) { - $self->{log}->debug("${debug_prefix}: (cached)"); - $promise->resolve($content); - return $promise; - } - - $self->{user_agent}->request_timeout(5) - ->get_p( $url => $self->{header} ) - ->then( - sub { - my ($tx) = @_; - - if ( $tx->result->is_success ) { - my $body = decode( 'utf-8', $tx->res->body ); - my $json = JSON->new->decode($body); - $self->{log}->debug("${debug_prefix}: success"); - $self->{main_cache}->freeze( $url, $json ); - $promise->resolve($json); - } - else { - my $code = $tx->res->code; - $self->{log}->debug("${debug_prefix}: HTTP ${code}"); - $promise->reject("HTTP ${code}"); - } - return; - } - )->catch( - sub { - my ($err) = @_; - $self->{log}->debug("${debug_prefix}: error ${err}"); - $promise->reject($err); - return; - } - )->wait; - return $promise; -} - sub get_stationinfo_p { my ( $self, $eva ) = @_; diff --git a/lib/Travelynx/Helper/DBRIS.pm b/lib/Travelynx/Helper/DBRIS.pm index 1b7f099..94746cc 100644 --- a/lib/Travelynx/Helper/DBRIS.pm +++ b/lib/Travelynx/Helper/DBRIS.pm @@ -29,18 +29,54 @@ sub new { return bless( \%opt, $class ); } +sub get_agent { + my ($self) = @_; + + my $agent = $self->{user_agent}; + my $proxy; + if ( my @proxies = @{ $self->{service_config}{'bahn.de'}{proxies} // [] } ) + { + $proxy = $proxies[ int( rand( scalar @proxies ) ) ]; + } + elsif ( my $p = $self->{service_config}{'bahn.de'}{proxy} ) { + $proxy = $p; + } + + if ($proxy) { + $agent = Mojo::UserAgent->new; + $agent->proxy->http($proxy); + $agent->proxy->https($proxy); + } + + return $agent; +} + +sub geosearch_p { + my ( $self, %opt ) = @_; + + return Travel::Status::DE::DBRIS->new_p( + promise => 'Mojo::Promise', + user_agent => $self->get_agent, + geoSearch => \%opt, + developer_mode => $self->{log}->is_level('debug') ? 1 : 0, + ); +} + sub get_station_id_p { my ( $self, $station_name ) = @_; + my $promise = Mojo::Promise->new; + Travel::Status::DE::DBRIS->new_p( locationSearch => $station_name, - cache => $self->{cache}, + cache => $self->{realtime_cache}, lwp_options => { timeout => 10, agent => $self->{header}{'User-Agent'}, }, - promise => 'Mojo::Promise', - user_agent => Mojo::UserAgent->new, + promise => 'Mojo::Promise', + user_agent => $self->get_agent, + developer_mode => $self->{log}->is_level('debug') ? 1 : 0, )->then( sub { my ($dbris) = @_; @@ -67,8 +103,6 @@ sub get_station_id_p { sub get_departures_p { my ( $self, %opt ) = @_; - my $agent = $self->{user_agent}; - if ( $opt{station} =~ m{ [@] L = (?<eva> \d+ ) }x ) { $opt{station} = { eva => $+{eva}, @@ -81,12 +115,14 @@ sub get_departures_p { ? $opt{timestamp}->clone : DateTime->now( time_zone => 'Europe/Berlin' ) )->subtract( minutes => $opt{lookbehind} ); + return Travel::Status::DE::DBRIS->new_p( - station => $opt{station}, - datetime => $when, - cache => $self->{cache}, - promise => 'Mojo::Promise', - user_agent => $agent->request_timeout(10), + station => $opt{station}, + datetime => $when, + cache => $self->{realtime_cache}, + promise => 'Mojo::Promise', + user_agent => $self->get_agent->request_timeout(10), + developer_mode => $self->{log}->is_level('debug') ? 1 : 0, ); } @@ -95,28 +131,13 @@ sub get_journey_p { my $promise = Mojo::Promise->new; - my $agent = $self->{user_agent}; - my $proxy; - if ( my @proxies = @{ $self->{service_config}{'bahn.de'}{proxies} // [] } ) - { - $proxy = $proxies[ int( rand( scalar @proxies ) ) ]; - } - elsif ( my $p = $self->{service_config}{'bahn.de'}{proxy} ) { - $proxy = $p; - } - - if ($proxy) { - $agent = Mojo::UserAgent->new; - $agent->proxy->http($proxy); - $agent->proxy->https($proxy); - } - Travel::Status::DE::DBRIS->new_p( - journey => $opt{trip_id}, - with_polyline => $opt{with_polyline}, - cache => $self->{realtime_cache}, - promise => 'Mojo::Promise', - user_agent => $agent->request_timeout(10), + journey => $opt{trip_id}, + with_polyline => $opt{with_polyline}, + cache => $self->{realtime_cache}, + promise => 'Mojo::Promise', + user_agent => $self->get_agent->request_timeout(10), + developer_mode => $self->{log}->is_level('debug') ? 1 : 0, )->then( sub { my ($dbris) = @_; @@ -143,4 +164,25 @@ sub get_journey_p { return $promise; } +sub get_wagonorder_p { + my ( $self, %opt ) = @_; + + $self->{log} + ->debug("get_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})"); + + return Travel::Status::DE::DBRIS->new_p( + cache => $self->{main_cache}, + failure_cache => $self->{realtime_cache}, + promise => 'Mojo::Promise', + user_agent => $self->get_agent->request_timeout(10), + formation => { + departure => $opt{datetime}, + eva => $opt{eva}, + train_type => $opt{train_type}, + train_number => $opt{train_no} + }, + developer_mode => $self->{log}->is_level('debug') ? 1 : 0, + ); +} + 1; |
