summaryrefslogtreecommitdiff
path: root/bin/cdr-voip_innovations.import
blob: 31a3e7cd6a43f5a1c6586956b8b2ecd10ef9897a (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl

use strict;
use Getopt::Std;
use Date::Format;
use File::Temp 'tempdir';
use Net::FTP;
use FS::UID qw(adminsuidsetup datasrc dbh);
use FS::cdr;
use FS::cdr_batch;
use FS::Record qw(qsearch qsearchs);
use Date::Format 'time2str';
use Date::Parse 'str2time';


###
# parse command line
###

use vars qw( $opt_d $opt_v $opt_c $opt_s $opt_e $opt_a );
getopts('dvc:s:e:a');

my ($user, $login, $password) = @ARGV;
$user and $login and $password or die &usage;

my $dbh = adminsuidsetup $user;
$FS::UID::AutoCommit = 0;

# index already-downloaded batches
my @previous = qsearch({
    'table'     => 'cdr_batch',
    'hashref'   => { 'cdrbatch' => {op=>'like', value=>'voip_innovations%'} },
    'order_by'  => 'ORDER BY cdrbatch DESC',
});
my %exists = map {$_->cdrbatch => 1} @previous;

my $tempdir = tempdir( CLEANUP => !$opt_v );

my $format = 'voip_innovations';
my $hostname = 'cdrs.globalpopsvoip.com';

my $ftp = Net::FTP->new($hostname, Debug => $opt_d)
  or die "Can't connect to $hostname: $@\n";

$ftp->login($login, $password)
  or die "Login failed: ".$ftp->message."\n";

###
# get the file list
###

warn "Retrieving directory listing\n" if $opt_v;

$ftp->cwd('/');
my @dirs = $ftp->ls();
warn scalar(@dirs)." directories found.\n" if $opt_v;
# apply date range
if ( $opt_a ) {
  my $most_recent = $previous[0];
  if ($most_recent) {
    if ($most_recent->cdrbatch =~ /^voip_innovations-(\d+)/) {
      my $date = $1;
      warn "limiting to dates > $date (from most recent batch)\n" if $opt_v;
      @dirs = grep {$_ > $date} @dirs;
    }
  } # else download them all
}
if ( $opt_s ) {
  # start date
  # normalize date format
  $opt_s = time2str('%Y%m%d', str2time($opt_s)) if $opt_s =~ /\D/;
  warn "limiting to dates > $opt_s\n" if $opt_v;
  @dirs = grep {$_ > $opt_s} @dirs;
}
if ( $opt_e ) {
  # end date
  $opt_e = time2str('%Y%m%d', str2time($opt_e)) if $opt_e =~ /\D/;
  warn "limiting to dates < $opt_e\n" if $opt_v;
  @dirs = grep {$_ < $opt_e} @dirs;
}
warn scalar(@dirs) ." to be downloaded\n" if $opt_v;
foreach my $dir (@dirs) {
  $ftp->cwd($dir);
  foreach my $file ($ftp->ls) {
    warn "downloading $file\n" if $opt_v;
    $ftp->get($file, "$tempdir/$file");
    warn "processing $file\n" if $opt_v;

    # "voip_innovations-20130628/20130628_20130628.CDR"
    my $batchname = "$format-$dir/$file";
    if ($exists{$batchname}) {
      warn "already imported $file\n";
      next;
    }
    my $import_options = {
      'file'            => "$tempdir/$file",
      'format'          => $format,
      'batch_namevalue' => $batchname,
      'empty_ok'        => 1,
    };
    $import_options->{'cdrtypenum'} = $opt_c if $opt_c;
  
    my $error = FS::cdr::batch_import($import_options);

    if ( $error ) {
      die "error processing $dir/$file: $error\n";
    }
  }
  $ftp->cwd('..');
}
warn "finished\n";
$dbh->commit;

###
# subs
###

sub usage {
  "Usage: \n  cdr-voip_innovations.import user login password\n [ options ]
  Options:
    -v: be verbose
    -d: enable FTP debugging (very noisy)
    -c num: apply a cdrtypenum to the imported CDRs
    -s date: start date
    -e date: end date
    -a: automatically choose start date from most recently downloaded batch
  ";
}