summaryrefslogtreecommitdiff
path: root/share/enhance
blob: 2f0a4a45bfff3d97004c2bd41cf512db7d4c812e (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use File::Slurp qw(read_file write_file);
use JSON;
use LWP::UserAgent;

my $json_str = read_file('stations.json');
my $stations = JSON->new->utf8->decode($json_str);
@{$stations} = sort { $a->{name} cmp $b->{name} } @{$stations};

my $ua = LWP::UserAgent->new();
$ua->env_proxy;

for my $station ( @{$stations} ) {
	if ( not $station->{latlong} ) {
		say "Requesting location for $station->{name} ...";
		my $res = $ua->get(
			sprintf( 'https://v5.db.transport.rest/stops/%07d',
				$station->{eva} )
		);
		if ( $res->is_error ) {
			say '    transport.rest returned error ' . $res->status_line;
		}
		else {
			my $content = $res->decoded_content;
			my $json    = JSON->new->utf8->decode($content);
			if ( $json and $json->{location} and $json->{location}{latitude} ) {
				$station->{latlong} = [ $json->{location}{latitude},
					$json->{location}{longitude} ];
			}
			else {
				say '    transport.rest has no location';
			}
		}
	}
}

my $json_out = JSON->new->utf8->canonical->pretty->encode($stations);
write_file( 'stations.json', $json_out );