File Coverage

blib/lib/Metabrik/Database/Ripe.pm
Criterion Covered Total %
statement 9 76 11.8
branch 0 32 0.0
condition 0 3 0.0
subroutine 3 6 50.0
pod 1 3 33.3
total 13 120 10.8


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # database::ripe Brik
5             #
6             package Metabrik::Database::Ripe;
7 1     1   653 use strict;
  1         2  
  1         31  
8 1     1   5 use warnings;
  1         2  
  1         42  
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     1   7 use base qw(Metabrik);
  1         2  
  1         1010  
14              
15             sub brik_properties {
16             return {
17 0     0 1   revision => '$Revision$',
18             tags => [ qw(unstable netname country as) ],
19             author => 'GomoR ',
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 0     0 0   my $self = shift;
44              
45 0           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.in.gz
53             http://ftp.apnic.net/apnic/dbase/data/twnic.pn.gz
54             ftp://ftp.arin.net/pub/rr/arin.db
55             );
56              
57 0           my $datadir = $self->datadir;
58              
59 0 0         my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
60              
61 0           my @fetched = ();
62 0           for my $url (@urls) {
63 0           $self->log->verbose("update: fetching url [$url]");
64              
65 0           (my $filename = $url) =~ s/^.*\/(.*?)$/$1/;
66 0           (my $unzipped = $filename) =~ s/\.gz$//;
67              
68 0           my $output = $datadir."/$filename";
69 0           my $r = $cw->mirror($url, $filename, $datadir);
70 0 0         if (! defined($r)) {
    0          
71 0           $self->log->warning("update: can't fetch url [$url]");
72 0           next;
73             }
74             elsif (@$r == 0) { # Already up to date
75 0           next;
76             }
77              
78 0           my $files = [];
79 0 0         if ($filename =~ m{.gz$}) {
80 0           $self->log->verbose("update: uncompressing file to [$unzipped]");
81              
82 0 0         my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
83 0           $files = $fc->uncompress($output, $unzipped, $datadir);
84 0 0         if (! defined($files)) {
85 0           $self->log->warning("update: can't uncompress file [$output]");
86 0           next;
87             }
88             }
89             else {
90 0           push @fetched, $output;
91             }
92              
93 0           push @fetched, @$files;
94             }
95              
96 0           return \@fetched;
97             }
98              
99             sub next_record {
100 0     0 0   my $self = shift;
101 0           my ($input) = @_;
102              
103 0   0       $input ||= $self->datadir.'/'.$self->input;
104 0 0         $self->brik_help_run_file_not_found('next_record', $input) or return;
105              
106 0           my $fr = $self->_read;
107 0 0         if (! defined($fr)) {
108 0 0         $fr = Metabrik::File::Read->new_from_brik_init($self) or return;
109 0           $fr->encoding('ascii');
110 0           $fr->input($input);
111 0           $fr->as_array(1);
112 0 0         $fr->open
113             or return $self->log->error("next_record: file::read open failed");
114 0           $self->_read($fr);
115             }
116              
117 0           my $lines = $fr->read_until_blank_line;
118 0 0         if (@$lines == 0) {
119             # If nothing has been read and eof reached, we return undef.
120             # Otherwise, we return an empty object.
121 0 0         if ($fr->eof) {
122 0           $fr->close;
123 0           $self->_read(undef);
124 0           return;
125             }
126             else {
127 0           return {};
128             }
129             }
130              
131 0           my %record = ();
132 0           for my $line (@$lines) {
133 0 0         next if ($line =~ /^\s*#/);
134              
135 0           $line =~ s/^\s*//;
136 0           $line =~ s/\s*$//;
137              
138 0           my ($key, $val);
139 0 0         if ($line =~ /^(.*?)\s*:\s*(.*)$/) {
140 0           $key = $1;
141 0           $val = $2;
142             }
143 0 0         next unless defined($val);
144              
145 0           push @{$record{raw}}, $line;
  0            
146              
147 0           $self->log->debug("next_record: key [$key] val[$val]");
148              
149 0 0         if (! exists($record{$key})) {
150 0           $record{$key} = $val;
151             }
152             else {
153 0           $record{$key} .= "\n$val";
154             }
155              
156             # Remove DUMMY data, it is kept in {raw} anyway
157 0           delete $record{'remarks'};
158 0           delete $record{'admin-c'};
159 0           delete $record{'tech-c'};
160 0           delete $record{'changed'};
161             }
162              
163 0           return \%record;
164             }
165              
166             1;
167              
168             __END__