| 1 | package Travel::Status::MOTIS; | ||
| 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::ISO8601; | ||
| 13 | use Encode qw(decode encode); | ||
| 14 | use JSON; | ||
| 15 | |||
| 16 | use LWP::UserAgent; | ||
| 17 | |||
| 18 | use URI; | ||
| 19 | |||
| 20 | use Travel::Status::MOTIS::Services; | ||
| 21 | use Travel::Status::MOTIS::TripAtStopover; | ||
| 22 | use Travel::Status::MOTIS::Trip; | ||
| 23 | use Travel::Status::MOTIS::Stopover; | ||
| 24 | use Travel::Status::MOTIS::Stop; | ||
| 25 | |||
| 26 | our $VERSION = '0.07'; | ||
| 27 | |||
| 28 | # {{{ Endpoint Definition | ||
| 29 | |||
| 30 | # Data sources: <https://github.com/public-transport/transport-apis>. | ||
| 31 | # Thanks to Jannis R / @derhuerst and all contributors for maintaining these. | ||
| 32 | my $motis_instance = Travel::Status::MOTIS::Services::get_service_ref(); | ||
| 33 | |||
| 34 | # }}} | ||
| 35 | # {{{ Constructors | ||
| 36 | |||
| 37 | sub new { | ||
| 38 | my ( $obj, %conf ) = @_; | ||
| 39 | my $service = $conf{service}; | ||
| 40 | |||
| 41 | if ( not defined $service ) { | ||
| 42 | confess("You must specify a service"); | ||
| 43 | } | ||
| 44 | |||
| 45 | if ( defined $service and not exists $motis_instance->{$service} ) { | ||
| 46 | confess("The service '$service' is not supported"); | ||
| 47 | } | ||
| 48 | |||
| 49 | my $user_agent = $conf{user_agent}; | ||
| 50 | |||
| 51 | if ( not $user_agent ) { | ||
| 52 | $user_agent | ||
| 53 | = LWP::UserAgent->new( %{ $conf{lwp_options} // { timeout => 10 } } ); | ||
| 54 | } | ||
| 55 | |||
| 56 | my $self = { | ||
| 57 | cache => $conf{cache}, | ||
| 58 | developer_mode => $conf{developer_mode}, | ||
| 59 | results => [], | ||
| 60 | station => $conf{station}, | ||
| 61 | user_agent => $user_agent, | ||
| 62 | time_zone => $conf{time_zone} // 'local', | ||
| 63 | }; | ||
| 64 | |||
| 65 | bless( $self, $obj ); | ||
| 66 | |||
| 67 | my $request_url = URI->new; | ||
| 68 | |||
| 69 | if ( my $stop_id = $conf{stop_id} ) { | ||
| 70 | my $timestamp = $conf{timestamp} // DateTime->now; | ||
| 71 | |||
| 72 | my @modes_of_transit = (qw(TRANSIT)); | ||
| 73 | |||
| 74 | if ( $conf{modes_of_transit} ) { | ||
| 75 | @modes_of_transit = @{ $conf{modes_of_transit} // [] }; | ||
| 76 | } | ||
| 77 | |||
| 78 | $request_url->path('api/v6/stoptimes'); | ||
| 79 | $request_url->query_form( | ||
| 80 | time => DateTime::Format::ISO8601->format_datetime($timestamp), | ||
| 81 | stopId => $stop_id, | ||
| 82 | n => $conf{results} // 10, | ||
| 83 | window => $conf{window}, | ||
| 84 | mode => join( ',', @modes_of_transit ), | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | elsif ( my $trip_id = $conf{trip_id} ) { | ||
| 88 | $request_url->path('api/v6/trip'); | ||
| 89 | $request_url->query_form( | ||
| 90 | tripId => $trip_id, | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | elsif ( my $coordinates = $conf{stops_by_coordinate} ) { | ||
| 94 | my $lat = $coordinates->{lat}; | ||
| 95 | my $lon = $coordinates->{lon}; | ||
| 96 | |||
| 97 | $request_url->path('api/v1/reverse-geocode'); | ||
| 98 | $request_url->query_form( | ||
| 99 | type => 'STOP', | ||
| 100 | place => "$lat,$lon,0", | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | elsif ( my $query = $conf{stops_by_query} ) { | ||
| 104 | $request_url->path('api/v1/geocode'); | ||
| 105 | $request_url->query_form( | ||
| 106 | text => $query, | ||
| 107 | ); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | confess( | ||
| 111 | 'stop_id / trip_id / stops_by_coordinate / stops_by_query must be specified' | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | my $json = $self->{json} = JSON->new->utf8; | ||
| 116 | |||
| 117 | $request_url | ||
| 118 | = $request_url->abs( $motis_instance->{$service}{endpoint} )->as_string; | ||
| 119 | |||
| 120 | if ( $conf{async} ) { | ||
| 121 | $self->{request_url} = $request_url; | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | if ( $conf{json} ) { | ||
| 126 | $self->{raw_json} = $conf{json}; | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | if ( $self->{developer_mode} ) { | ||
| 130 | say "requesting $request_url"; | ||
| 131 | } | ||
| 132 | |||
| 133 | my ( $content, $error ) = $self->get_with_cache($request_url); | ||
| 134 | |||
| 135 | if ($error) { | ||
| 136 | $self->{errstr} = $error; | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | if ( $self->{developer_mode} ) { | ||
| 141 | say decode( 'utf-8', $content ); | ||
| 142 | } | ||
| 143 | |||
| 144 | $self->{raw_json} = $json->decode($content); | ||
| 145 | } | ||
| 146 | |||
| 147 | if ( $conf{stop_id} ) { | ||
| 148 | $self->parse_trips_at_stopover; | ||
| 149 | $self->parse_stop; | ||
| 150 | } | ||
| 151 | elsif ( $conf{trip_id} ) { | ||
| 152 | $self->parse_trip; | ||
| 153 | } | ||
| 154 | elsif ( $conf{stops_by_query} or $conf{stops_by_coordinate} ) { | ||
| 155 | $self->parse_stops_by; | ||
| 156 | } | ||
| 157 | |||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub new_p { | ||
| 162 | my ( $obj, %conf ) = @_; | ||
| 163 | |||
| 164 | my $promise = $conf{promise}->new; | ||
| 165 | |||
| 166 | if ( | ||
| 167 | not( $conf{stop_id} | ||
| 168 | or $conf{trip_id} | ||
| 169 | or $conf{stops_by_coordinate} | ||
| 170 | or $conf{stops_by_query} ) | ||
| 171 | ) | ||
| 172 | { | ||
| 173 | return $promise->reject( | ||
| 174 | 'stop_id / trip_id / stops_by_coordinate / stops_by_query flag must be passed' | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | my $self = $obj->new( %conf, async => 1 ); | ||
| 179 | |||
| 180 | $self->{promise} = $conf{promise}; | ||
| 181 | |||
| 182 | $self->get_with_cache_p( $self->{request_url} )->then( | ||
| 183 | sub { | ||
| 184 | my ($content) = @_; | ||
| 185 | $self->{raw_json} = $self->{json}->decode($content); | ||
| 186 | |||
| 187 | if ( $conf{stop_id} ) { | ||
| 188 | $self->parse_trips_at_stopover; | ||
| 189 | $self->parse_stop; | ||
| 190 | } | ||
| 191 | elsif ( $conf{trip_id} ) { | ||
| 192 | $self->parse_trip; | ||
| 193 | } | ||
| 194 | elsif ( $conf{stops_by_query} or $conf{stops_by_coordinate} ) { | ||
| 195 | $self->parse_stops_by; | ||
| 196 | } | ||
| 197 | |||
| 198 | if ( $self->errstr ) { | ||
| 199 | $promise->reject( $self->errstr, $self ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $promise->resolve($self); | ||
| 203 | } | ||
| 204 | |||
| 205 | return; | ||
| 206 | } | ||
| 207 | )->catch( | ||
| 208 | sub { | ||
| 209 | my ($err) = @_; | ||
| 210 | $promise->reject($err); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | )->wait; | ||
| 214 | |||
| 215 | return $promise; | ||
| 216 | } | ||
| 217 | |||
| 218 | # }}} | ||
| 219 | # {{{ Internal Helpers | ||
| 220 | |||
| 221 | sub get_with_cache { | ||
| 222 | my ( $self, $url ) = @_; | ||
| 223 | my $cache = $self->{cache}; | ||
| 224 | |||
| 225 | if ( $self->{developer_mode} ) { | ||
| 226 | say "GET $url"; | ||
| 227 | } | ||
| 228 | |||
| 229 | if ($cache) { | ||
| 230 | my $content = $cache->thaw($url); | ||
| 231 | if ($content) { | ||
| 232 | if ( $self->{developer_mode} ) { | ||
| 233 | say ' cache hit'; | ||
| 234 | } | ||
| 235 | |||
| 236 | return ( ${$content}, undef ); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | if ( $self->{developer_mode} ) { | ||
| 241 | say ' cache miss'; | ||
| 242 | } | ||
| 243 | |||
| 244 | my $reply = $self->{user_agent}->get($url); | ||
| 245 | |||
| 246 | if ( $reply->is_error ) { | ||
| 247 | return ( undef, $reply->status_line ); | ||
| 248 | } | ||
| 249 | |||
| 250 | my $content = $reply->content; | ||
| 251 | |||
| 252 | if ($cache) { | ||
| 253 | $cache->freeze( $url, \$content ); | ||
| 254 | } | ||
| 255 | |||
| 256 | return ( $content, undef ); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub get_with_cache_p { | ||
| 260 | my ( $self, $url ) = @_; | ||
| 261 | |||
| 262 | my $cache = $self->{cache}; | ||
| 263 | |||
| 264 | if ( $self->{developer_mode} ) { | ||
| 265 | say "GET $url"; | ||
| 266 | } | ||
| 267 | |||
| 268 | my $promise = $self->{promise}->new; | ||
| 269 | |||
| 270 | if ($cache) { | ||
| 271 | my $content = $cache->thaw($url); | ||
| 272 | if ($content) { | ||
| 273 | if ( $self->{developer_mode} ) { | ||
| 274 | say ' cache hit'; | ||
| 275 | } | ||
| 276 | |||
| 277 | return $promise->resolve( ${$content} ); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | if ( $self->{developer_mode} ) { | ||
| 282 | say ' cache miss'; | ||
| 283 | } | ||
| 284 | |||
| 285 | $self->{user_agent}->get_p($url)->then( | ||
| 286 | sub { | ||
| 287 | my ($tx) = @_; | ||
| 288 | if ( my $err = $tx->error ) { | ||
| 289 | $promise->reject( | ||
| 290 | "GET $url returned HTTP $err->{code} $err->{message}"); | ||
| 291 | |||
| 292 | return; | ||
| 293 | } | ||
| 294 | |||
| 295 | my $content = $tx->res->body; | ||
| 296 | |||
| 297 | if ($cache) { | ||
| 298 | $cache->freeze( $url, \$content ); | ||
| 299 | } | ||
| 300 | |||
| 301 | $promise->resolve($content); | ||
| 302 | return; | ||
| 303 | } | ||
| 304 | )->catch( | ||
| 305 | sub { | ||
| 306 | my ($err) = @_; | ||
| 307 | $promise->reject($err); | ||
| 308 | return; | ||
| 309 | } | ||
| 310 | )->wait; | ||
| 311 | |||
| 312 | return $promise; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub parse_trip { | ||
| 316 | my ( $self, %opt ) = @_; | ||
| 317 | |||
| 318 | $self->{result} = Travel::Status::MOTIS::Trip->new( | ||
| 319 | json => $self->{raw_json}, | ||
| 320 | time_zone => $self->{time_zone}, | ||
| 321 | ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub parse_stops_by { | ||
| 325 | my ($self) = @_; | ||
| 326 | |||
| 327 | @{ $self->{results} } = map { | ||
| 328 | $_->{type} eq 'STOP' | ||
| 329 | ? Travel::Status::MOTIS::Stop->from_match( json => $_ ) | ||
| 330 | : () | ||
| 331 | } @{ $self->{raw_json} // [] }; | ||
| 332 | |||
| 333 | return $self; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub parse_trips_at_stopover { | ||
| 337 | my ($self) = @_; | ||
| 338 | |||
| 339 | @{ $self->{results} } = map { | ||
| 340 | Travel::Status::MOTIS::TripAtStopover->new( | ||
| 341 | json => $_, | ||
| 342 | time_zone => $self->{time_zone}, | ||
| 343 | ) | ||
| 344 | } @{ $self->{raw_json}{stopTimes} // [] }; | ||
| 345 | |||
| 346 | return $self; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub parse_stop { | ||
| 350 | my ($self) = @_; | ||
| 351 | |||
| 352 | $self->{result} = Travel::Status::MOTIS::Stop->from_stopover( | ||
| 353 | json => $self->{raw_json}{place}, | ||
| 354 | ); | ||
| 355 | |||
| 356 | return $self; | ||
| 357 | } | ||
| 358 | |||
| 359 | # }}} | ||
| 360 | # {{{ Public Functions | ||
| 361 | |||
| 362 | sub errstr { | ||
| 363 | my ($self) = @_; | ||
| 364 | |||
| 365 | return $self->{errstr}; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub results { | ||
| 369 | my ($self) = @_; | ||
| 370 | return @{ $self->{results} }; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub result { | ||
| 374 | my ($self) = @_; | ||
| 375 | return $self->{result}; | ||
| 376 | } | ||
| 377 | |||
| 378 | # static | ||
| 379 | sub get_services { | ||
| 380 | my @services; | ||
| 381 | for my $service ( sort keys %{$motis_instance} ) { | ||
| 382 | my %desc = %{ $motis_instance->{$service} }; | ||
| 383 | $desc{shortname} = $service; | ||
| 384 | push( @services, \%desc ); | ||
| 385 | } | ||
| 386 | return @services; | ||
| 387 | } | ||
| 388 | |||
| 389 | # static | ||
| 390 | sub get_service { | ||
| 391 | my ($service) = @_; | ||
| 392 | |||
| 393 | if ( defined $service and exists $motis_instance->{$service} ) { | ||
| 394 | return $motis_instance->{$service}; | ||
| 395 | } | ||
| 396 | return; | ||
| 397 | } | ||
| 398 | |||
| 399 | # }}} | ||
| 400 | |||
| 401 | 1; | ||
| 402 | |||
| 403 | __END__ |