fix gen_usdb_data.pl
[spider.git] / perl / gen_usdb_data.pl
1 #!/usr/bin/env perl
2 #
3 # Something to create my subset of the US call book data,
4 # in my flat file form, either from the main data base or
5 # else the daily updates. 
6 #
7 # You can get the main database from: 
8 #
9 #   https://data.fcc.gov/download/pub/uls/complete/l_amat.zip
10 #
11 # The daily data bases are available as a set of seven from here:-
12 #
13 #  https://data.fcc.gov/download/pub/uls/daily/l_am_sat.zip 
14 #  https://data.fcc.gov/download/pub/uls/daily/l_am_sun.zip 
15 #  https://data.fcc.gov/download/pub/uls/daily/l_am_mon.zip 
16 #  https://data.fcc.gov/download/pub/uls/daily/l_am_tue.zip 
17 #  https://data.fcc.gov/download/pub/uls/daily/l_am_wed.zip 
18 #  https://data.fcc.gov/download/pub/uls/daily/l_am_thu.zip 
19 #  https://data.fcc.gov/download/pub/uls/daily/l_am_fri.zip
20
21 # this program expects one or more zip files containing the call book
22 # data as arguments.
23 #
24 # Copyright (c) 2002 Dirk Koopman G1TLH
25 #
26 #
27 #
28
29 use strict;
30
31 # make sure that modules are searched in the order local then perl
32 BEGIN {
33         # root of directory tree for this system
34         use vars qw($root);
35         
36         $root = "/spider"; 
37         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
38
39         mkdir "$root/local_data", 02777 unless -d "$root/local_data";
40
41         unshift @INC, "$root/perl";     # this IS the right way round!
42         unshift @INC, "$root/local";
43 }
44
45 use SysVar;
46
47 use Archive::Zip qw(:ERROR_CODES);
48 use Archive::Zip::MemberRead;
49 use IO::File;
50 use Compress::Zlib;
51 use DXUtil;
52
53 my $blksize = 1024 * 1024;
54
55 STDOUT->autoflush(1);
56
57 my $dbrawfn = localdata("usdbraw.gz");
58
59 rename "$dbrawfn.oo", "$dbrawfn.ooo";
60 rename "$dbrawfn.o", "$dbrawfn.oo";
61 rename "$dbrawfn", "$dbrawfn.o";
62 my $gzfh = gzopen($dbrawfn, "wb") or die "Cannot open $dbrawfn $!";
63
64 my $ctycount;
65   
66 foreach my $argv (@ARGV) {
67         my $zip = new Archive::Zip($argv) or die "Cannot open $argv $!\n";
68         print "Doing $argv\n";
69         handleEN($zip, $argv);
70         handleAM($zip, $argv);
71         handleHS($zip, $argv);
72 }
73
74 $gzfh->gzclose;
75
76 exit(0);
77
78 sub handleEN
79 {
80         my ($zip, $argv) = @_;
81         my $mname = "EN.dat";
82         my $ofn = localdata($mname);
83         print "  Handling EN records, unzipping";
84         if ($zip->extractMember($mname, $ofn) == AZ_OK) {
85                 my $fh = new IO::File "$ofn" or die "Cannot open $ofn $!";
86                 if ($fh) {
87                         
88                         print ", storing";
89                         
90                         my $count;
91                         my $buf;
92                         
93                         while (my $l = $fh->getline) {
94                                 $l =~ s/[\r\n]+$//;
95                                 my ($rt,$usi,$ulsfn,$ebfno,$call,$type,$lid,$name,$first,$middle,$last,$suffix,
96                                         $phone,$fax,$email,$street,$city,$state,$zip,$pobox,$attl,$sgin,$frn) = split /\|/, $l;
97
98 #                               print "ERR: $l\n" unless $call && $city && $state;
99
100                                 if ($call && $city && $state) {
101                                         my $rec = uc join '|', $call,$city,$state if $city && $state;
102                                         $buf .= "$rec\n";
103                                         if (length $buf > $blksize) {
104                                                 $gzfh->gzwrite($buf);
105                                                 undef $buf;
106                                         }
107                                         $count++;
108                                 }
109                         }
110                         $gzfh->gzwrite($buf) if length $buf;
111                         print ", $count records\n";
112                         $fh->close;
113                 }
114                 unlink $ofn;
115         } else {
116                 print "EN missing in $argv\n";
117                 return;
118         }
119 }
120
121 sub handleAM
122 {
123
124 }
125
126 sub handleHS
127 {
128
129 }