| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use NOLookup::Brreg::DataLookup; | ||
| 6 | use Encode; | ||
| 7 | |||
| 8 | use vars qw / | ||
| 9 | $opt_o $opt_n $opt_f $opt_t $opt_p $opt_i $opt_c | ||
| 10 | $opt_u $opt_d $opt_X $opt_v $opt_h $opt_D | ||
| 11 | /; | ||
| 12 | use Getopt::Std; | ||
| 13 | |||
| 14 | use Pod::Usage; | ||
| 15 | |||
| 16 | use Data::Dumper; | ||
| 17 | $Data::Dumper::Indent=1; | ||
| 18 | |||
| 19 | # Use this to activate LWP debug, | ||
| 20 | # ref. https://metacpan.org/pod/LWP::ConsoleLogger::Everywhere | ||
| 21 | #use LWP::ConsoleLogger::Everywhere; | ||
| 22 | |||
| 23 | &getopts('hvud:o:n:f:p:c:t:i:D:X:'); | ||
| 24 | |||
| 25 | if ($opt_h) { | ||
| 26 | pod2usage(); | ||
| 27 | } | ||
| 28 | |||
| 29 | unless ($opt_o || $opt_n || $opt_f || $opt_t || $opt_D || $opt_X) { | ||
| 30 | pod2usage("An organization number, name, from/to dates or update date or -id must be specified!\n"); | ||
| 31 | } | ||
| 32 | |||
| 33 | my $h1 = "OrgNumber\tOrgForm\tregDate \torgName"; | ||
| 34 | my $h2 = "OrgNumber\tupdateDateTime\t\t\tupdateId"; | ||
| 35 | |||
| 36 | my $bo = NOLookup::Brreg::DataLookup->new; | ||
| 37 | |||
| 38 | $bo->{debug} = 1 if ($opt_d); | ||
| 39 | |||
| 40 | if ($opt_o) { | ||
| 41 | $bo->lookup_orgno($opt_o, $opt_u); | ||
| 42 | |||
| 43 | } elsif ($opt_n) { | ||
| 44 | my $nm = decode('UTF-8', $opt_n); | ||
| 45 | $bo->lookup_orgname($nm, $opt_p, $opt_i, $opt_u, $opt_c); | ||
| 46 | |||
| 47 | } elsif ($opt_f || $opt_t) { | ||
| 48 | $bo->lookup_reg_dates($opt_f, $opt_t, $opt_p, $opt_i, $opt_u); | ||
| 49 | |||
| 50 | } elsif ($opt_D || $opt_X) { | ||
| 51 | $bo->lookup_update_dates($opt_D, $opt_X, $opt_p, $opt_i, $opt_u); | ||
| 52 | } | ||
| 53 | |||
| 54 | if ($bo->error) { | ||
| 55 | print STDERR "Error: ", $bo->status, "\n"; | ||
| 56 | exit; | ||
| 57 | } | ||
| 58 | |||
| 59 | if ($bo->warning) { | ||
| 60 | print STDERR "Warning: ", $bo->status, "\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $etype = "(Enhet)"; | ||
| 64 | |||
| 65 | if ($opt_u) { | ||
| 66 | $etype = "(UnderEnhet)"; | ||
| 67 | } | ||
| 68 | |||
| 69 | #print STDERR "bo: ", Dumper $bo; | ||
| 70 | |||
| 71 | if ($bo->size <1) { | ||
| 72 | print "No match on $etype search\n"; | ||
| 73 | |||
| 74 | } elsif ($bo->size == 1) { | ||
| 75 | print "Found ", $bo->size, " matching $etype entries:\n"; | ||
| 76 | print "$h1\n"; | ||
| 77 | foreach my $e (@{$bo->data}) { | ||
| 78 | print $e->organisasjonsnummer, "\t", | ||
| 79 | $e->organisasjonsform->{kode}, "\t", | ||
| 80 | $e->registreringsdatoEnhetsregisteret || ($e->slettedato . " (slettet)"), "\t", | ||
| 81 | encode('UTF-8', $e->navn), "\n"; | ||
| 82 | } | ||
| 83 | } elsif ($bo->size > 1 && ($opt_n || $opt_f || $opt_t)) { | ||
| 84 | print "Found ", $bo->size, " matching $etype entries:\n"; | ||
| 85 | print "$h1\n"; | ||
| 86 | foreach my $e (@{$bo->data}) { | ||
| 87 | print $e->organisasjonsnummer, "\t", | ||
| 88 | $e->organisasjonsform->{kode}, "\t", | ||
| 89 | $e->registreringsdatoEnhetsregisteret, "\t", | ||
| 90 | encode('UTF-8', $e->navn), "\n"; | ||
| 91 | } | ||
| 92 | |||
| 93 | } elsif ($bo->size > 1 && ($opt_D || $opt_X)) { | ||
| 94 | print "Found ", $bo->size, " matching updated $etype entries:\n"; | ||
| 95 | print "$h2\n"; | ||
| 96 | foreach my $e (@{$bo->data}) { | ||
| 97 | print $e->organisasjonsnummer, "\t", | ||
| 98 | $e->dato, "\t", | ||
| 99 | $e->oppdateringsid, "\n"; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | if ($opt_v) { | ||
| 104 | print "\n--\nJSON data structure: ", | ||
| 105 | Dumper($bo->raw_json_decoded), "\n--\n"; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | =pod | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | no_brreg.pl | ||
| 114 | |||
| 115 | =head1 DESCRIPTION | ||
| 116 | |||
| 117 | Uses NOLookup::Brreg::DataLookup to perform lookup on an orgnumber | ||
| 118 | or an orgname and fetch and print the matching information. | ||
| 119 | |||
| 120 | The data found are stored as NOLookup::Brreg::Entry data objects. | ||
| 121 | |||
| 122 | =head1 USAGE | ||
| 123 | |||
| 124 | no_brreg.pl -o 985821585 | ||
| 125 | |||
| 126 | no_brreg.pl -n norid | ||
| 127 | Note that only orgs starting with -n are listed due | ||
| 128 | to a limitation in the Brreg API service. | ||
| 129 | |||
| 130 | no_brreg.pl -f 2017-04-29 -t 2017-04-30 -i 2 -p 1 | ||
| 131 | |||
| 132 | Up to a maximum of 1000 matches (10 json pages) are listed. | ||
| 133 | |||
| 134 | Examples: | ||
| 135 | |||
| 136 | perl no_brreg.pl -o 985821585 | ||
| 137 | |||
| 138 | Found 1 matching entries: | ||
| 139 | OrgNumber OrgForm regDate orgName | ||
| 140 | 985821585 AS 2003-06-30 UNINETT NORID AS | ||
| 141 | |||
| 142 | perl no_brreg.pl -n uninett | ||
| 143 | |||
| 144 | Found 4 matching (Enhet) entries: | ||
| 145 | OrgNumber OrgForm regDate orgName | ||
| 146 | 819549532 FLI 2017-09-12 UNINETT PENSJONISTFORENING | ||
| 147 | 968100211 AS 1995-02-20 UNINETT AS | ||
| 148 | 985821585 AS 2003-06-30 UNINETT NORID AS | ||
| 149 | 814864332 AS 2015-01-26 UNINETT SIGMA2 AS | ||
| 150 | |||
| 151 | If name consists of several words, hits on each word is also | ||
| 152 | found. | ||
| 153 | |||
| 154 | perl no_brreg.pl -n uninett -u | ||
| 155 | |||
| 156 | Found 3 matching (UnderEnhet) entries: | ||
| 157 | OrgNumber OrgForm regDate orgName | ||
| 158 | 973897187 BEDR 1995-02-23 UNINETT AS | ||
| 159 | 986671773 BEDR 2004-03-11 UNINETT NORID AS | ||
| 160 | 987631473 BEDR 2004-12-22 UNINETT SIGMA2 AS | ||
| 161 | |||
| 162 | perl no_brreg.pl -n "uninett as" -p3 | ||
| 163 | |||
| 164 | Found 300 matching (Enhet) entries: | ||
| 165 | OrgNumber OrgForm regDate orgName | ||
| 166 | 968100211 AS 1995-02-20 UNINETT AS | ||
| 167 | 985821585 AS 2003-06-30 UNINETT NORID AS | ||
| 168 | 814864332 AS 2015-01-26 UNINETT SIGMA2 AS | ||
| 169 | 819549532 FLI 2017-09-12 UNINETT PENSJONISTFORENING | ||
| 170 | 918312080 AS 2017-01-24 AS TRANSPORT AS | ||
| 171 | 918591052 AS 2017-02-21 AS HOLDING AS | ||
| 172 | : | ||
| 173 | : | ||
| 174 | Lists max. 3 pages, each of 100 hits. Default max. pages are 10, e.g. 1000 hits. | ||
| 175 | |||
| 176 | perl no_brreg.pl -f 2019-02-23 -t 2019-02-24 | ||
| 177 | |||
| 178 | Found 10 matching (Enhet) entries: | ||
| 179 | OrgNumber OrgForm regDate orgName | ||
| 180 | 922173222 AS 2019-02-23 AERO DRAGBAG AS | ||
| 181 | 922141266 NUF 2019-02-23 COMAEN B.V. | ||
| 182 | 922082510 ENK 2019-02-23 SHIRAZI ISOKONTROLL MEHDI SADEGHINEJADIAN SHIRAZI | ||
| 183 | 922084823 ENK 2019-02-23 MOFRAD HMS MOHAMMAD NEKOOMANESHMOFRAD | ||
| 184 | : | ||
| 185 | |||
| 186 | |||
| 187 | perl ./no_brreg.pl -d 2019-02-25 -p1 | ||
| 188 | |||
| 189 | Found 100 matching updated (Enhet) entries: | ||
| 190 | OrgNumber updateDateTime updateId | ||
| 191 | 816860482 2019-02-25T05:01:06.526Z 2686439 | ||
| 192 | 816860482 2019-02-25T05:01:47.969Z 2686443 | ||
| 193 | 958292104 2019-02-25T05:01:47.969Z 2686445 | ||
| 194 | : | ||
| 195 | : | ||
| 196 | |||
| 197 | perl no_brreg.pl -d 2019-02-25 -p1 -x 2687774 | ||
| 198 | |||
| 199 | Found 2 matching updated (Enhet) entries: | ||
| 200 | OrgNumber updateDateTime updateId | ||
| 201 | 919640170 2019-02-25T18:25:34.449Z 2687776 | ||
| 202 | 919640170 2019-02-25T18:26:16.002Z 2687778 | ||
| 203 | |||
| 204 | List enheter on date and with optional updateId | ||
| 205 | |||
| 206 | Arguments: | ||
| 207 | |||
| 208 | -o: orgnumber (9 digits) | ||
| 209 | |||
| 210 | -n: orgname (complete name or start of name, minimum 2 chars) | ||
| 211 | |||
| 212 | To list entries registered in period from/to date: | ||
| 213 | -f: from registration date (2017-04-10) | ||
| 214 | -t: to registration date (2017-04-11) | ||
| 215 | |||
| 216 | To list entries updated/changed since update date/id: | ||
| 217 | -D: from update date (2017-04-11) | ||
| 218 | -X: from update id (1234) | ||
| 219 | |||
| 220 | When -n, -f, -t, -d or -i is specifed, also: | ||
| 221 | -u: search in underenheter, else enheter. | ||
| 222 | -p: max number of pages (1..x, default 10). 100 hits per page. | ||
| 223 | -c: page count, number of entries per page (1..x, default 100). | ||
| 224 | -i: page index (0..x), which page to start on | ||
| 225 | |||
| 226 | Other: | ||
| 227 | -d: debug: 1 or higher for increased debug level | ||
| 228 | -h: help | ||
| 229 | -v: verbose dump of the complete JSON data structure | ||
| 230 | |||
| 231 | =cut | ||
| 232 | |||
| 233 | 1; | ||
| 234 | |||
| 235 |