fix DBI connection, RT#39250
[freeside.git] / FS / bin / freeside-lata-import
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Std;
5 use FS::UID qw(adminsuidsetup);
6 use FS::Conf;
7 use FS::Record qw(qsearch qsearchs dbh);
8 use LWP::Simple;
9 use HTML::TableExtract;
10 use Data::Dumper;
11
12 &untaint_argv;  #what it sounds like  (eww)
13 use vars qw(%opt);
14
15 my $user = shift or die &usage;
16 my $dbh = adminsuidsetup $user;
17
18 my $content = get("http://www.localcallingguide.com/lca_listlata.php");
19 my $te = new HTML::TableExtract();
20 $te->parse($content);
21 my $table = $te->first_table_found;
22 my $sql = 'insert into lata (latanum, description) values ';
23 my @sql;
24 foreach my $row ( $table->rows ) {
25     my @row = @$row;
26     next unless $row[0] =~ /\d+/;
27     $row[1] =~ s/'//g;
28     push @sql, "( ${row[0]}, '${row[1]}')";
29 }
30 $sql .= join(',',@sql);
31
32 my $sth = $dbh->prepare('delete from lata');
33 $sth->execute or die $sth->errstr;
34
35 $sth = $dbh->prepare($sql);
36 $sth->execute or die $sth->errstr;
37
38 $dbh->commit;
39
40 ###
41 # subroutines
42 ###
43
44 sub untaint_argv {
45   foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
46     #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
47     # Date::Parse
48     $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
49     $ARGV[$_]=$1;
50   }
51 }
52
53 sub usage {
54   die "Usage:\n  freeside-lata-import user \n";
55 }
56
57 ###
58 # documentation
59 ###
60
61 =head1 NAME
62
63 freeside-lata-import - Pull LATA data from and insert into LATA table
64
65 =head1 SYNOPSIS
66
67   freeside-lata-import user
68
69 =head1 DESCRIPTION
70
71 user - name of an internal Freeside user
72
73 =head1 BUGS
74
75 =head1 SEE ALSO
76
77 L<FS::lata>
78
79 =cut
80