-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_to_mmdb.pl
160 lines (152 loc) · 4.87 KB
/
csv_to_mmdb.pl
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#/usr/bin/env perl
use MaxMind::DB::Writer::Tree;
use Net::Works::Network;
use open qw(:std :utf8);
if(@ARGV != 2){
print "Usage: perl csv_file output_mmdb_file.\n";
}
open (FILE, @ARGV[0]) || die "Can not open file: @ARGV[0]\n";
@line=<FILE>;
my %types = (
city => 'map',
country => 'map',
county => 'map',
registered_country => 'map',
subdivisions => 'map',
location => 'map',
continent => 'map',
geoname_id => 'uint32',
id => 'uint32',
names => 'map',
isp => 'map',
en => 'utf8_string',
zh_CN => 'utf8_string',
de => 'utf8_string',
fr => 'utf8_string',
ru => 'utf8_string',
spa => 'utf8_string',
jp => 'utf8_string',
pt => 'utf8_string',
code => 'utf8_string',
iso_code => 'utf8_string',
org => 'utf8_string',
latitude => 'double',
longitude => 'double',
time_zone => 'utf8_string',
states => [ 'array', 'utf8_string' ],
);
my $tree = MaxMind::DB::Writer::Tree->new(
ip_version => 4,
record_size => 32,
database_type => 'GeoIP',
languages => ['en'],
description => { en => 'GeoIP database' },
map_key_type_callback => sub { $types{ $_[0] } },
);
my $i = 0;
foreach (@line){
$i++;
if($i != 1){
my $linestr = $_;
my @arr = ();
my $position = 0;
my $end = -1;
my $start = 0;
if(substr($linestr, 0, 1) eq '"'){
$linestr = substr($linestr, 1, length($linestr)-1);
$end = index($linestr, '"');
$start = 1;
}
else{
$end = index($linestr, ',');
}
while($end != -1){
if($end == 0){
$str = "";
}
else{
$str = substr($linestr, 0, $end);
}
push(@arr, $str);
$end++;
$linestr = substr($linestr, $end+$start, length($linestr)-$end);
if(substr($linestr, 0, 1) eq '"'){
$linestr = substr($linestr, 1, length($linestr)-1);
$end = index($linestr, '"');
$start = 1;
}
else{
$end = index($linestr, ',');
$start = 0;
}
}
$linestr =~ s/\"//g;
push(@arr, $linestr);
$scalar = @arr;
print "$arr[0]\n";
if($arr[0] != "" && $scalar == 22){
my $network
= Net::Works::Network->new_from_string( string => $arr[0] );
$arr[21] =~ s/\n//;
$tree->insert_network(
$network,
{
continent => {
code => $arr[1],
names => {
en => $arr[2],
zh_CN => $arr[3],
},
},
country => {
iso_code => $arr[4],
names => {
en => $arr[5],
zh_CN => $arr[6],
},
},
subdivisions => {
iso_code => $arr[7],
names => {
en => $arr[8],
zh_CN => $arr[9],
},
},
city => {
id => $arr[10]+0,
names => {
en => $arr[11],
zh_CN => $arr[12],
},
},
county => {
id => $arr[13]+0,
names => {
en => $arr[14],
zh_CN => $arr[15],
},
},
isp => {
id => $arr[16]+0,
names => {
zh_CN => $arr[17],
},
},
location => {
time_zone => $arr[18],
org => $arr[19],
latitude => $arr[20]+0.0,
longitude => $arr[21]+0.0,
},
},
);
}
else{
print $line;
}
}
}
close FILE;
open my $fh, '>:raw', "@ARGV[1]";
$tree->write_tree($fh);
close $fh;