summaryrefslogtreecommitdiff
path: root/share/xml2meta
blob: 79b4e23f83bea3b4ca33644fb6233e769028c613 (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
44
45
46
47
48
49
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use File::Slurp qw(read_file write_file);
use JSON;
use List::Util qw(any);
use XML::LibXML;

my $json_str = read_file('stations.json');
my %known_eva;
for my $station ( @{ JSON->new->utf8->decode($json_str) } ) {
	$known_eva{ $station->{eva} } = 1;
}

# Norddeich and Norddeich Mole are illegaly coupled in the backend (they are
# different stations with different departure times). Ignore their EVA IDs.
delete $known_eva{8007768};
delete $known_eva{8004449};

# same goes for Essen-Dellwig / Essen-Dellwig Ost
delete $known_eva{8001903};
delete $known_eva{8001904};

my $xml_str = read_file('stations.xml');
my $tree    = XML::LibXML->load_xml( string => $xml_str );

my %meta;

for my $station ( $tree->findnodes('//station') ) {
	my $eva  = $station->getAttribute('eva');
	my $meta = $station->getAttribute('meta');

	if ( $known_eva{$eva} and $meta ) {
		for my $ref ( split( qr{[|]}, $meta ) ) {
			if ( $known_eva{$ref} ) {
				push( @{ $meta{$eva} }, 0 + $ref );
			}
			else {
				say "Note: Ignoring $eva -> $ref (unknown)";
			}
		}
	}
}

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