summaryrefslogtreecommitdiff
path: root/lib/Travel/Routing/DE/DBRIS.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Travel/Routing/DE/DBRIS.pm')
-rw-r--r--lib/Travel/Routing/DE/DBRIS.pm55
1 files changed, 33 insertions, 22 deletions
diff --git a/lib/Travel/Routing/DE/DBRIS.pm b/lib/Travel/Routing/DE/DBRIS.pm
index f0892c6..4d93cf9 100644
--- a/lib/Travel/Routing/DE/DBRIS.pm
+++ b/lib/Travel/Routing/DE/DBRIS.pm
@@ -14,11 +14,12 @@ use DateTime::Format::Strptime;
use Encode qw(decode encode);
use JSON;
use LWP::UserAgent;
+use IO::Uncompress::Gunzip;
use Travel::Status::DE::DBRIS;
use Travel::Routing::DE::DBRIS::Connection;
use Travel::Routing::DE::DBRIS::Offer;
-our $VERSION = '0.05';
+our $VERSION = '0.07';
Travel::Routing::DE::DBRIS->mk_ro_accessors(qw(earlier later));
@@ -61,13 +62,13 @@ sub new {
@mots = @{ $conf{modes_of_transit} // [] };
}
- my ($req_url, $req);
+ my ( $req_url, $req );
- if ($conf{from} and $conf{to}) {
+ if ( $conf{from} and $conf{to} ) {
$req_url
- = $self->{language} eq 'de'
- ? 'https://www.bahn.de/web/api/angebote/fahrplan'
- : 'https://int.bahn.de/web/api/angebote/fahrplan';
+ = $self->{language} eq 'de'
+ ? 'https://www.bahn.de/web/api/angebote/fahrplan'
+ : 'https://int.bahn.de/web/api/angebote/fahrplan';
$req = {
abfahrtsHalt => $conf{from}->id,
ankunftsHalt => $conf{to}->id,
@@ -96,15 +97,15 @@ sub new {
deutschlandTicketVorhanden => \0
};
}
- elsif ($conf{offers}) {
+ elsif ( $conf{offers} ) {
$req_url
- = $self->{language} eq 'de'
- ? 'https://www.bahn.de/web/api/angebote/recon'
- : 'https://int.bahn.de/web/api/angebote/recon';
+ = $self->{language} eq 'de'
+ ? 'https://www.bahn.de/web/api/angebote/recon'
+ : 'https://int.bahn.de/web/api/angebote/recon';
$req = {
- klasse => $conf{first_class} ? 'KLASSE_1' : 'KLASSE_2',
+ klasse => $conf{first_class} ? 'KLASSE_1' : 'KLASSE_2',
ctxRecon => $conf{offers}{recon},
- reisende => [
+ reisende => [
{
typ => 'ERWACHSENER',
ermaessigungen => [
@@ -203,22 +204,28 @@ sub new {
say "requesting $req_str";
}
- my ( $content, $error ) = $self->post_with_cache( $req_url, $req_str );
+ my ( $raw_content, $error )
+ = $self->post_with_cache( $req_url, $req_str );
if ($error) {
$self->{errstr} = $error;
return $self;
}
+ my $gunzip = IO::Uncompress::Gunzip->new( \$raw_content, Append => 1 );
+ my $content = q{};
+
+ while ( $gunzip->read($content) ) { }
+
if ( $self->{developer_mode} ) {
say decode( 'utf-8', $content );
}
$self->{raw_json} = $json->decode($content);
- if ($conf{from} and $conf{to}) {
+ if ( $conf{from} and $conf{to} ) {
$self->parse_connections;
}
- elsif ($conf{offers}) {
+ elsif ( $conf{offers} ) {
$self->parse_offers;
}
}
@@ -288,6 +295,7 @@ sub post_with_cache {
my $reply = $self->{ua}->post(
$url,
Accept => 'application/json',
+ 'Accept-Encoding' => 'gzip',
'Accept-Language' => $self->{language},
'Content-Type' => 'application/json; charset=utf-8',
Content => $req,
@@ -332,10 +340,13 @@ sub parse_connections {
sub parse_offers {
my ($self) = @_;
- for my $offer (@{$self->{raw_json}{verbindungen}[0]{reiseAngebote} // []}) {
- push(@{$self->{offers}}, Travel::Routing::DE::DBRIS::Offer->new(
- json => $offer
- ));
+ for
+ my $offer ( @{ $self->{raw_json}{verbindungen}[0]{reiseAngebote} // [] } )
+ {
+ push(
+ @{ $self->{offers} },
+ Travel::Routing::DE::DBRIS::Offer->new( json => $offer )
+ );
}
}
@@ -350,12 +361,12 @@ sub errstr {
sub connections {
my ($self) = @_;
- return @{ $self->{connections} // []};
+ return @{ $self->{connections} // [] };
}
sub offers {
my ($self) = @_;
- return @{$self->{offers} // [] };
+ return @{ $self->{offers} // [] };
}
# }}}
@@ -402,7 +413,7 @@ Travel::Routing::DE::DBRIS - Interface to the bahn.de itinerary service
=head1 VERSION
-version 0.05
+version 0.07
=head1 DESCRIPTION