| 1 | package Travel::Routing::DE::DBRIS; | ||
| 2 | |||
| 3 | # vim:foldmethod=marker | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use 5.020; | ||
| 8 | use utf8; | ||
| 9 | |||
| 10 | use parent 'Class::Accessor'; | ||
| 11 | |||
| 12 | use DateTime; | ||
| 13 | use DateTime::Format::Strptime; | ||
| 14 | use Encode qw(decode encode); | ||
| 15 | use JSON; | ||
| 16 | use LWP::UserAgent; | ||
| 17 | use UUID qw(uuid4); | ||
| 18 | use Travel::Status::DE::DBRIS; | ||
| 19 | use Travel::Routing::DE::DBRIS::Connection; | ||
| 20 | use Travel::Routing::DE::DBRIS::Offer; | ||
| 21 | |||
| 22 | our $VERSION = '0.18'; | ||
| 23 | |||
| 24 | Travel::Routing::DE::DBRIS->mk_ro_accessors(qw(earlier later)); | ||
| 25 | |||
| 26 | my %passenger_type_map = ( | ||
| 27 | adult => 'ERWACHSENER', | ||
| 28 | junior => 'JUGENDLICHER', | ||
| 29 | senior => 'SENIOR', | ||
| 30 | ); | ||
| 31 | |||
| 32 | # {{{ Constructors | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my ( $obj, %conf ) = @_; | ||
| 36 | |||
| 37 | my $ua = $conf{user_agent}; | ||
| 38 | |||
| 39 | if ( not $ua ) { | ||
| 40 | my %lwp_options = %{ $conf{lwp_options} // { timeout => 20 } }; | ||
| 41 | |||
| 42 | # work around Akamai BMP TLS fingerprinting | ||
| 43 | $lwp_options{ssl_opts}{SSL_ciphersuites} | ||
| 44 | = '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'; | ||
| 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/114.0.XXXX.YYY Mobile Safari/537.36', | ||
| 51 | 'Mozilla/5.0 (Linux; Android 14; SM-S928B/DS) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.XXXX.YYY Mobile Safari/537.36', | ||
| 52 | 'Mozilla/5.0 (Linux; Android 14; Pixel 9 Pro Build/AD1A.240418.003; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.XXXX.YYY Mobile Safari/537.36', | ||
| 53 | 'Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.XXXX.YYY Mobile Safari/537.36', | ||
| 54 | 'Mozilla/5.0 (Linux; Android 15; moto g - 2025 Build/V1VK35.22-13-2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/132.0.XXXX.YYY Mobile Safari/537.36', | ||
| 55 | 'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.XXXX.YYY Safari/537.36', | ||
| 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 | # Supported Languages: de cs da en es fr it nl pl | ||
| 64 | |||
| 65 | my $self = { | ||
| 66 | cache => $conf{cache}, | ||
| 67 | developer_mode => $conf{developer_mode}, | ||
| 68 | from => $conf{from}, | ||
| 69 | to => $conf{to}, | ||
| 70 | language => $conf{language} // 'de', | ||
| 71 | ua => $ua, | ||
| 72 | header => { | ||
| 73 | 'accept' => 'application/json', | ||
| 74 | 'accept-encoding' => 'gzip, br', | ||
| 75 | 'content-type' => 'application/json; charset=utf-8', | ||
| 76 | 'Origin' => 'https://www.bahn.de', | ||
| 77 | 'Referer' => 'https://www.bahn.de/buchung/fahrplan/suche', | ||
| 78 | 'User-Agent' => $ua_str, | ||
| 79 | 'x-correlation-id' => uuid4() . '_' . uuid4(), | ||
| 80 | }, | ||
| 81 | }; | ||
| 82 | |||
| 83 | bless( $self, $obj ); | ||
| 84 | |||
| 85 | my $dt = $conf{datetime} // DateTime->now( time_zone => 'Europe/Berlin' ); | ||
| 86 | my @mots | ||
| 87 | = (qw(ICE EC_IC IR REGIONAL SBAHN BUS SCHIFF UBAHN TRAM ANRUFPFLICHTIG)); | ||
| 88 | if ( $conf{modes_of_transit} ) { | ||
| 89 | @mots = @{ $conf{modes_of_transit} // [] }; | ||
| 90 | } | ||
| 91 | |||
| 92 | my ( $req_url, $req ); | ||
| 93 | |||
| 94 | if ( $conf{from} and $conf{to} ) { | ||
| 95 | $req_url | ||
| 96 | = $self->{language} eq 'de' | ||
| 97 | ? 'https://www.bahn.de/web/api/angebote/fahrplan' | ||
| 98 | : 'https://int.bahn.de/web/api/angebote/fahrplan'; | ||
| 99 | $req = { | ||
| 100 | abfahrtsHalt => $conf{from}->id, | ||
| 101 | ankunftsHalt => $conf{to}->id, | ||
| 102 | anfrageZeitpunkt => $dt->strftime('%Y-%m-%dT%H:%M:00'), | ||
| 103 | ankunftSuche => $conf{arrival} ? 'ANKUNFT' : 'ABFAHRT', | ||
| 104 | klasse => $conf{first_class} ? 'KLASSE_1' : 'KLASSE_2', | ||
| 105 | produktgattungen => \@mots, | ||
| 106 | reisende => [ | ||
| 107 | { | ||
| 108 | typ => 'ERWACHSENER', | ||
| 109 | ermaessigungen => [ | ||
| 110 | { | ||
| 111 | art => 'KEINE_ERMAESSIGUNG', | ||
| 112 | klasse => 'KLASSENLOS' | ||
| 113 | }, | ||
| 114 | ], | ||
| 115 | alter => [], | ||
| 116 | anzahl => 1, | ||
| 117 | } | ||
| 118 | ], | ||
| 119 | schnelleVerbindungen => \1, | ||
| 120 | sitzplatzOnly => \0, | ||
| 121 | bikeCarriage => \0, | ||
| 122 | reservierungsKontingenteVorhanden => \0, | ||
| 123 | nurDeutschlandTicketVerbindungen => \0, | ||
| 124 | deutschlandTicketVorhanden => \0 | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | elsif ( $conf{offers} ) { | ||
| 128 | $req_url | ||
| 129 | = $self->{language} eq 'de' | ||
| 130 | ? 'https://www.bahn.de/web/api/angebote/recon' | ||
| 131 | : 'https://int.bahn.de/web/api/angebote/recon'; | ||
| 132 | $req = { | ||
| 133 | klasse => $conf{first_class} ? 'KLASSE_1' : 'KLASSE_2', | ||
| 134 | ctxRecon => $conf{offers}{recon}, | ||
| 135 | reisende => [ | ||
| 136 | { | ||
| 137 | typ => 'ERWACHSENER', | ||
| 138 | ermaessigungen => [ | ||
| 139 | { | ||
| 140 | art => 'KEINE_ERMAESSIGUNG', | ||
| 141 | klasse => 'KLASSENLOS' | ||
| 142 | }, | ||
| 143 | ], | ||
| 144 | alter => [], | ||
| 145 | anzahl => 1, | ||
| 146 | } | ||
| 147 | ], | ||
| 148 | reservierungsKontingenteVorhanden => \0, | ||
| 149 | nurDeutschlandTicketVerbindungen => \0, | ||
| 150 | deutschlandTicketVorhanden => \0 | ||
| 151 | }; | ||
| 152 | } | ||
| 153 | |||
| 154 | for my $via ( @{ $conf{via} } ) { | ||
| 155 | my $via_stop = { id => $via->{stop}->id }; | ||
| 156 | if ( $via->{duration} ) { | ||
| 157 | $via_stop->{aufenthaltsdauer} = 0 + $via->{duration}; | ||
| 158 | } | ||
| 159 | push( @{ $req->{zwischenhalte} }, $via_stop ); | ||
| 160 | } | ||
| 161 | |||
| 162 | if ( @{ $conf{passengers} // [] } ) { | ||
| 163 | $req->{reisende} = []; | ||
| 164 | } | ||
| 165 | |||
| 166 | for my $passenger ( @{ $conf{passengers} // [] } ) { | ||
| 167 | if ( not $passenger_type_map{ $passenger->{type} } ) { | ||
| 168 | die("Unknown passenger type: '$passenger->{type}'"); | ||
| 169 | } | ||
| 170 | my $entry = { | ||
| 171 | typ => $passenger_type_map{ $passenger->{type} }, | ||
| 172 | alter => [], | ||
| 173 | anzahl => 1 | ||
| 174 | }; | ||
| 175 | for my $discount ( @{ $passenger->{discounts} // [] } ) { | ||
| 176 | my ( $type, $class ); | ||
| 177 | for my $num (qw(25 50 100)) { | ||
| 178 | if ( $discount eq "bc${num}" ) { | ||
| 179 | $type = "BAHNCARD${num}"; | ||
| 180 | $class = 'KLASSE_2'; | ||
| 181 | } | ||
| 182 | elsif ( $discount eq "bc${num}-first" ) { | ||
| 183 | $type = "BAHNCARD${num}"; | ||
| 184 | $class = 'KLASSE_1'; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | if ($type) { | ||
| 188 | push( | ||
| 189 | @{ $entry->{ermaessigungen} }, | ||
| 190 | { | ||
| 191 | art => $type, | ||
| 192 | klasse => $class, | ||
| 193 | } | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | if ( not @{ $entry->{ermaessigungen} // [] } ) { | ||
| 198 | $entry->{ermaessigungen} = [ | ||
| 199 | { | ||
| 200 | art => 'KEINE_ERMAESSIGUNG', | ||
| 201 | klasse => 'KLASSENLOS' | ||
| 202 | } | ||
| 203 | ]; | ||
| 204 | } | ||
| 205 | push( @{ $req->{reisende} }, $entry ); | ||
| 206 | } | ||
| 207 | |||
| 208 | $self->{strptime_obj} //= DateTime::Format::Strptime->new( | ||
| 209 | pattern => '%Y-%m-%dT%H:%M:%S', | ||
| 210 | time_zone => 'Europe/Berlin', | ||
| 211 | ); | ||
| 212 | |||
| 213 | $self->{strpdate_obj} //= DateTime::Format::Strptime->new( | ||
| 214 | pattern => '%Y-%m-%d', | ||
| 215 | time_zone => 'Europe/Berlin', | ||
| 216 | ); | ||
| 217 | |||
| 218 | my $json = $self->{json} = JSON->new->utf8->canonical; | ||
| 219 | |||
| 220 | if ( $conf{async} ) { | ||
| 221 | $self->{req} = $req; | ||
| 222 | return $self; | ||
| 223 | } | ||
| 224 | |||
| 225 | if ( $conf{json} ) { | ||
| 226 | $self->{raw_json} = $conf{json}; | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | my $req_str = $json->encode($req); | ||
| 230 | if ( $self->{developer_mode} ) { | ||
| 231 | say "requesting $req_str"; | ||
| 232 | } | ||
| 233 | |||
| 234 | while ( my ( $key, $value ) = each %{ $self->{header} } ) { | ||
| 235 | $ua->default_header( $key => $value ); | ||
| 236 | } | ||
| 237 | |||
| 238 | my ( $content, $error ) = $self->post_with_cache( $req_url, $req_str ); | ||
| 239 | |||
| 240 | if ($error) { | ||
| 241 | $self->{errstr} = $error; | ||
| 242 | return $self; | ||
| 243 | } | ||
| 244 | |||
| 245 | if ( $self->{developer_mode} ) { | ||
| 246 | say decode( 'utf-8', $content ); | ||
| 247 | } | ||
| 248 | |||
| 249 | $self->{raw_json} = $json->decode($content); | ||
| 250 | if ( $conf{from} and $conf{to} ) { | ||
| 251 | $self->parse_connections; | ||
| 252 | } | ||
| 253 | elsif ( $conf{offers} ) { | ||
| 254 | $self->parse_offers; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | return $self; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub new_p { | ||
| 262 | my ( $obj, %conf ) = @_; | ||
| 263 | my $promise = $conf{promise}->new; | ||
| 264 | |||
| 265 | if ( | ||
| 266 | not( $conf{from} | ||
| 267 | and $conf{to} ) | ||
| 268 | ) | ||
| 269 | { | ||
| 270 | return $promise->reject('"from" and "to" opts are mandatory'); | ||
| 271 | } | ||
| 272 | |||
| 273 | my $self = $obj->new( %conf, async => 1 ); | ||
| 274 | $self->{promise} = $conf{promise}; | ||
| 275 | |||
| 276 | $self->post_with_cache_p( $self->{url} )->then( | ||
| 277 | sub { | ||
| 278 | my ($content) = @_; | ||
| 279 | $self->{raw_json} = $self->{json}->decode($content); | ||
| 280 | $self->parse_connections; | ||
| 281 | $promise->resolve($self); | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | )->catch( | ||
| 285 | sub { | ||
| 286 | my ($err) = @_; | ||
| 287 | $promise->reject( $err, $self ); | ||
| 288 | return; | ||
| 289 | } | ||
| 290 | )->wait; | ||
| 291 | |||
| 292 | return $promise; | ||
| 293 | } | ||
| 294 | |||
| 295 | # }}} | ||
| 296 | # {{{ Internal Helpers | ||
| 297 | |||
| 298 | sub post_with_cache { | ||
| 299 | my ( $self, $url, $req ) = @_; | ||
| 300 | my $cache = $self->{cache}; | ||
| 301 | |||
| 302 | if ( $self->{developer_mode} ) { | ||
| 303 | say "POST $url $req"; | ||
| 304 | } | ||
| 305 | |||
| 306 | if ($cache) { | ||
| 307 | my $content = $cache->thaw("$url $req"); | ||
| 308 | if ($content) { | ||
| 309 | if ( $self->{developer_mode} ) { | ||
| 310 | say ' cache hit'; | ||
| 311 | } | ||
| 312 | return ( ${$content}, undef ); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | if ( $self->{developer_mode} ) { | ||
| 317 | say ' cache miss'; | ||
| 318 | } | ||
| 319 | |||
| 320 | my $reply = $self->{ua}->post( | ||
| 321 | $url, | ||
| 322 | Accept => 'application/json', | ||
| 323 | 'Accept-Encoding' => 'gzip, br', | ||
| 324 | 'Accept-Language' => $self->{language}, | ||
| 325 | 'Content-Type' => 'application/json; charset=utf-8', | ||
| 326 | Content => $req, | ||
| 327 | ); | ||
| 328 | |||
| 329 | if ( $reply->is_error ) { | ||
| 330 | return ( undef, $reply->status_line ); | ||
| 331 | } | ||
| 332 | my $content = $reply->decoded_content; | ||
| 333 | |||
| 334 | if ($cache) { | ||
| 335 | $cache->freeze( "$url $req", \$content ); | ||
| 336 | } | ||
| 337 | |||
| 338 | return ( $content, undef ); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub post_with_cache_p { | ||
| 342 | ...; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub parse_connections { | ||
| 346 | my ($self) = @_; | ||
| 347 | |||
| 348 | my $json = $self->{raw_json}; | ||
| 349 | |||
| 350 | $self->{earlier} = $json->{verbindungReference}{earlier}; | ||
| 351 | $self->{later} = $json->{verbindungReference}{later}; | ||
| 352 | |||
| 353 | for my $connection ( @{ $json->{verbindungen} // [] } ) { | ||
| 354 | push( | ||
| 355 | @{ $self->{connections} }, | ||
| 356 | Travel::Routing::DE::DBRIS::Connection->new( | ||
| 357 | json => $connection, | ||
| 358 | strpdate_obj => $self->{strpdate_obj}, | ||
| 359 | strptime_obj => $self->{strptime_obj} | ||
| 360 | ) | ||
| 361 | ); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | sub parse_offers { | ||
| 366 | my ($self) = @_; | ||
| 367 | |||
| 368 | for | ||
| 369 | my $offer ( @{ $self->{raw_json}{verbindungen}[0]{reiseAngebote} // [] } ) | ||
| 370 | { | ||
| 371 | push( | ||
| 372 | @{ $self->{offers} }, | ||
| 373 | Travel::Routing::DE::DBRIS::Offer->new( json => $offer ) | ||
| 374 | ); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | # }}} | ||
| 379 | # {{{ Public Functions | ||
| 380 | |||
| 381 | sub errstr { | ||
| 382 | my ($self) = @_; | ||
| 383 | |||
| 384 | return $self->{errstr}; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub connections { | ||
| 388 | my ($self) = @_; | ||
| 389 | return @{ $self->{connections} // [] }; | ||
| 390 | } | ||
| 391 | |||
| 392 | sub offers { | ||
| 393 | my ($self) = @_; | ||
| 394 | return @{ $self->{offers} // [] }; | ||
| 395 | } | ||
| 396 | |||
| 397 | # }}} | ||
| 398 | |||
| 399 | 1; | ||
| 400 | |||
| 401 | __END__ |