]> gb7djk.dxcluster.net Git - spider.git/blob - perl/gen_usdb_data.pl
add default systemd service (init) file
[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 #   http://wireless.fcc.gov/uls/data/complete/l_amat.zip
10 #
11 # The daily data bases are available as a set of seven from here:-
12 #
13 #   http://wireless.fcc.gov/uls/data/daily/l_am_sat.zip 
14 #   http://wireless.fcc.gov/uls/data/daily/l_am_sun.zip 
15 #   http://wireless.fcc.gov/uls/data/daily/l_am_mon.zip 
16 #   http://wireless.fcc.gov/uls/data/daily/l_am_tue.zip 
17 #   http://wireless.fcc.gov/uls/data/daily/l_am_wed.zip 
18 #   http://wireless.fcc.gov/uls/data/daily/l_am_thu.zip 
19 #   http://wireless.fcc.gov/uls/data/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
52 my $blksize = 1024 * 1024;
53
54 STDOUT->autoflush(1);
55
56 my $dbrawfn = localdata("usdbraw.gz");
57
58 rename "$dbrawfn.oo", "$dbrawfn.ooo";
59 rename "$dbrawfn.o", "$dbrawfn.oo";
60 rename "$dbrawfn", "$dbrawfn.o";
61 my $gzfh = gzopen($dbrawfn, "wb") or die "Cannot open $dbrawfn $!";
62
63 my $ctycount;
64   
65 foreach my $argv (@ARGV) {
66         my $zip = new Archive::Zip($argv) or die "Cannot open $argv $!\n";
67         print "Doing $argv\n";
68         handleEN($zip, $argv);
69         handleAM($zip, $argv);
70         handleHS($zip, $argv);
71 }
72
73 $gzfh->gzclose;
74
75 exit(0);
76
77 sub handleEN
78 {
79         my ($zip, $argv) = @_;
80         my $mname = "EN.dat";
81         my $ofn = localdata($mname);
82         print "  Handling EN records, unzipping";
83         if ($zip->extractMember($mname, $ofn) == AZ_OK) {
84                 my $fh = new IO::File "$ofn" or die "Cannot open $ofn $!";
85                 if ($fh) {
86                         
87                         print ", storing";
88                         
89                         my $count;
90                         my $buf;
91                         
92                         while (my $l = $fh->getline) {
93                                 $l =~ s/[\r\n]+$//;
94                                 my ($rt,$usi,$ulsfn,$ebfno,$call,$type,$lid,$name,$first,$middle,$last,$suffix,
95                                         $phone,$fax,$email,$street,$city,$state,$zip,$pobox,$attl,$sgin,$frn) = split /\|/, $l;
96
97 #                               print "ERR: $l\n" unless $call && $city && $state;
98
99                                 if ($call && $city && $state) {
100                                         my $rec = uc join '|', $call,$city,$state if $city && $state;
101                                         $buf .= "$rec\n";
102                                         if (length $buf > $blksize) {
103                                                 $gzfh->gzwrite($buf);
104                                                 undef $buf;
105                                         }
106                                         $count++;
107                                 }
108                         }
109                         $gzfh->gzwrite($buf) if length $buf;
110                         print ", $count records\n";
111                         $fh->close;
112                 }
113                 unlink $ofn;
114         } else {
115                 print "EN missing in $argv\n";
116                 return;
117         }
118 }
119
120 sub handleAM
121 {
122
123 }
124
125 sub handleHS
126 {
127
128 }