1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
|
package Travel::Routing::DE::EFA;
use strict;
use warnings;
use 5.010;
use utf8;
use Carp qw(cluck);
use Encode qw(encode);
use Travel::Routing::DE::EFA::Route;
use Travel::Routing::DE::EFA::Route::Message;
use LWP::UserAgent;
use XML::LibXML;
use Exception::Class (
'Travel::Routing::DE::EFA::Exception',
'Travel::Routing::DE::EFA::Exception::Setup' => {
isa => 'Travel::Routing::DE::EFA::Exception',
description => 'invalid argument on setup',
fields => [ 'option', 'have', 'want' ],
},
'Travel::Routing::DE::EFA::Exception::Net' => {
isa => 'Travel::Routing::DE::EFA::Exception',
description => 'could not submit POST request',
fields => 'http_response',
},
'Travel::Routing::DE::EFA::Exception::NoData' => {
isa => 'Travel::Routing::DE::EFA::Exception',
description => 'backend returned no parsable route',
},
'Travel::Routing::DE::EFA::Exception::Ambiguous' => {
isa => 'Travel::Routing::DE::EFA::Exception',
description => 'ambiguous input',
fields => [ 'post_key', 'post_value', 'possibilities' ],
},
'Travel::Routing::DE::EFA::Exception::Other' => {
isa => 'Travel::Routing::DE::EFA::Exception',
description => 'EFA backend returned an error',
fields => ['message'],
},
);
our $VERSION = '2.24';
sub set_time {
my ( $self, %conf ) = @_;
my $time;
if ( $conf{departure_time} ) {
$self->{post}->{itdTripDateTimeDepArr} = 'dep';
$time = $conf{departure_time};
}
elsif ( $conf{arrival_time} ) {
$self->{post}->{itdTripDateTimeDepArr} = 'arr';
$time = $conf{arrival_time};
}
else {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'time',
error => 'Specify either departure_time or arrival_time'
);
}
if ( $time !~ / ^ [0-2]? \d : [0-5]? \d $ /x ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'time',
have => $time,
want => 'HH:MM',
);
}
@{ $self->{post} }{ 'itdTimeHour', 'itdTimeMinute' } = split( /:/, $time );
return;
}
sub departure_time {
my ( $self, $time ) = @_;
return $self->set_time( departure_time => $time );
}
sub arrival_time {
my ( $self, $time ) = @_;
return $self->set_time( arrival_time => $time );
}
sub date {
my ( $self, $date ) = @_;
my ( $day, $month, $year ) = split( /[.]/, $date );
if ( $date eq 'tomorrow' ) {
( undef, undef, undef, $day, $month, $year )
= localtime( time + 86400 );
$month += 1;
$year += 1900;
}
if (
not( defined $day
and length($day)
and $day >= 1
and $day <= 31
and defined $month
and length($month)
and $month >= 1
and $month <= 12 )
)
{
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'date',
have => $date,
want => 'DD.MM[.[YYYY]]'
);
}
if ( not defined $year or not length($year) ) {
$year = ( localtime(time) )[5] + 1900;
}
@{ $self->{post} }{ 'itdDateDay', 'itdDateMonth', 'itdDateYear' }
= ( $day, $month, $year );
return;
}
sub exclude {
my ( $self, @exclude ) = @_;
my @mapping = qw{
zug s-bahn u-bahn stadtbahn tram stadtbus regionalbus
schnellbus seilbahn schiff ast sonstige
};
foreach my $exclude_type (@exclude) {
my $ok = 0;
for my $map_id ( 0 .. $#mapping ) {
if ( $exclude_type eq $mapping[$map_id] ) {
delete $self->{post}->{"inclMOT_${map_id}"};
$ok = 1;
}
}
if ( not $ok ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'exclude',
have => $exclude_type,
want => join( ' / ', @mapping ),
);
}
}
return;
}
sub max_interchanges {
my ( $self, $max ) = @_;
$self->{post}->{maxChanges} = $max;
return;
}
sub number_of_trips {
my ( $self, $num ) = @_;
$self->{post}->{calcNumberOfTrips} = $num;
return;
}
sub select_interchange_by {
my ( $self, $prefer ) = @_;
if ( $prefer eq 'speed' ) { $self->{post}->{routeType} = 'LEASTTIME' }
elsif ( $prefer eq 'waittime' ) {
$self->{post}->{routeType} = 'LEASTINTERCHANGE';
}
elsif ( $prefer eq 'distance' ) {
$self->{post}->{routeType} = 'LEASTWALKING';
}
else {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'select_interchange_by',
have => $prefer,
want => 'speed / waittime / distance',
);
}
return;
}
sub train_type {
my ( $self, $include ) = @_;
if ( $include eq 'local' ) { $self->{post}->{lineRestriction} = 403 }
elsif ( $include eq 'ic' ) { $self->{post}->{lineRestriction} = 401 }
elsif ( $include eq 'ice' ) { $self->{post}->{lineRestriction} = 400 }
else {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'train_type',
have => $include,
want => 'local / ic / ice',
);
}
return;
}
sub use_near_stops {
my ( $self, $duration ) = @_;
if ($duration) {
$self->{post}->{useProxFootSearch} = 1;
$self->{post}->{trITArrMOTvalue100} = $duration;
$self->{post}->{trITDepMOTvalue100} = $duration;
}
else {
$self->{post}->{useProxFootSearch} = 0;
}
return;
}
sub walk_speed {
my ( $self, $walk_speed ) = @_;
if ( $walk_speed =~ m{ ^ (?: normal | fast | slow ) $ }x ) {
$self->{post}->{changeSpeed} = $walk_speed;
}
else {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'walk_speed',
have => $walk_speed,
want => 'normal / fast / slow',
);
}
return;
}
sub with_bike {
my ( $self, $bike ) = @_;
$self->{post}->{bikeTakeAlong} = $bike;
return;
}
sub without_solid_stairs {
my ( $self, $opt ) = @_;
$self->{post}->{noSolidStairs} = $opt;
return;
}
sub without_escalators {
my ( $self, $opt ) = @_;
$self->{post}->{noEscalators} = $opt;
return;
}
sub without_elevators {
my ( $self, $opt ) = @_;
$self->{post}->{noElevators} = $opt;
return;
}
sub with_low_platform {
my ( $self, $opt ) = @_;
$self->{post}->{lowPlatformVhcl} = $opt;
return;
}
sub with_wheelchair {
my ( $self, $opt ) = @_;
$self->{post}->{wheelchair} = $opt;
return;
}
sub place {
my ( $self, $which, $place, $stop, $type ) = @_;
if ( not( $place and $stop ) ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'place',
error => 'Need >= three elements'
);
}
$type //= 'stop';
@{ $self->{post} }{ "place_${which}", "name_${which}" } = ( $place, $stop );
if ( $type =~ m{ ^ (?: address | poi | stop ) $ }x ) {
$self->{post}->{"type_${which}"} = $type;
}
return;
}
sub create_post {
my ($self) = @_;
my $conf = $self->{config};
my @now = localtime( time() );
$self->{post} = {
changeSpeed => 'normal',
command => q{},
execInst => q{},
imparedOptionsActive => 1,
inclMOT_0 => 'on',
inclMOT_1 => 'on',
inclMOT_10 => 'on',
inclMOT_11 => 'on',
inclMOT_2 => 'on',
inclMOT_3 => 'on',
inclMOT_4 => 'on',
inclMOT_5 => 'on',
inclMOT_6 => 'on',
inclMOT_7 => 'on',
inclMOT_8 => 'on',
inclMOT_9 => 'on',
includedMeans => 'checkbox',
itOptionsActive => 1,
itdDateDay => $now[3],
itdDateMonth => $now[4] + 1,
itdDateYear => $now[5] + 1900,
itdTimeHour => $now[2],
itdTimeMinute => $now[1],
itdTripDateTimeDepArr => 'dep',
language => 'de',
lineRestriction => 403,
maxChanges => 9,
nameInfo_destination => 'invalid',
nameInfo_origin => 'invalid',
nameInfo_via => 'invalid',
nameState_destination => 'empty',
nameState_origin => 'empty',
nameState_via => 'empty',
name_destination => q{},
name_origin => q{},
name_via => q{},
nextDepsPerLeg => 1,
outputFormat => 'XML',
placeInfo_destination => 'invalid',
placeInfo_origin => 'invalid',
placeInfo_via => 'invalid',
placeState_destination => 'empty',
placeState_origin => 'empty',
placeState_via => 'empty',
place_destination => q{},
place_origin => q{},
place_via => q{},
ptOptionsActive => 1,
requestID => 0,
routeType => 'LEASTTIME',
sessionID => 0,
text => 1993,
trITArrMOT => 100,
trITArrMOTvalue100 => 10,
trITArrMOTvalue101 => 10,
trITArrMOTvalue104 => 10,
trITArrMOTvalue105 => 10,
trITDepMOT => 100,
trITDepMOTvalue100 => 10,
trITDepMOTvalue101 => 10,
trITDepMOTvalue104 => 10,
trITDepMOTvalue105 => 10,
typeInfo_destination => 'invalid',
typeInfo_origin => 'invalid',
typeInfo_via => 'invalid',
type_destination => 'stop',
type_origin => 'stop',
type_via => 'stop',
useRealtime => 1
};
$self->place( 'origin', @{ $conf->{origin} } );
$self->place( 'destination', @{ $conf->{destination} } );
if ( $conf->{via} ) {
$self->place( 'via', @{ $conf->{via} } );
}
if ( $conf->{arrival_time} || $conf->{departure_time} ) {
$self->set_time( %{$conf} );
}
if ( $conf->{date} ) {
$self->date( $conf->{date} );
}
if ( $conf->{exclude} ) {
$self->exclude( @{ $conf->{exclude} } );
}
if ( $conf->{max_interchanges} ) {
$self->max_interchanges( $conf->{max_interchanges} );
}
if ( $conf->{num_results} ) {
$self->number_of_trips( $conf->{num_results} );
}
if ( $conf->{select_interchange_by} ) {
$self->select_interchange_by( $conf->{select_interchange_by} );
}
if ( $conf->{use_near_stops} ) {
$self->use_near_stops( $conf->{use_near_stops} );
}
if ( $conf->{train_type} ) {
$self->train_type( $conf->{train_type} );
}
if ( $conf->{walk_speed} ) {
$self->walk_speed( $conf->{walk_speed} );
}
if ( $conf->{with_bike} ) {
$self->with_bike(1);
}
if ( $conf->{with_low_platform} ) {
$self->with_low_platform(1);
}
if ( $conf->{with_wheelchair} ) {
$self->with_wheelchair(1);
}
if ( $conf->{without_solid_stairs} ) {
$self->without_solid_stairs(1);
}
if ( $conf->{without_escalators} ) {
$self->without_escalators(1);
}
if ( $conf->{without_elevators} ) {
$self->without_elevators(1);
}
for my $val ( values %{ $self->{post} } ) {
$val = encode( 'UTF-8', $val );
}
return;
}
sub new {
my ( $obj, %conf ) = @_;
my $ref = {};
$ref->{config} = \%conf;
bless( $ref, $obj );
if ( not $ref->{config}->{efa_url} ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'efa_url',
error => 'must be set'
);
}
$ref->{config}->{efa_url} =~ m{
(?<netroot> (?<root> [^:]+ : // [^/]+ ) / [^/]+ / )
}ox;
$ref->{config}->{rm_base} = $+{netroot};
$ref->{config}->{sm_base} = $+{root} . '/download/envmaps/';
$ref->create_post;
if ( not( defined $conf{submit} and $conf{submit} == 0 ) ) {
$ref->submit( %{ $conf{lwp_options} } );
}
return $ref;
}
sub new_from_xml {
my ( $class, %opt ) = @_;
my $self = { xml_reply => $opt{xml} };
$self->{config} = {
efa_url => $opt{efa_url},
};
$self->{config}->{efa_url} =~ m{
(?<netroot> (?<root> [^:]+ : // [^/]+ ) / [^/]+ / )
}ox;
$self->{config}->{rm_base} = $+{netroot};
$self->{config}->{sm_base} = $+{root} . '/download/envmaps/';
bless( $self, $class );
$self->parse_xml;
return $self;
}
sub submit {
my ( $self, %conf ) = @_;
$self->{ua} = LWP::UserAgent->new(%conf);
$self->{ua}->env_proxy;
my $response
= $self->{ua}->post( $self->{config}->{efa_url}, $self->{post} );
if ( $response->is_error ) {
Travel::Routing::DE::EFA::Exception::Net->throw(
http_response => $response,
);
}
$self->{xml_reply} = $response->decoded_content;
$self->parse_xml;
return;
}
sub itddate_str {
my ( $self, $node ) = @_;
return sprintf( '%02d.%02d.%04d',
$node->getAttribute('day'),
$node->getAttribute('month'),
$node->getAttribute('year') );
}
sub itdtime_str {
my ( $self, $node ) = @_;
return sprintf( '%02d:%02d',
$node->getAttribute('hour'),
$node->getAttribute('minute') );
}
sub parse_cur_info {
my ( $self, $node ) = @_;
my $xp_text = XML::LibXML::XPathExpression->new('./infoLinkText');
my $xp_subject = XML::LibXML::XPathExpression->new('./infoText/subject');
my $xp_subtitle = XML::LibXML::XPathExpression->new('./infoText/subtitle');
my $xp_content = XML::LibXML::XPathExpression->new('./infoText/content');
my $e_text = ( $node->findnodes($xp_text) )[0];
my $e_subject = ( $node->findnodes($xp_subject) )[0];
my $e_subtitle = ( $node->findnodes($xp_subtitle) )[0];
my $e_content = ( $node->findnodes($xp_content) )[0];
my %msg = (
summary => $e_text->textContent,
subject => $e_subject->textContent,
subtitle => $e_subtitle->textContent,
raw_content => $e_content->textContent,
);
for my $key ( keys %msg ) {
chomp( $msg{$key} );
}
return Travel::Routing::DE::EFA::Route::Message->new(%msg);
}
sub parse_reg_info {
my ( $self, $node ) = @_;
my %msg = (
summary => $node->textContent,
);
return Travel::Routing::DE::EFA::Route::Message->new(%msg);
}
sub parse_xml_part {
my ( $self, $route ) = @_;
my $xp_route = XML::LibXML::XPathExpression->new(
'./itdPartialRouteList/itdPartialRoute');
my $xp_dep
= XML::LibXML::XPathExpression->new('./itdPoint[@usage="departure"]');
my $xp_arr
= XML::LibXML::XPathExpression->new('./itdPoint[@usage="arrival"]');
my $xp_date = XML::LibXML::XPathExpression->new('./itdDateTime/itdDate');
my $xp_time = XML::LibXML::XPathExpression->new('./itdDateTime/itdTime');
my $xp_via = XML::LibXML::XPathExpression->new('./itdStopSeq/itdPoint');
my $xp_sdate
= XML::LibXML::XPathExpression->new('./itdDateTimeTarget/itdDate');
my $xp_stime
= XML::LibXML::XPathExpression->new('./itdDateTimeTarget/itdTime');
my $xp_mot = XML::LibXML::XPathExpression->new('./itdMeansOfTransport');
my $xp_fp = XML::LibXML::XPathExpression->new('./itdFootPathInfo');
my $xp_fp_e
= XML::LibXML::XPathExpression->new('./itdFootPathInfo/itdFootPathElem');
my $xp_delay = XML::LibXML::XPathExpression->new('./itdRBLControlled');
my $xp_sched_info
= XML::LibXML::XPathExpression->new('./itdInfoTextList/infoTextListElem');
my $xp_cur_info = XML::LibXML::XPathExpression->new('./infoLink');
my $xp_mapitem_rm = XML::LibXML::XPathExpression->new(
'./itdMapItemList/itdMapItem[@type="RM"]/itdImage');
my $xp_mapitem_sm = XML::LibXML::XPathExpression->new(
'./itdMapItemList/itdMapItem[@type="SM"]/itdImage');
my $xp_fare
= XML::LibXML::XPathExpression->new('./itdFare/itdSingleTicket');
my @route_parts;
my $info = {
duration => $route->getAttribute('publicDuration'),
vehicle_time => $route->getAttribute('vehicleTime'),
};
my $e_fare = ( $route->findnodes($xp_fare) )[0];
if ($e_fare) {
$info->{ticket_type} = $e_fare->getAttribute('unitsAdult');
$info->{fare_adult} = $e_fare->getAttribute('fareAdult');
$info->{fare_child} = $e_fare->getAttribute('fareChild');
$info->{ticket_text} = $e_fare->textContent;
}
for my $e ( $route->findnodes($xp_route) ) {
my $e_dep = ( $e->findnodes($xp_dep) )[0];
my $e_arr = ( $e->findnodes($xp_arr) )[0];
my $e_ddate = ( $e_dep->findnodes($xp_date) )[0];
my $e_dtime = ( $e_dep->findnodes($xp_time) )[0];
my $e_dsdate = ( $e_dep->findnodes($xp_sdate) )[0];
my $e_dstime = ( $e_dep->findnodes($xp_stime) )[0];
my $e_adate = ( $e_arr->findnodes($xp_date) )[0];
my $e_atime = ( $e_arr->findnodes($xp_time) )[0];
my $e_asdate = ( $e_arr->findnodes($xp_sdate) )[0];
my $e_astime = ( $e_arr->findnodes($xp_stime) )[0];
my $e_mot = ( $e->findnodes($xp_mot) )[0];
my $e_delay = ( $e->findnodes($xp_delay) )[0];
my $e_fp = ( $e->findnodes($xp_fp) )[0];
my @e_sinfo = $e->findnodes($xp_sched_info);
my @e_cinfo = $e->findnodes($xp_cur_info);
my @e_dmap_rm = $e_dep->findnodes($xp_mapitem_rm);
my @e_dmap_sm = $e_dep->findnodes($xp_mapitem_sm);
my @e_amap_rm = $e_arr->findnodes($xp_mapitem_rm);
my @e_amap_sm = $e_arr->findnodes($xp_mapitem_sm);
my @e_fp_e = $e->findnodes($xp_fp_e);
# not all EFA services distinguish between scheduled and realtime
# data. Set sdate / stime to date / time when not provided.
$e_dsdate //= $e_ddate;
$e_dstime //= $e_dtime;
$e_asdate //= $e_adate;
$e_astime //= $e_atime;
my $delay = $e_delay ? $e_delay->getAttribute('delayMinutes') : 0;
my $delay_arr
= $e_delay ? $e_delay->getAttribute('delayMinutesArr') : 0;
my ( @dep_rms, @dep_sms, @arr_rms, @arr_sms );
if ( $self->{config}->{rm_base} ) {
my $base = $self->{config}->{rm_base};
@dep_rms = map { $base . $_->getAttribute('src') } @e_dmap_rm;
@arr_rms = map { $base . $_->getAttribute('src') } @e_amap_rm;
}
if ( $self->{config}->{sm_base} ) {
my $base = $self->{config}->{sm_base};
@dep_sms = map { $base . $_->getAttribute('src') } @e_dmap_sm;
@arr_sms = map { $base . $_->getAttribute('src') } @e_amap_sm;
}
my $hash = {
departure_date => $self->itddate_str($e_ddate),
departure_delay => $delay,
departure_time => $self->itdtime_str($e_dtime),
departure_sdate => $self->itddate_str($e_dsdate),
departure_stime => $self->itdtime_str($e_dstime),
departure_stop => $e_dep->getAttribute('name'),
departure_platform => $e_dep->getAttribute('platformName'),
occupancy => $e_dep->getAttribute('occupancy'),
train_line => $e_mot->getAttribute('name'),
train_product => $e_mot->getAttribute('productName'),
train_destination => $e_mot->getAttribute('destination'),
arrival_date => $self->itddate_str($e_adate),
arrival_delay => $delay_arr,
arrival_time => $self->itdtime_str($e_atime),
arrival_sdate => $self->itddate_str($e_asdate),
arrival_stime => $self->itdtime_str($e_astime),
arrival_stop => $e_arr->getAttribute('name'),
arrival_platform => $e_arr->getAttribute('platformName'),
};
if ($e_fp) {
# Note that position=IDEST footpaths are coupled with a special
# "walking" connection, so their duration is already known and
# accounted for. However, we still save it here, since
# detecting and handling this is the API client's job (for now).
$hash->{footpath_type} = $e_fp->getAttribute('position');
$hash->{footpath_duration} = $e_fp->getAttribute('duration');
for my $e (@e_fp_e) {
push(
@{ $hash->{footpath_parts} },
[ $e->getAttribute('type'), $e->getAttribute('level') ]
);
}
}
$hash->{departure_routemaps} = \@dep_rms;
$hash->{departure_stationmaps} = \@dep_sms;
$hash->{arrival_routemaps} = \@arr_rms;
$hash->{arrival_stationmaps} = \@arr_sms;
for my $ve ( $e->findnodes($xp_via) ) {
my $e_vdate = ( $ve->findnodes($xp_date) )[0];
my $e_vtime = ( $ve->findnodes($xp_time) )[0];
if ( not( $e_vdate and $e_vtime )
or ( $e_vdate->getAttribute('weekday') == -1 ) )
{
next;
}
my $name = $ve->getAttribute('name');
my $platform = $ve->getAttribute('platformName');
my $arr_delay = $ve->getAttribute('arrDelay');
if ( $name eq $hash->{departure_stop}
or $name eq $hash->{arrival_stop} )
{
next;
}
push(
@{ $hash->{via} },
[
$self->itddate_str($e_vdate),
$self->itdtime_str($e_vtime),
$name,
$platform,
$arr_delay,
]
);
}
$hash->{regular_notes}
= [ map { $self->parse_reg_info($_) } @e_sinfo ];
$hash->{current_notes} = [ map { $self->parse_cur_info($_) } @e_cinfo ];
push( @route_parts, $hash );
}
push(
@{ $self->{routes} },
Travel::Routing::DE::EFA::Route->new( $info, @route_parts )
);
return;
}
sub parse_xml {
my ($self) = @_;
my $tree = $self->{tree} = XML::LibXML->load_xml(
string => $self->{xml_reply},
);
if ( $self->{config}->{developer_mode} ) {
say $tree->toString(2);
}
my $xp_element = XML::LibXML::XPathExpression->new(
'//itdItinerary/itdRouteList/itdRoute');
my $xp_err = XML::LibXML::XPathExpression->new(
'//itdTripRequest/itdMessage[@type="error"]');
my $xp_odv = XML::LibXML::XPathExpression->new('//itdOdv');
for my $odv ( $tree->findnodes($xp_odv) ) {
$self->check_ambiguous_xml($odv);
}
my $err = ( $tree->findnodes($xp_err) )[0];
if ($err) {
Travel::Routing::DE::EFA::Exception::Other->throw(
message => $err->textContent );
}
for my $part ( $tree->findnodes($xp_element) ) {
$self->parse_xml_part($part);
}
if ( not defined $self->{routes} or @{ $self->{routes} } == 0 ) {
Travel::Routing::DE::EFA::Exception::NoData->throw;
}
return 1;
}
sub check_ambiguous_xml {
my ( $self, $tree ) = @_;
my $xp_place = XML::LibXML::XPathExpression->new('./itdOdvPlace');
my $xp_name = XML::LibXML::XPathExpression->new('./itdOdvName');
my $xp_place_elem = XML::LibXML::XPathExpression->new('./odvPlaceElem');
my $xp_place_input = XML::LibXML::XPathExpression->new('./odvPlaceInput');
my $xp_name_elem = XML::LibXML::XPathExpression->new('./odvNameElem');
my $xp_name_input = XML::LibXML::XPathExpression->new('./odvNameInput');
my $e_place = ( $tree->findnodes($xp_place) )[0];
my $e_name = ( $tree->findnodes($xp_name) )[0];
if ( not( $e_place and $e_name ) ) {
cluck('skipping ambiguity check - itdOdvPlace/itdOdvName missing');
return;
}
my $s_place = $e_place->getAttribute('state');
my $s_name = $e_name->getAttribute('state');
if ( $s_place eq 'list' ) {
Travel::Routing::DE::EFA::Exception::Ambiguous->throw(
post_key => 'place',
post_value =>
( $e_place->findnodes($xp_place_input) )[0]->textContent,
possibilities => join( q{ | },
map { $_->textContent }
@{ $e_place->findnodes($xp_place_elem) } )
);
}
if ( $s_name eq 'list' ) {
Travel::Routing::DE::EFA::Exception::Ambiguous->throw(
post_key => 'name',
post_value =>
( $e_name->findnodes($xp_name_input) )[0]->textContent,
possibilities => join( q{ | },
map { $_->textContent } @{ $e_name->findnodes($xp_name_elem) } )
);
}
if ( $s_place eq 'notidentified' ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'place',
error => 'unknown place',
have => ( $e_place->findnodes($xp_place_input) )[0]->textContent,
);
}
if ( $s_name eq 'notidentified' ) {
Travel::Routing::DE::EFA::Exception::Setup->throw(
option => 'name',
error => 'unknown name',
have => ( $e_name->findnodes($xp_name_input) )[0]->textContent,
);
}
# 'identified' and 'empty' are ok
return;
}
sub routes {
my ($self) = @_;
return @{ $self->{routes} };
}
# static
sub get_efa_urls {
# sorted lexically by shortname
return (
{
url => 'https://bsvg.efa.de/bsvagstd/XML_TRIP_REQUEST2',
name => 'Braunschweiger Verkehrs-GmbH',
shortname => 'BSVG',
},
{
url => 'https://www.ding.eu/ding3/XSLT_TRIP_REQUEST2',
name => 'Donau-Iller Nahverkehrsverbund',
shortname => 'DING',
},
{
url => 'https://projekte.kvv-efa.de/sl3-alone/XSLT_TRIP_REQUEST2',
name => 'Karlsruher Verkehrsverbund',
shortname => 'KVV',
},
{
url => 'https://www.linzag.at/static/XSLT_TRIP_REQUEST2',
name => 'Linz AG',
shortname => 'LinzAG',
},
{
url => 'https://efa.mvv-muenchen.de/mobile/XSLT_TRIP_REQUEST2',
name => 'Münchner Verkehrs- und Tarifverbund',
shortname => 'MVV',
},
{
url => 'https://www.efa-bw.de/nvbw/XSLT_TRIP_REQUEST2',
name => 'Nahverkehrsgesellschaft Baden-Württemberg',
shortname => 'NVBW',
},
{
url => 'https://efa.vagfr.de/vagfr3/XSLT_TRIP_REQUEST2',
name => 'Freiburger Verkehrs AG',
shortname => 'VAG',
},
{
url => 'https://efa.vgn.de/vgnExt_oeffi/XML_TRIP_REQUEST2',
name => 'Verkehrsverbund Grossraum Nuernberg',
shortname => 'VGN',
},
# HTTPS: certificate verification fails
{
url => 'http://efa.vmv-mbh.de/vmv/XML_TRIP_REQUEST2',
name => 'Verkehrsgesellschaft Mecklenburg-Vorpommern',
shortname => 'VMV',
},
{
url => 'https://www.vrn.de/mngvrn/XML_TRIP_REQUEST2',
name => 'Verkehrsverbund Rhein-Neckar',
shortname => 'VRN',
},
{
url => 'https://app.vrr.de/vrrstd/XML_TRIP_REQUEST2',
name => 'Verkehrsverbund Rhein-Ruhr',
shortname => 'VRR',
},
{
url => 'https://efa.vrr.de/rbgstd3/XSLT_TRIP_REQUEST2',
name => 'Verkehrsverbund Rhein-Ruhr (alternative)',
shortname => 'VRR2',
},
{
url => 'https://efa.vvo-online.de/VMSSL3/XSLT_TRIP_REQUEST2',
name => 'Verkehrsverbund Oberelbe',
shortname => 'VVO',
},
{
url => 'https://www2.vvs.de/vvs/XSLT_TRIP_REQUEST2',
name => 'Verkehrsverbund Stuttgart',
shortname => 'VVS',
},
);
}
1;
__END__
=head1 NAME
Travel::Routing::DE::EFA - unofficial interface to EFA-based itinerary services
=head1 SYNOPSIS
use Travel::Routing::DE::EFA;
my $efa = Travel::Routing::DE::EFA->new(
efa_url => 'https://app.vrr.de/vrrstd/XML_TRIP_REQUEST2';',
origin => [ 'Essen', 'HBf' ],
destination => [ 'Duisburg', 'HBf' ],
);
for my $route ( $efa->routes ) {
for my $part ( $route->parts ) {
printf(
"%s at %s -> %s at %s, via %s to %s\n",
$part->departure_time, $part->departure_stop,
$part->arrival_time, $part->arrival_stop,
$part->train_line, $part->train_destination,
);
}
print "\n";
}
=head1 VERSION
version 2.24
=head1 DESCRIPTION
B<Travel::Routing::DE::EFA> is a client for EFA-based itinerary services.
You pass it the start/stop of your journey, maybe a time and a date and more
details, and it returns the up-to-date scheduled connections between those two
stops.
It uses B<LWP::UserAgent> and B<XML::LibXML> for this.
=head1 METHODS
=over
=item $efa = Travel::Routing::DE::EFA->new(I<%opts>)
Returns a new Travel::Routing::DE::EFA object and sets up its POST data via
%opts.
Valid hash keys and their values are:
=over
=item B<efa_url> => I<efa_url>
Mandatory. Sets the entry point to the EFA itinerary service.
See C<< efa --list >> for a list of supported services.
If you found a URL not listed there, please send it to
E<lt>derf@finalrewind.orgE<gt>.
=item B<origin> => B<[> I<city>B<,> I<stop> [ B<,> I<type> ] B<]>
Mandatory. Sets the start of the journey.
I<type> is optional and may be one of B<stop> (default), B<address> (street
and house number) or B<poi> ("point of interest").
=item B<destination> => B<[> I<city>B<,> I<stop> [ B<,> I<type> ] B<]>
Mandatory. Sets the end of the journey, see B<origin>.
=item B<via> => B<[> I<city>B<,> I<stop> [ B<,> I<type> ] B<]>
Optional. Specifies an intermediate stop which the resulting itinerary must
contain. See B<origin> for arguments.
=item B<arrival_time> => I<HH:MM>
Journey end time
=item B<departure_time> => I<HH:MM>
Journey start time. Default: now
=item B<date> => I<DD.MM.>[I<YYYY>]
Journey date. Also accepts the string B<tomorrow>. Default: today
=item B<exclude> => \@exclude
Do not use certain transport types for itinerary. Accepted arguments:
zug, s-bahn, u-bahn, stadtbahn, tram, stadtbus, regionalbus, schnellbus,
seilbahn, schiff, ast, sonstige
=item B<max_interchanges> => I<num>
Set maximum number of interchanges
=item B<num_results> => I<num>
Return up to I<num> connections. If unset, the default of the respective
EFA server is used (usually 4 or 5).
=item B<select_interchange_by> => B<speed>|B<waittime>|B<distance>
Prefer either fast connections (default), connections with low wait time or
connections with little distance to walk
=item B<use_near_stops> => I<$int>
If I<$int> is a true value: Take stops close to the stop/start into account and
possibly use them instead. Up to I<$int> minutes of walking are considered
acceptable.
Otherwise: Do not take stops close to stop/start into account.
=item B<train_type> => B<local>|B<ic>|B<ice>
Include only local trains into itinerary (default), all but ICEs, or all.
The latter two are usually way more expensive for short routes.
=item B<walk_speed> => B<slow>|B<fast>|B<normal>
Set walk speed. Default: B<normal>
=item B<with_bike> => B<0>|B<1>
If true: Request connections allowing passengers with bikes. Note that the
backed may return an empty result if no such connection exists or bike-support
simply isn't known.
=item B<with_low_platform> => B<0>|B<1>
If true: Request connections which only use low-platform ("Niederflur")
vehicles. Note that the backed will return an empty result if no such
connection exists.
=item B<with_wheelchair> => B<0>|B<1>
If true: Request connections which are wheelchair-accessible. Again, note that
the backend may return an empty result if no such connection exists or
wheelchair-support isn't known.
=item B<without_elevators> => B<0>|B<1>
If true: Request that transfers do not require usage of elevators.
=item B<without_escalators> => B<0>|B<1>
If true: Request that transfers do not require usage of escalators.
=item B<without_solid_stairs> => B<0>|B<1>
If true: Request that transfers do not require stairs to be taken (i.e.
ramps, escalators, elevators or similar must be available).
=item B<lwp_options> => I<\%hashref>
Options to pass to C<< LWP::UserAgent->new >>.
=item B<submit> => B<0>|B<1>
By default, B<new> will create a POST request and submit it. If you do not
want it to be submitted yet, set this to B<0>.
=back
=item $efa->submit(I<%opts>)
Submit the query to I<efa_url>.
I<%opts> is passed on to C<< LWP::UserAgent->new >>.
=item $efa->routes
Returns a list of Travel::Routing::DE::EFA::Route(3pm) elements. Each one contains
one method of getting from start to stop.
=back
=head2 ACCESSORS
The following methods act like the arguments to B<new>. See there.
=over
=item $efa->departure_time(I<$time>)
=item $efa->arrival_time(I<$time>)
=item $efa->date(I<$date>)
=item $efa->exclude(I<@exclude>)
=item $efa->max_interchanges(I<$num>)
=item $efa->select_interchange_by(I<$selection>)
=item $efa->train_type(I<$type>)
=item $efa->use_near_stops(I<$duration>)
=item $efa->walk_speed(I<$speed>)
=item $efa->with_bike(I<$bool>)
=back
=head2 STATIC METHODS
=over
=item Travel::Routing::DE::EFA::get_efa_urls()
Returns a list of known EFA entry points. Each list element is a hashref with
the following elements.
=over
=item B<url>: service URL as passed to B<efa_url>
=item B<name>: Name of the entity operating this service
=item B<shortname>: Short name of the entity
=back
=back
=head1 DIAGNOSTICS
When encountering an error, Travel::Routing::DE::EFA throws a
Travel::Routing::DE::EFA::Exception(3pm) object.
=head1 DEPENDENCIES
=over
=item * LWP::UserAgent(3pm)
=item * XML::LibXML(3pm)
=back
=head1 BUGS AND LIMITATIONS
None known.
=head1 SEE ALSO
=over
=item * Travel::Routing::DE::EFA::Exception(3pm)
=item * Travel::Routing::DE::EFA::Route(3pm)
=item * L<WWW::EFA> is another implementation, using L<Moose>.
=back
=head1 AUTHOR
Copyright (C) 2009-2023 by Birte Kristina Friesel E<lt>derf@finalrewind.orgE<gt>
=head1 LICENSE
This program is licensed under the same terms as Perl itself.
|