summaryrefslogtreecommitdiff
path: root/bin/cdr.http_and_import
blob: 8910eece68745c70df3b7e09e433aaea41ec96d1 (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
#!/usr/bin/perl
#
# Usage:
#  cdr.http_and_import [ -p prefix ] [ -e extension ] [ -v ] user format URL
#
# -e: file extension, defaults to .csv
# -d: if specified, moves files to the specified folder when done

use strict;
use Getopt::Std;
use WWW::IndexParser;
#use LWP::UserAgent;
use FS::UID qw(adminsuidsetup datasrc dbh);
use FS::cdr;

###
# parse command line
###

use vars qw( $opt_p $opt_e $opt_v );
getopts('p:e:v');

$opt_e ||= 'csv';
#$opt_e = ".$opt_e" unless $opt_e =~ /^\./;
$opt_e =~ s/^\.//;

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

# %%%FREESIDE_CACHE%%%
my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs';
mkdir $cachedir unless -d $cachedir;

my $format = shift or die &usage;

use vars qw( $URL );
$URL = shift or die &usage;

###
# get the file list
###

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

my @files = WWW::IndexParser->new(url => $URL);

###
# import each file
###

foreach my $file ( @files ) {

  my $filename = $file->{filename};

  if ( $opt_p ) { next unless $filename =~ /^$opt_p/ };
  if ( $opt_e ) { next unless $filename =~ /\.$opt_e$/i };

  #check and see if we've gotten this file already!!!
  #just going to cheat with filenames in the cache for now
  if ( -e "$cachedir/$filename" ) {
    warn "Already have unprocessed $cachedir/$filename; skipping\n"; # if $opt_v;
    next;
  }
  if ( -e "$cachedir/$filename.DONE" ) {
    warn "Already processed $cachedir/$filename; skipping\n" if $opt_v;
    next;
  }

  warn "Downloading $filename\n" if $opt_v;

  #get the file

  my $ua = LWP::UserAgent->new;
  my $response = $ua->get("$URL/$filename");

  unless ( $response->is_success ) {
    die "Error retreiving $URL/$filename: ". $response->status_line;
  }

  open(FILE, ">$cachedir/$filename")
    or die "can't open $cachedir/$filename: $!";
  print FILE $response->content;
  close FILE or die "can't close $cachedir/$filename: $!";

  warn "Processing $filename\n" if $opt_v;

  my $error = FS::cdr::batch_import( {
    'file'            => "$cachedir/$filename",
    'format'          => $format,
    'batch_namevalue' => $filename,
    'empty_ok'        => 1,
  } );
  die $error if $error;

  close FILE;

  rename("$cachedir/$filename", "$cachedir/$filename.DONE");

}

###
# sub
###

sub usage {
  "Usage: \n  cdr.http_and_import  [ -p prefix ] [ -e extension ] [ -v ] user format URL\n";
}