Database/Ripe.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Database/Nvd.pm ↑ Summary Database/Rir.pm »
1 #
2 # $Id$
3 #
4 # database::ripe Brik
5 #
6 package Metabrik::Database::Ripe;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # API RIPE search : http://rest.db.ripe.net/search?query-string=193.6.223.152/24
11 # https://github.com/RIPE-NCC/whois/wiki/WHOIS-REST-API
12  
13 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub brik_properties {
16 return {
17 revision => '$Revision$',
18 tags => [ qw(unstable netname country as) ],
19 author => 'GomoR <GomoR[at]metabrik.org>',
20 license => 'http://opensource.org/licenses/BSD-3-Clause',
21 attributes => {
22 datadir => [ qw(datadir) ],
23 input => [ qw(ripe.db) ],
24 _read => [ qw(INTERNAL) ],
25 },
26 attributes_default => {
27 input => 'ripe.db',
28 },
29 commands => {
30 update => [ ],
31 next_record => [ qw(file.ripe|OPTIONAL) ],
32 },
33 require_modules => {
34 'Metabrik::Client::Www' => [ ],
35 'Metabrik::File::Compress' => [ ],
36 'Metabrik::File::Read' => [ ],
37 'Metabrik::File::Text' => [ ],
38 },
39 };
40 }
41  
42 sub update {
43 my $self = shift;
44  
45 my @urls = qw(
46 ftp://ftp.apnic.net/apnic/whois/apnic.db.inetnum.gz
47 ftp://ftp.apnic.net/apnic/whois/apnic.db.inet6num.gz
48 ftp://ftp.ripe.net/ripe/dbase/ripe.db.gz
49 ftp://ftp.afrinic.net/dbase/afrinic.db.gz
50 http://ftp.apnic.net/apnic/dbase/data/jpnic.db.gz
51 http://ftp.apnic.net/apnic/dbase/data/krnic.db.gz
52 http://ftp.apnic.net/apnic/dbase/data/twnic.db.gz
53 ftp://ftp.arin.net/pub/rr/arin.db.gz
54 );
55  
56 my $datadir = $self->datadir;
57  
58 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
59  
60 my @fetched = ();
61 for my $url (@urls) {
62 $self->log->verbose("update: fetching url [$url]");
63  
64 (my $filename = $url) =~ s/^.*\/(.*?)$/$1/;
65 (my $unzipped = $filename) =~ s/\.gz$//;
66  
67 my $output = $datadir."/$filename";
68 my $r = $cw->mirror($url, $filename, $datadir);
69 if (! defined($r)) {
70 $self->log->warning("update: can't fetch url [$url]");
71 next;
72 }
73 elsif (@$r == 0) { # Already up to date
74 next;
75 }
76  
77 my $files = [];
78 if ($filename =~ m{.gz$}) {
79 $self->log->verbose("update: uncompressing file to [$unzipped]");
80  
81 my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
82 $files = $fc->uncompress($output, $unzipped, $datadir);
83 if (! defined($files)) {
84 $self->log->warning("update: can't uncompress file [$output]");
85 next;
86 }
87 }
88 else {
89 push @fetched, $output;
90 }
91  
92 push @fetched, @$files;
93 }
94  
95 return \@fetched;
96 }
97  
98 sub next_record {
99 my $self = shift;
100 my ($input) = @_;
101  
102 $input ||= $self->datadir.'/'.$self->input;
103 $self->brik_help_run_file_not_found('next_record', $input) or return;
104  
105 my $fr = $self->_read;
106 if (! defined($fr)) {
107 $fr = Metabrik::File::Read->new_from_brik_init($self) or return;
108 $fr->encoding('ascii');
109 $fr->input($input);
110 $fr->as_array(1);
111 $fr->open
112 or return $self->log->error("next_record: file::read open failed");
113 $self->_read($fr);
114 }
115  
116 my $lines = $fr->read_until_blank_line;
117 if (@$lines == 0) {
118 # If nothing has been read and eof reached, we return undef.
119 # Otherwise, we return an empty object.
120 if ($fr->eof) {
121 $fr->close;
122 $self->_read(undef);
123 return;
124 }
125 else {
126 return {};
127 }
128 }
129  
130 my %record = ();
131 for my $line (@$lines) {
132 next if ($line =~ /^\s*#/);
133  
134 $line =~ s/^\s*//;
135 $line =~ s/\s*$//;
136  
137 my ($key, $val);
138 if ($line =~ /^(.*?)\s*:\s*(.*)$/) {
139 $key = $1;
140 $val = $2;
141 }
142 next unless defined($val);
143  
144 push @{$record{raw}}, $line;
145  
146 $self->log->debug("next_record: key [$key] val[$val]");
147  
148 if (! exists($record{$key})) {
149 $record{$key} = $val;
150 }
151 else {
152 $record{$key} .= "\n$val";
153 }
154  
155 # Remove DUMMY data, it is kept in {raw} anyway
156 delete $record{'remarks'};
157 delete $record{'admin-c'};
158 delete $record{'tech-c'};
159 delete $record{'changed'};
160 }
161  
162 return \%record;
163 }
164  
165 1;
166  
167 __END__
« Database/Nvd.pm ↑ Summary Database/Rir.pm »