forked from maxmind/MaxMind-DB-Writer-perl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.p
29 lines (25 loc) · 956 Bytes
/
main.p
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
use MaxMind::DB::Writer::Tree;
use JSON::Parse 'json_file_to_perl';
use JSON::Parse 'parse_json';
use warnings;
my ($config_path, $data_path, $output_path) = @ARGV;
my $configuration = json_file_to_perl($config_path);
my $tree = MaxMind::DB::Writer::Tree->new(
ip_version => $configuration->{ip_version},
record_size => $configuration->{record_size},
database_type => $configuration->{database_type},
languages => $configuration->{languages},
description => $configuration->{description},
map_key_type_callback => sub { $configuration->{types}->{ $_[0] } },
);
if (open(my $fh, '<:encoding(UTF-8)', $data_path)) {
while (my $row = <$fh>) {
chomp $row;
my $record = parse_json($row);
$tree->insert_network($record->{network}, $record->{data});
}
} else {
warn "Could not open file '$data_path' $!";
}
open my $fh, '>:raw', $output_path;
$tree->write_tree($fh);