6 use File::Temp 'tempdir';
8 use FS::UID qw(adminsuidsetup datasrc dbh);
11 use FS::Record qw(qsearch qsearchs);
18 use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
21 my ($user, $login, $password) = @ARGV;
22 $user and $login and $password or die &usage;
24 my $dbh = adminsuidsetup $user;
25 $FS::UID::AutoCommit = 0;
27 # index already-downloaded batches
28 my @previous = qsearch({
29 'table' => 'cdr_batch',
30 'hashref' => { 'cdrbatch' => {op=>'like', value=>'voip_innovations%'} },
31 'order_by' => 'ORDER BY cdrbatch DESC',
33 my %exists = map {$_->cdrbatch => 1} @previous;
35 my $tempdir = tempdir( CLEANUP => !$opt_v );
37 my $format = 'voip_innovations';
38 my $hostname = 'cdrs.globalpopsvoip.com';
40 my $ftp = Net::FTP->new($hostname, Debug => $opt_d)
41 or die "Can't connect to $hostname: $@\n";
43 $ftp->login($login, $password)
44 or die "Login failed: ".$ftp->message."\n";
50 warn "Retrieving directory listing\n" if $opt_v;
53 my @dirs = $ftp->ls();
54 warn scalar(@dirs)." directories found.\n" if $opt_v;
57 my $most_recent = $previous[0];
59 if ($most_recent->cdrbatch =~ /^voip_innovations-(\d+)/) {
61 warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
62 @dirs = grep {$_ > $date} @dirs;
64 } # else download them all
68 # normalize date format
69 $opt_s = str2time('%Y%m%d', time2str($opt_s)) if $opt_s =~ /\D/;
70 warn "limiting to dates > $opt_s\n" if $opt_v;
71 @dirs = grep {$_ > $opt_s} @dirs;
75 $opt_e = str2time('%Y%m%d', time2str($opt_e)) if $opt_e =~ /\D/;
76 warn "limiting to dates < $opt_e\n" if $opt_v;
77 @dirs = grep {$_ < $opt_e} @dirs;
79 warn scalar(@dirs) ." to be downloaded\n" if $opt_v;
80 foreach my $dir (@dirs) {
82 foreach my $file ($ftp->ls) {
83 warn "downloading $file\n" if $opt_v;
84 $ftp->get($file, "$tempdir/$file");
85 warn "processing $file\n" if $opt_v;
87 # "voip_innovations-20130628/20130628_20130628.CDR"
88 my $batchname = "$format-$dir/$file";
89 if ($exists{$batchname}) {
90 warn "already imported $file\n";
93 my $import_options = {
94 'file' => "$tempdir/$file",
96 'batch_namevalue' => $batchname,
99 $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
101 my $error = FS::cdr::batch_import($import_options);
104 die "error processing $dir/$file: $error\n";
117 "Usage: \n cdr-voip_innovations.import user login password\n [ options ]
120 -d: enable FTP debugging (very noisy)
121 -c num: apply a cdrtypenum to the imported CDRs
124 -a: automatically choose start date from most recently downloaded batch