-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-hafas.pl
executable file
·71 lines (67 loc) · 1.4 KB
/
json-hafas.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
use Travel::Status::DE::HAFAS;
my $result = Travel::Status::DE::HAFAS->new(
service => $ARGV[0],
journey => {id => $ARGV[1]},
with_polyline=>1
);
if (my $status = $result->result) {
my @polyline;
my $only_eva = 1;
foreach my $point ($status->polyline) {
if (not defined $point->{eva}) {
$only_eva=0;
}
push(@polyline, {
lat=>$point->{lat},
lon=>$point->{lon},
eva=>$point->{eva} // JSON::null,
name=>$point->{name} // JSON::null
});
}
my @messages;
foreach my $message ($status->messages) {
push(@messages, {
short=>$message->short,
text=>$message->text,
code=>$message->code,
type=>$message->type,
});
}
my @route;
my %stop_messages;
foreach my $stop ($status->route) {
push(@route, {
name=>$stop->loc->name,
eva=>$stop->loc->eva
});
my @ret;
foreach my $message ($stop->messages) {
push(@ret, {
short=>$message->short,
text=>$message->text,
code=>$message->code,
type=>$message->type,
});
}
@stop_messages{$stop->loc->eva} = [@ret];
}
print encode_json({
id=>$status->id,
operator=>$status->operator,
direction=>$status->direction,
polyline=>[@polyline],
beeline=>$only_eva,
route=>[@route],
messages=>[@messages],
stop_messages=>{%stop_messages}
});
} else {
print encode_json({
error_code => $result->errcode,
error_string => $result->errstr
});
}