diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-12-16 20:49:59 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-12-16 20:49:59 +0100 |
commit | 40c836802b1ffb9a0a894c26a45ee628d1076b9c (patch) | |
tree | f94a930bc5538a73679a215e2a03ef05c6d1bd12 /share | |
parent | b43d69441e2a363d5436f8ac9c951dce39ec4a45 (diff) |
add enhance helper (fetch location data from marudor.de)
Diffstat (limited to 'share')
-rwxr-xr-x | share/enhance | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/share/enhance b/share/enhance new file mode 100755 index 0000000..5fe683f --- /dev/null +++ b/share/enhance @@ -0,0 +1,41 @@ +#!/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( + 'https://marudor.de/api/station/v1/station/' . $station->{eva} ); + if ( $res->is_error ) { + say ' marudor.de 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 ' marudor.de has no location'; + } + } + } +} + +my $json_out = JSON->new->utf8->canonical->pretty->encode($stations); +write_file( 'stations.json', $json_out ); |