summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-lata-import
blob: 295b40e0b8c4dd4913e3bfa4414c03b89170dbf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Conf;
use FS::Record qw(qsearch qsearchs dbh);
use LWP::Simple;
use HTML::TableExtract;
use Data::Dumper;

&untaint_argv;	#what it sounds like  (eww)
use vars qw(%opt);

my $user = shift or die &usage;
my $dbh = adminsuidsetup $user;

my $content = get("http://www.localcallingguide.com/lca_listlata.php");
my $te = new HTML::TableExtract();
$te->parse($content);
my $table = $te->first_table_found;
my $sql = 'insert into lata (latanum, description) values ';
my @sql;
foreach my $row ( $table->rows ) {
    my @row = @$row;
    next unless $row[0] =~ /\d+/;
    $row[1] =~ s/'//g;
    push @sql, "( ${row[0]}, '${row[1]}')";
}
$sql .= join(',',@sql);

my $sth = $dbh->prepare('delete from lata');
$sth->execute or die $sth->errstr;

$sth = $dbh->prepare($sql);
$sth->execute or die $sth->errstr;

$dbh->commit;

###
# subroutines
###

sub untaint_argv {
  foreach $_ ( $[ .. $#ARGV ) { #untaint @ARGV
    #$ARGV[$_] =~ /^([\w\-\/]*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
    # Date::Parse
    $ARGV[$_] =~ /^(.*)$/ || die "Illegal arguement \"$ARGV[$_]\"";
    $ARGV[$_]=$1;
  }
}

sub usage {
  die "Usage:\n  freeside-lata-import user \n";
}

###
# documentation
###

=head1 NAME

freeside-lata-import - Pull LATA data from and insert into LATA table

=head1 SYNOPSIS

  freeside-lata-import user

=head1 DESCRIPTION

user - name of an internal Freeside user

=head1 BUGS

=head1 SEE ALSO

L<FS::lata>

=cut