Lookup/Countrycode.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
« Lookup/Alexa.pm ↑ Summary Lookup/Ethernet.pm »
1 #
2 # $Id$
3 #
4 # lookup::countrycode Brik
5 #
6 package Metabrik::Lookup::Countrycode;
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 1 ▶use base qw(Metabrik::Client::Www);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable iana cc tld) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(file) ],
21 _data => [ qw(INTERNAL) ],
22 _data_by_tld => [ qw(INTERNAL) ],
23 },
24 commands => {
25 update => [ ],
26 load => [ qw(input|OPTIONAL) ],
27 load_by_tld => [ qw(input|OPTIONAL) ],
28 list_types => [ ],
29 list_tlds => [ ],
30 from_tld => [ qw(tld) ],
31 },
32 require_modules => {
33 'Metabrik::File::Csv' => [ ],
34 'Metabrik::System::File' => [ ],
35 },
36 };
37 }
38  
39 sub brik_use_properties {
40 my $self = shift;
41  
42 my $datadir = $self->datadir;
43  
44 return {
45 attributes_default => {
46 input => $datadir.'/country-codes.csv',
47 },
48 };
49 }
50  
51 sub _load {
52 my $self = shift;
53  
54 my $data = $self->_data;
55 if (! defined($data)) {
56 $data = $self->load or return;
57 $self->_data($data);
58 }
59  
60 return $data;
61 }
62  
63 sub _load_by_tld {
64 my $self = shift;
65  
66 my $data = $self->_data_by_tld;
67 if (! defined($data)) {
68 $data = $self->load_by_tld or return;
69 $self->_data_by_tld($data);
70 }
71  
72 return $data;
73 }
74  
75 sub list_types {
76 my $self = shift;
77  
78 my $data = $self->_load or return;
79  
80 my %list = ();
81 for my $this (@$data) {
82 $list{$this->{type}}++;
83 }
84  
85 my @types = sort { $a cmp $b } keys %list;
86  
87 return \@types;
88 }
89  
90 sub list_tlds {
91 my $self = shift;
92  
93 my $data = $self->_load or return;
94  
95 my %list = ();
96 for my $this (@$data) {
97 $list{$this->{tld}}++;
98 }
99  
100 my @tlds = sort { $a cmp $b } keys %list;
101  
102 return \@tlds;
103 }
104  
105 #
106 # Port numbers:
107 # http://www.iana.org/protocols
108 # http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
109 #
110 sub update {
111 my $self = shift;
112  
113 my $input = $self->input;
114  
115 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
116 $sf->remove($input);
117  
118 my $uri = 'http://www.iana.org/domains/root/db';
119  
120 my $get = $self->get($uri) or return;
121 my $html = $get->{content};
122  
123 my @cc = ();
124 while ($html =~ m{<span class="domain tld">(.*?)</tr>}gcs) {
125 my $this = $1;
126  
127 $this =~ s/\n//g;
128  
129 $self->log->debug("update: this[$this]");
130  
131 # <tr>
132 # <td>
133 # <span class="domain tld"><a href="/domains/root/db/aaa.html">.aaa</a></span></td>
134 # <td>generic</td>
135 # <td>American Automobile Association, Inc.</td>
136 # </tr>
137  
138 my ($tld, $type, $sponsor) =
139 ($this =~ m{^.*?<a href.*?>(.*?)<.*?<td>(.*?)<.*?<td>(.*?)<.*$});
140  
141 # Remove leading . and put to lowercase
142 $tld =~ s{^\.}{};
143 $tld = lc($tld);
144  
145 push @cc, {
146 tld => $tld,
147 type => "\"$type\"",
148 sponsor => "\"$sponsor\"",
149 };
150 }
151  
152 my $fc = Metabrik::File::Csv->new_from_brik_init($self) or return;
153 $fc->append(0);
154 $fc->overwrite(1);
155 $fc->encoding('utf8');
156 $fc->write_header(1);
157 $fc->separator(',');
158 $fc->header([qw(tld type sponsor)]);
159  
160 $fc->write(\@cc, $input) or return;
161  
162 return $input;
163 }
164  
165 sub load {
166 my $self = shift;
167 my ($input) = @_;
168  
169 $input ||= $self->input;
170 $self->brik_help_run_undef_arg('load', $input) or return;
171 $self->brik_help_run_file_not_found('load', $input) or return;
172  
173 my $fc = Metabrik::File::Csv->new_from_brik_init($self) or return;
174 $fc->encoding('utf8');
175 $fc->first_line_is_header(1);
176 $fc->separator(',');
177  
178 my $csv = $fc->read($input) or return;
179  
180 return $csv;
181 }
182  
183 sub load_by_tld {
184 my $self = shift;
185 my ($input) = @_;
186  
187 $input ||= $self->input;
188 $self->brik_help_run_undef_arg('load_by_tld', $input) or return;
189 $self->brik_help_run_file_not_found('load_by_tld', $input) or return;
190  
191 my $fc = Metabrik::File::Csv->new_from_brik_init($self) or return;
192 $fc->encoding('utf8');
193 $fc->first_line_is_header(1);
194 $fc->separator(',');
195  
196 my $csv = $fc->read($input) or return;
197  
198 my %by_tld = ();
199 for (@$csv) {
200 $by_tld{$_->{tld}} = $_;
201 }
202  
203 return \%by_tld;
204 }
205  
206 sub from_tld {
207 my $self = shift;
208 my ($tld) = @_;
209  
210 $self->brik_help_run_undef_arg('from_tld', $tld) or return;
211  
212 my $data = $self->_load_by_tld or return;
213  
214 if (exists($data->{lc($tld)})) {
215 return $data->{lc($tld)};
216 }
217  
218 return 0;
219 }
220  
221 1;
222  
223 __END__
« Lookup/Alexa.pm ↑ Summary Lookup/Ethernet.pm »