2020-09-06 10:41:36 +00:00
|
|
|
package Travelynx::Helper::DBDB;
|
2021-02-06 11:31:35 +00:00
|
|
|
|
2023-07-03 15:59:25 +00:00
|
|
|
# Copyright (C) 2020-2023 Birte Kristina Friesel
|
2020-11-27 21:12:56 +00:00
|
|
|
#
|
2021-01-29 17:32:13 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-09-06 10:41:36 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use 5.020;
|
|
|
|
|
|
|
|
use Encode qw(decode);
|
|
|
|
use Mojo::Promise;
|
|
|
|
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 has_wagonorder_p {
|
2024-08-08 19:13:39 +00:00
|
|
|
my ( $self, %opt ) = @_;
|
|
|
|
|
2024-08-12 16:17:05 +00:00
|
|
|
$opt{train_type} //= q{};
|
2024-08-08 19:13:39 +00:00
|
|
|
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',
|
2024-08-13 21:05:34 +00:00
|
|
|
join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) );
|
2024-08-08 19:13:39 +00:00
|
|
|
|
2020-09-06 10:41:36 +00:00
|
|
|
my $promise = Mojo::Promise->new;
|
2024-08-08 19:13:39 +00:00
|
|
|
my $debug_prefix
|
|
|
|
= "has_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})";
|
2020-09-06 10:41:36 +00:00
|
|
|
|
2024-08-17 06:55:01 +00:00
|
|
|
if ( my $content = $self->{main_cache}->get("HEAD $url")
|
|
|
|
// $self->{realtime_cache}->get("HEAD $url") )
|
|
|
|
{
|
2021-02-06 11:31:35 +00:00
|
|
|
if ( $content eq 'n' ) {
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: n (cached)");
|
2020-09-10 19:20:26 +00:00
|
|
|
return $promise->reject;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
2021-02-06 11:31:35 +00:00
|
|
|
else {
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: ${content} (cached)");
|
2021-02-06 11:31:35 +00:00
|
|
|
return $promise->resolve($content);
|
|
|
|
}
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
2020-09-06 10:41:36 +00:00
|
|
|
->then(
|
|
|
|
sub {
|
|
|
|
my ($tx) = @_;
|
|
|
|
if ( $tx->result->is_success ) {
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: a");
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{main_cache}->set( "HEAD $url", 'a' );
|
2024-08-08 19:13:39 +00:00
|
|
|
my $body = decode( 'utf-8', $tx->res->body );
|
|
|
|
my $json = JSON->new->decode($body);
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{main_cache}->freeze( $url, $json );
|
2021-12-12 15:38:26 +00:00
|
|
|
$promise->resolve('a');
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-08-11 08:31:06 +00:00
|
|
|
my $code = $tx->res->code;
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: n (HTTP $code)");
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{realtime_cache}->set( "HEAD $url", 'n' );
|
2020-09-06 10:41:36 +00:00
|
|
|
$promise->reject;
|
|
|
|
}
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
2024-08-08 19:13:39 +00:00
|
|
|
my ($err) = @_;
|
|
|
|
$self->{log}->debug("${debug_prefix}: n ($err)");
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{realtime_cache}->set( "HEAD $url", 'n' );
|
2020-09-06 10:41:36 +00:00
|
|
|
$promise->reject;
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_wagonorder_p {
|
2024-08-08 19:13:39 +00:00
|
|
|
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',
|
2024-08-13 21:05:34 +00:00
|
|
|
join( '&', map { $_ . '=' . $param{$_} } sort keys %param ) );
|
2024-08-08 19:13:39 +00:00
|
|
|
my $debug_prefix
|
|
|
|
= "get_wagonorder_p($opt{train_type} $opt{train_no} @ $opt{eva})";
|
2020-09-06 10:41:36 +00:00
|
|
|
|
|
|
|
my $promise = Mojo::Promise->new;
|
|
|
|
|
2024-08-17 06:55:01 +00:00
|
|
|
if ( my $content = $self->{main_cache}->thaw($url) ) {
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: (cached)");
|
2020-09-06 10:41:36 +00:00
|
|
|
$promise->resolve($content);
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
|
|
|
->then(
|
|
|
|
sub {
|
|
|
|
my ($tx) = @_;
|
|
|
|
|
2021-12-12 16:24:24 +00:00
|
|
|
if ( $tx->result->is_success ) {
|
|
|
|
my $body = decode( 'utf-8', $tx->res->body );
|
|
|
|
my $json = JSON->new->decode($body);
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: success");
|
2024-08-17 06:55:01 +00:00
|
|
|
$self->{main_cache}->freeze( $url, $json );
|
2021-12-12 16:24:24 +00:00
|
|
|
$promise->resolve($json);
|
|
|
|
}
|
|
|
|
else {
|
2024-08-11 08:31:06 +00:00
|
|
|
my $code = $tx->res->code;
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: HTTP ${code}");
|
2021-12-12 16:24:24 +00:00
|
|
|
$promise->reject("HTTP ${code}");
|
|
|
|
}
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
2024-08-08 19:13:39 +00:00
|
|
|
$self->{log}->debug("${debug_prefix}: error ${err}");
|
2020-09-06 10:41:36 +00:00
|
|
|
$promise->reject($err);
|
2020-09-10 19:20:26 +00:00
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_stationinfo_p {
|
|
|
|
my ( $self, $eva ) = @_;
|
|
|
|
|
|
|
|
my $url = "https://lib.finalrewind.org/dbdb/s/${eva}.json";
|
|
|
|
|
2024-05-30 09:53:33 +00:00
|
|
|
my $cache = $self->{main_cache};
|
2020-09-06 10:41:36 +00:00
|
|
|
my $promise = Mojo::Promise->new;
|
|
|
|
|
|
|
|
if ( my $content = $cache->thaw($url) ) {
|
2024-05-30 09:54:02 +00:00
|
|
|
$self->{log}->debug("get_stationinfo_p(${eva}): (cached)");
|
2020-09-10 19:20:26 +00:00
|
|
|
return $promise->resolve($content);
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
|
|
|
|
->then(
|
|
|
|
sub {
|
|
|
|
my ($tx) = @_;
|
|
|
|
|
|
|
|
if ( my $err = $tx->error ) {
|
2024-05-30 09:54:02 +00:00
|
|
|
$self->{log}->debug(
|
|
|
|
"get_stationinfo_p(${eva}): HTTP $err->{code} $err->{message}"
|
|
|
|
);
|
2020-09-20 07:55:28 +00:00
|
|
|
$cache->freeze( $url, {} );
|
2020-09-10 19:20:26 +00:00
|
|
|
$promise->reject("HTTP $err->{code} $err->{message}");
|
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my $json = $tx->result->json;
|
2024-05-30 09:54:02 +00:00
|
|
|
$self->{log}->debug("get_stationinfo_p(${eva}): success");
|
2020-09-06 10:41:36 +00:00
|
|
|
$cache->freeze( $url, $json );
|
2020-09-10 19:20:26 +00:00
|
|
|
$promise->resolve($json);
|
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->catch(
|
|
|
|
sub {
|
|
|
|
my ($err) = @_;
|
2024-05-30 09:54:02 +00:00
|
|
|
$self->{log}->debug("get_stationinfo_p(${eva}): Error ${err}");
|
2020-09-20 07:55:28 +00:00
|
|
|
$cache->freeze( $url, {} );
|
2020-09-10 19:20:26 +00:00
|
|
|
$promise->reject($err);
|
|
|
|
return;
|
2020-09-06 10:41:36 +00:00
|
|
|
}
|
|
|
|
)->wait;
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|