| 1 | package Travel::Status::DE::DBRIS; | ||
| 2 | |||
| 3 | # vim:foldmethod=marker | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use 5.020; | ||
| 8 | use utf8; | ||
| 9 | |||
| 10 | use Carp qw(confess); | ||
| 11 | use DateTime; | ||
| 12 | use DateTime::Format::Strptime; | ||
| 13 | use Encode qw(decode encode); | ||
| 14 | use IO::Uncompress::Brotli; | ||
| 15 | use JSON; | ||
| 16 | use LWP::UserAgent; | ||
| 17 | use UUID qw(uuid4); | ||
| 18 | |||
| 19 | use Travel::Status::DE::DBRIS::Formation; | ||
| 20 | use Travel::Status::DE::DBRIS::JourneyAtStop; | ||
| 21 | use Travel::Status::DE::DBRIS::Journey; | ||
| 22 | use Travel::Status::DE::DBRIS::Location; | ||
| 23 | |||
| 24 | our $VERSION = '0.40'; | ||
| 25 | |||
| 26 | # bahn.de employs Akamai BMP, whose fingerprinting includes the advertised | ||
| 27 | # TLS options. So we gotta work around that, too. | ||
| 28 | my %tls_options = ( | ||
| 29 | SSL_ciphersuites => | ||
| 30 | 'TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384:TLS_AES_128_CCM_SHA256:TLS_AES_128_CCM_8_SHA256', | ||
| 31 | ); | ||
| 32 | |||
| 33 | # {{{ Constructors | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my ( $obj, %conf ) = @_; | ||
| 37 | |||
| 38 | my $ua = $conf{user_agent}; | ||
| 39 | |||
| 40 | if ( not $ua ) { | ||
| 41 | my %lwp_options = %{ $conf{lwp_options} // { timeout => 20 } }; | ||
| 42 | while ( my ( $k, $v ) = each %tls_options ) { | ||
| 43 | $lwp_options{ssl_opts}{$k} = $v; | ||
| 44 | } | ||
| 45 | $ua = LWP::UserAgent->new(%lwp_options); | ||
| 46 | $ua->env_proxy; | ||
| 47 | } | ||
| 48 | |||
| 49 | my @ua_base = ( | ||
| 50 | 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.XXXX.YYY Mobile Safari/539.36', | ||
| 51 | 'Mozilla/5.0 (Linux; Android 16; SM-S928B/DS) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.XXXX.YYY Mobile Safari/538.35', | ||
| 52 | 'Mozilla/5.0 (Linux; Android 15; Pixel 9 Pro Build/AD1A.250418.003; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/133.0.XXXX.YYY Mobile Safari/537.29', | ||
| 53 | 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.XXXX.YYY Mobile Safari/538.34', | ||
| 54 | 'Mozilla/5.0 (Linux; Android 16; moto g - 2025 Build/V1VK35.22-13-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/133.0.XXXX.YYY Mobile Safari/537.31', | ||
| 55 | 'Mozilla/5.0 (X11; CrOS x86_64 14542.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.XXXX.YYY Safari/538.32', | ||
| 56 | ); | ||
| 57 | |||
| 58 | my $rand1 = int( rand(1000) ); | ||
| 59 | my $rand2 = int( rand(100) ); | ||
| 60 | my $ua_str = $ua_base[ int( rand(@ua_base) ) ] =~ s{XXXX}{$rand1}r | ||
| 61 | =~ s{YYY}{$rand2}r; | ||
| 62 | |||
| 63 | my $self = { | ||
| 64 | cache => $conf{cache}, | ||
| 65 | failure_cache => $conf{failure_cache}, | ||
| 66 | developer_mode => $conf{developer_mode}, | ||
| 67 | messages => [], | ||
| 68 | results => [], | ||
| 69 | station => $conf{station}, | ||
| 70 | ua => $ua, | ||
| 71 | header => { | ||
| 72 | 'accept' => 'application/json', | ||
| 73 | 'accept-encoding' => 'gzip, br', | ||
| 74 | 'content-type' => 'application/json; charset=utf-8', | ||
| 75 | 'Origin' => 'https://www.bahn.de', | ||
| 76 | 'Referer' => 'https://www.bahn.de/buchung/fahrplan/suche', | ||
| 77 | 'User-Agent' => $ua_str, | ||
| 78 | 'x-correlation-id' => uuid4() . '_' . uuid4(), | ||
| 79 | }, | ||
| 80 | }; | ||
| 81 | |||
| 82 | bless( $self, $obj ); | ||
| 83 | |||
| 84 | my $req; | ||
| 85 | |||
| 86 | if ( my $station = $conf{station} ) { | ||
| 87 | my $dt = $conf{datetime} | ||
| 88 | // DateTime->now( time_zone => 'Europe/Berlin' ); | ||
| 89 | my @mots | ||
| 90 | = ( | ||
| 91 | qw(ICE EC_IC IR REGIONAL SBAHN BUS SCHIFF UBAHN TRAM ANRUFPFLICHTIG) | ||
| 92 | ); | ||
| 93 | if ( $conf{modes_of_transit} ) { | ||
| 94 | @mots = @{ $conf{modes_of_transit} // [] }; | ||
| 95 | } | ||
| 96 | |||
| 97 | # arr → /ankuenfte | ||
| 98 | $req | ||
| 99 | = 'https://www.bahn.de/web/api/reiseloesung/abfahrten' | ||
| 100 | . '?datum=' | ||
| 101 | . $dt->strftime('%Y-%m-%d') | ||
| 102 | . '&zeit=' | ||
| 103 | . $dt->strftime('%H:%M:00') | ||
| 104 | . '&ortExtId=' | ||
| 105 | . $station->{eva} | ||
| 106 | . '&ortId=' | ||
| 107 | . $station->{id} | ||
| 108 | . '&mitVias=true&maxVias=' | ||
| 109 | . ( $conf{num_vias} // 5 ); | ||
| 110 | for my $mot (@mots) { | ||
| 111 | $req .= '&verkehrsmittel[]=' . $mot; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | elsif ( my $gs = $conf{geoSearch} ) { | ||
| 115 | my $lat = $gs->{latitude}; | ||
| 116 | my $lon = $gs->{longitude}; | ||
| 117 | $req | ||
| 118 | = "https://www.bahn.de/web/api/reiseloesung/orte/nearby?lat=${lat}&long=${lon}&radius=9999&maxNo=100"; | ||
| 119 | } | ||
| 120 | elsif ( my $query = $conf{locationSearch} ) { | ||
| 121 | $req | ||
| 122 | = "https://www.bahn.de/web/api/reiseloesung/orte?suchbegriff=${query}&typ=ALL&limit=10"; | ||
| 123 | } | ||
| 124 | elsif ( my $journey_id = $conf{journey} ) { | ||
| 125 | my $poly = $conf{with_polyline} ? 'true' : 'false'; | ||
| 126 | $journey_id =~ s{[#]}{%23}g; | ||
| 127 | $req | ||
| 128 | = "https://www.bahn.de/web/api/reiseloesung/fahrt?journeyId=${journey_id}&poly=${poly}"; | ||
| 129 | } | ||
| 130 | elsif ( my $cf = $conf{formation} ) { | ||
| 131 | my $datetime = $cf->{departure}->clone->set_time_zone('UTC'); | ||
| 132 | my $date = $datetime->strftime('%Y-%m-%d'); | ||
| 133 | my $time = $datetime->rfc3339 =~ s{(?=Z)}{.000}r; | ||
| 134 | my %param = ( | ||
| 135 | administrationId => 80, | ||
| 136 | category => $cf->{train_type}, | ||
| 137 | date => $date, | ||
| 138 | evaNumber => $cf->{eva}, | ||
| 139 | number => $cf->{train_number}, | ||
| 140 | time => $time | ||
| 141 | ); | ||
| 142 | $req | ||
| 143 | = 'https://www.bahn.de/web/api/reisebegleitung/wagenreihung/vehicle-sequence?' | ||
| 144 | . join( '&', map { $_ . '=' . $param{$_} } sort keys %param ); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | confess( | ||
| 148 | 'station / formation / geoSearch / locationSearch / journey must be specified' | ||
| 149 | ); | ||
| 150 | } | ||
| 151 | |||
| 152 | $self->{strptime_obj} //= DateTime::Format::Strptime->new( | ||
| 153 | pattern => '%Y-%m-%dT%H:%M:%S', | ||
| 154 | time_zone => 'Europe/Berlin', | ||
| 155 | ); | ||
| 156 | |||
| 157 | $self->{strpdate_obj} //= DateTime::Format::Strptime->new( | ||
| 158 | pattern => '%Y-%m-%d', | ||
| 159 | time_zone => 'Europe/Berlin', | ||
| 160 | ); | ||
| 161 | |||
| 162 | my $json = $self->{json} = JSON->new->utf8; | ||
| 163 | |||
| 164 | if ( $conf{async} ) { | ||
| 165 | $self->{req} = $req; | ||
| 166 | return $self; | ||
| 167 | } | ||
| 168 | |||
| 169 | if ( $conf{json} ) { | ||
| 170 | $self->{raw_json} = $conf{json}; | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | if ( $self->{developer_mode} ) { | ||
| 174 | say "requesting $req"; | ||
| 175 | } | ||
| 176 | |||
| 177 | while ( my ( $key, $value ) = each %{ $self->{header} } ) { | ||
| 178 | $ua->default_header( $key => $value ); | ||
| 179 | } | ||
| 180 | |||
| 181 | my ( $content, $error ) = $self->get_with_cache($req); | ||
| 182 | |||
| 183 | if ($error) { | ||
| 184 | $self->{errstr} = $error; | ||
| 185 | return $self; | ||
| 186 | } | ||
| 187 | |||
| 188 | if ( $self->{developer_mode} ) { | ||
| 189 | say decode( 'utf-8', $content ); | ||
| 190 | } | ||
| 191 | |||
| 192 | $self->{raw_json} = $json->decode($content); | ||
| 193 | } | ||
| 194 | |||
| 195 | if ( $conf{station} ) { | ||
| 196 | $self->parse_stationboard; | ||
| 197 | } | ||
| 198 | elsif ( $conf{journey} ) { | ||
| 199 | $self->parse_journey( id => $conf{journey} ); | ||
| 200 | } | ||
| 201 | elsif ( $conf{geoSearch} or $conf{locationSearch} ) { | ||
| 202 | $self->parse_search; | ||
| 203 | } | ||
| 204 | elsif ( $conf{formation} ) { | ||
| 205 | $self->parse_formation( $conf{formation} ); | ||
| 206 | } | ||
| 207 | |||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub new_p { | ||
| 212 | my ( $obj, %conf ) = @_; | ||
| 213 | my $promise = $conf{promise}->new; | ||
| 214 | |||
| 215 | if ( | ||
| 216 | not( $conf{station} | ||
| 217 | or $conf{formation} | ||
| 218 | or $conf{geoSearch} | ||
| 219 | or $conf{locationSearch} | ||
| 220 | or $conf{journey} ) | ||
| 221 | ) | ||
| 222 | { | ||
| 223 | return $promise->reject( | ||
| 224 | 'station / geoSearch / locationSearch / journey flag must be passed' | ||
| 225 | ); | ||
| 226 | } | ||
| 227 | |||
| 228 | if ( $conf{user_agent}->can('tls_options') ) { | ||
| 229 | $conf{user_agent}->tls_options( \%tls_options ); | ||
| 230 | } | ||
| 231 | |||
| 232 | my $self = $obj->new( %conf, async => 1 ); | ||
| 233 | $self->{promise} = $conf{promise}; | ||
| 234 | |||
| 235 | $self->get_with_cache_p( $self->{req} )->then( | ||
| 236 | sub { | ||
| 237 | my ($content) = @_; | ||
| 238 | $self->{raw_json} = $self->{json}->decode($content); | ||
| 239 | |||
| 240 | if ( $conf{station} ) { | ||
| 241 | $self->parse_stationboard; | ||
| 242 | } | ||
| 243 | elsif ( $conf{journey} ) { | ||
| 244 | $self->parse_journey( id => $conf{journey} ); | ||
| 245 | } | ||
| 246 | elsif ( $conf{geoSearch} or $conf{locationSearch} ) { | ||
| 247 | $self->parse_search; | ||
| 248 | } | ||
| 249 | elsif ( $conf{formation} ) { | ||
| 250 | $self->parse_formation( $conf{formation} ); | ||
| 251 | } | ||
| 252 | |||
| 253 | if ( $self->errstr ) { | ||
| 254 | $promise->reject( $self->errstr, $self ); | ||
| 255 | } | ||
| 256 | else { | ||
| 257 | $promise->resolve($self); | ||
| 258 | } | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | )->catch( | ||
| 262 | sub { | ||
| 263 | my ($err) = @_; | ||
| 264 | $promise->reject($err); | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | )->wait; | ||
| 268 | |||
| 269 | return $promise; | ||
| 270 | } | ||
| 271 | |||
| 272 | # }}} | ||
| 273 | # {{{ Internal Helpers | ||
| 274 | |||
| 275 | sub get_with_cache { | ||
| 276 | my ( $self, $url ) = @_; | ||
| 277 | my $cache = $self->{cache}; | ||
| 278 | my $failure_cache = $self->{failure_cache}; | ||
| 279 | |||
| 280 | if ( $self->{developer_mode} ) { | ||
| 281 | say "GET $url"; | ||
| 282 | } | ||
| 283 | |||
| 284 | if ($failure_cache) { | ||
| 285 | if ( my $content = $cache->thaw($url) ) { | ||
| 286 | if ( ${$content} =~ s{ ^ n : }{}x ) { | ||
| 287 | if ( $self->{developer_mode} ) { | ||
| 288 | say " cached failure: ${$content}"; | ||
| 289 | } | ||
| 290 | return ( undef, ${$content} ); | ||
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | if ($cache) { | ||
| 296 | if ( my $content = $cache->thaw($url) ) { | ||
| 297 | if ( $self->{developer_mode} ) { | ||
| 298 | say ' cache hit'; | ||
| 299 | } | ||
| 300 | return ( ${$content}, undef ); | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | if ( $self->{developer_mode} ) { | ||
| 305 | say ' cache miss'; | ||
| 306 | } | ||
| 307 | |||
| 308 | my $reply = $self->{ua}->get($url); | ||
| 309 | |||
| 310 | if ( $reply->is_error ) { | ||
| 311 | if ( $self->{developer_mode} ) { | ||
| 312 | say ' request failed: ' . $reply->status_line; | ||
| 313 | } | ||
| 314 | if ($failure_cache) { | ||
| 315 | my $err = 'n:' . $reply->status_line; | ||
| 316 | $failure_cache->freeze( $url, \$err ); | ||
| 317 | } | ||
| 318 | return ( undef, $reply->status_line ); | ||
| 319 | } | ||
| 320 | my $content = $reply->decoded_content; | ||
| 321 | |||
| 322 | if ($cache) { | ||
| 323 | $cache->freeze( $url, \$content ); | ||
| 324 | } | ||
| 325 | |||
| 326 | return ( $content, undef ); | ||
| 327 | } | ||
| 328 | |||
| 329 | sub get_with_cache_p { | ||
| 330 | my ( $self, $url ) = @_; | ||
| 331 | my $cache = $self->{cache}; | ||
| 332 | my $failure_cache = $self->{failure_cache}; | ||
| 333 | |||
| 334 | if ( $self->{developer_mode} ) { | ||
| 335 | say "GET $url"; | ||
| 336 | } | ||
| 337 | |||
| 338 | my $promise = $self->{promise}->new; | ||
| 339 | |||
| 340 | if ($failure_cache) { | ||
| 341 | if ( my $content = $cache->thaw($url) ) { | ||
| 342 | if ( ${$content} =~ s{ ^ n : }{}x ) { | ||
| 343 | if ( $self->{developer_mode} ) { | ||
| 344 | say " cached failure: ${$content}"; | ||
| 345 | } | ||
| 346 | return $promise->reject( ${$content} ); | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | if ($cache) { | ||
| 352 | my $content = $cache->thaw($url); | ||
| 353 | if ($content) { | ||
| 354 | if ( $self->{developer_mode} ) { | ||
| 355 | say ' cache hit'; | ||
| 356 | } | ||
| 357 | return $promise->resolve( ${$content} ); | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | if ( $self->{developer_mode} ) { | ||
| 362 | say ' cache miss'; | ||
| 363 | } | ||
| 364 | |||
| 365 | $self->{ua}->get_p( $url => $self->{header} )->then( | ||
| 366 | sub { | ||
| 367 | my ($tx) = @_; | ||
| 368 | if ( my $err = $tx->error ) { | ||
| 369 | if ( $self->{developer_mode} ) { | ||
| 370 | say " request failed: $err"; | ||
| 371 | } | ||
| 372 | if ($failure_cache) { | ||
| 373 | my $cached_err = 'n:' . $err; | ||
| 374 | $failure_cache->freeze( $url, \$cached_err ); | ||
| 375 | } | ||
| 376 | $promise->reject( | ||
| 377 | "GET $url returned HTTP $err->{code} $err->{message}"); | ||
| 378 | return; | ||
| 379 | } | ||
| 380 | my $content = $tx->res->body; | ||
| 381 | |||
| 382 | if ( $tx->res->headers->content_encoding | ||
| 383 | && $tx->res->headers->content_encoding eq 'br' ) | ||
| 384 | { | ||
| 385 | # size (1_000_000) is optional in recent Brotli versions, | ||
| 386 | # but not yet in the one shipped with Debian 13. | ||
| 387 | $content = IO::Uncompress::Brotli::unbro( | ||
| 388 | $tx->res->content->asset->slurp, 1_000_000 ); | ||
| 389 | } | ||
| 390 | |||
| 391 | if ($cache) { | ||
| 392 | $cache->freeze( $url, \$content ); | ||
| 393 | } | ||
| 394 | $promise->resolve($content); | ||
| 395 | return; | ||
| 396 | } | ||
| 397 | )->catch( | ||
| 398 | sub { | ||
| 399 | my ($err) = @_; | ||
| 400 | if ( $self->{developer_mode} ) { | ||
| 401 | say " request failed: ${err}"; | ||
| 402 | } | ||
| 403 | if ($failure_cache) { | ||
| 404 | my $cached_err = 'n:' . $err; | ||
| 405 | $failure_cache->freeze( $url, \$cached_err ); | ||
| 406 | } | ||
| 407 | $promise->reject($err); | ||
| 408 | return; | ||
| 409 | } | ||
| 410 | )->wait; | ||
| 411 | |||
| 412 | return $promise; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub parse_journey { | ||
| 416 | my ( $self, %opt ) = @_; | ||
| 417 | |||
| 418 | $self->{result} = Travel::Status::DE::DBRIS::Journey->new( | ||
| 419 | id => $opt{id}, | ||
| 420 | json => $self->{raw_json}, | ||
| 421 | strpdate_obj => $self->{strpdate_obj}, | ||
| 422 | strptime_obj => $self->{strptime_obj}, | ||
| 423 | ); | ||
| 424 | } | ||
| 425 | |||
| 426 | sub parse_search { | ||
| 427 | my ($self) = @_; | ||
| 428 | |||
| 429 | @{ $self->{results} } | ||
| 430 | = map { Travel::Status::DE::DBRIS::Location->new( json => $_ ) } | ||
| 431 | @{ $self->{raw_json} // [] }; | ||
| 432 | |||
| 433 | return $self; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub parse_stationboard { | ||
| 437 | my ($self) = @_; | ||
| 438 | |||
| 439 | # @{$self->{messages}} = map { Travel::Status::DE::DBRIS::Message->new(...) } @{$self->{raw_json}{globalMessages}/[]}; | ||
| 440 | |||
| 441 | @{ $self->{results} } = map { | ||
| 442 | Travel::Status::DE::DBRIS::JourneyAtStop->new( | ||
| 443 | json => $_, | ||
| 444 | strptime_obj => $self->{strptime_obj} | ||
| 445 | ) | ||
| 446 | } @{ $self->{raw_json}{entries} // [] }; | ||
| 447 | |||
| 448 | return $self; | ||
| 449 | } | ||
| 450 | |||
| 451 | sub parse_formation { | ||
| 452 | my ( $self, $conf ) = @_; | ||
| 453 | |||
| 454 | $self->{result} = Travel::Status::DE::DBRIS::Formation->new( | ||
| 455 | json => $self->{raw_json}, | ||
| 456 | train_type => $conf->{train_type} | ||
| 457 | ); | ||
| 458 | } | ||
| 459 | |||
| 460 | # }}} | ||
| 461 | # {{{ Public Functions | ||
| 462 | |||
| 463 | sub errstr { | ||
| 464 | my ($self) = @_; | ||
| 465 | |||
| 466 | return $self->{errstr}; | ||
| 467 | } | ||
| 468 | |||
| 469 | sub results { | ||
| 470 | my ($self) = @_; | ||
| 471 | return @{ $self->{results} }; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub result { | ||
| 475 | my ($self) = @_; | ||
| 476 | return $self->{result}; | ||
| 477 | } | ||
| 478 | |||
| 479 | # }}} | ||
| 480 | |||
| 481 | 1; | ||
| 482 | |||
| 483 | __END__ |