summaryrefslogtreecommitdiff
path: root/share/json2json
blob: 7cdc57716c3cb0a719e6e65a0545a4887901ee49 (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
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
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

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

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

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

my $have_duplicates = 0;
my @names           = map { $_->{name} } @{$stations};
my @ds100
  = map { $_->{ds100} } sort { $a->{ds100} cmp $b->{ds100} } @{$stations};
my @eva_ids = map { $_->{eva} } sort { $a->{eva} <=> $b->{eva} } @{$stations};

my %name   = map { $_ => 1 } @names;
my %ds100  = map { $_ => 1 } @ds100;
my %eva_id = map { $_ => 1 } @eva_ids;

my @old_names = map { $_->{name} } @{$old_stations};
my @old_ds100
  = map { $_->{ds100} } sort { $a->{ds100} cmp $b->{ds100} } @{$old_stations};
my @old_eva_ids
  = map { $_->{eva} } sort { $a->{eva} <=> $b->{eva} } @{$old_stations};

for my $i ( 1 .. $#ds100 ) {
	if ( $ds100[ $i - 1 ] eq $ds100[$i] ) {
		say "Duplicate DS100 code: $ds100[$i]";
		$have_duplicates = 1;
	}
}
for my $i ( 1 .. $#eva_ids ) {
	if ( $eva_ids[ $i - 1 ] == $eva_ids[$i] ) {
		say "Duplicate EVA ID: $eva_ids[$i]";
		$have_duplicates = 1;
	}
}

for my $old_ds100 (@old_ds100) {
	if ( $ds100{$old_ds100} ) {
		say "Old DS100 also present in new station list: $old_ds100";
	}
}

for my $old_eva (@old_eva_ids) {
	if ( $eva_id{$old_eva} ) {
		say "Old EVA also present in new station list: $old_eva";
	}
}

for my $old_name (@old_names) {
	if ( $name{$old_name} ) {
		say "Old name also present in new station list: $old_name";
	}
}

if ($have_duplicates) {
	say "Thank you for your contribution.";
	say "Please remove duplicate entries before opening a pull request.";
}

for my $station ( @{$stations} ) {
	$station->{eva} = 0 + $station->{eva};
	if (    $station->{latlong}
		and $station->{latlong}[0] == 0
		and $station->{latlong}[1] == 0 )
	{
		$station->{latlong} = undef;
	}
	elsif ( not exists $station->{latlong} ) {
		$station->{latlong} = undef;
	}
}

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

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