4 # cdr.http_and_import [ -p prefix ] [ -e extension ] [ -v ] user format URL
6 # -e: file extension, defaults to .csv
7 # -d: if specified, moves files to the specified folder when done
13 use FS::UID qw(adminsuidsetup datasrc dbh);
20 use vars qw( $opt_p $opt_e $opt_v );
24 #$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
27 my $user = shift or die &usage;
30 # %%%FREESIDE_CACHE%%%
31 my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
32 mkdir $cachedir unless -d $cachedir;
34 my $format = shift or die &usage;
37 $URL = shift or die &usage;
43 warn "Retreiving directory listing\n" if $opt_v;
45 my @files = WWW::IndexParser->new(url => $URL);
51 foreach my $file ( @files ) {
53 my $filename = $file->{filename};
55 if ( $opt_p ) { next unless $filename =~ /^$opt_p/ };
56 if ( $opt_e ) { next unless $filename =~ /\.$opt_e$/i };
58 #check and see if we've gotten this file already!!!
59 #just going to cheat with filenames in the cache for now
60 if ( -e "$cachedir/$filename" ) {
61 warn "Already have unprocessed $cachedir/$filename; skipping\n"; # if $opt_v;
64 if ( -e "$cachedir/$filename.DONE" ) {
65 warn "Already processed $cachedir/$filename; skipping\n" if $opt_v;
69 warn "Downloading $filename\n" if $opt_v;
73 my $ua = LWP::UserAgent->new;
74 my $response = $ua->get("$URL/$filename");
76 unless ( $response->is_success ) {
77 die "Error retreiving $URL/$filename: ". $response->status_line;
80 open(FILE, ">$cachedir/$filename")
81 or die "can't open $cachedir/$filename: $!";
82 print FILE $response->content;
83 close FILE or die "can't close $cachedir/$filename: $!";
85 warn "Processing $filename\n" if $opt_v;
87 my $error = FS::cdr::batch_import( {
88 'file' => "$cachedir/$filename",
90 'params' => { 'cdrbatch' => $filename },
97 rename("$cachedir/$filename", "$cachedir/$filename.DONE");
106 "Usage: \n cdr.http_and_import [ -p prefix ] [ -e extension ] [ -v ] user format URL\n";