csv fetcher; remote account
authorBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 17:18:50 +0000 (12:18 -0500)
committerBill Erickson <berick@esilibrary.com>
Fri, 7 Dec 2012 17:18:50 +0000 (12:18 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/support-scripts/csv_notify_fetcher.pl

index b033a26..8fd0391 100755 (executable)
@@ -7,12 +7,14 @@ use OpenILS::Utils::CStoreEditor;
 use OpenILS::Utils::Cronscript;
 use OpenILS::Utils::Fieldmapper;
 use OpenSRF::Utils::Logger qw/$logger/;
-binmode(STDOUT, ":utf8");
 
 my %defaults = (
-    'osrf-config=s' => '/openils/conf/opensrf_core.xml',
-    'response-file=s' => '',
-    'verbose'       => 0
+    'osrf-config=s'     => '/openils/conf/opensrf_core.xml',
+    'response-file=s'   => '',
+    'remote-account=s'  => 0,
+    'remote-file=s'     => '',
+    'local-file=s'     => '', # store fetched file locally
+    'verbose'           => 0
 );
 
 sub help {
@@ -32,8 +34,52 @@ my $opts = $o->MyGetOptions();
 $o->bootstrap;
 
 my $file = $opts->{'response-file'};
+my $remote_account = $opts->{'remote-account'};
+my $remote_file = $opts->{'remote-file'};
+my $local_file = $opts->{'local-file'};
+my $verbose = $opts->{'verbose'};
+
+die "--response-file OR --remote-account and --remote-file required\n"
+    unless $file or ($remote_account and $remote_file);
+
+if (!$file and $remote_account) {
+
+    my $racct = $o->editor->retrieve_config_remote_account($remote_account);
+    die "No such remote account $remote_account" unless $racct;
+
+    my $type;
+    my $host = $racct->host;
+    ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
+    ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
+
+    if ($local_file) {
+        $file = $local_file;
+    } else {
+        my $tmp_file = File::Temp->new(); 
+        $file = $tmp_file->filename;
+    }
+
+    my $acct = OpenILS::Utils::RemoteAccount->new(
+        type            => $type,
+        account_object  => $racct,
+        local_file      => $file,
+        remote_file     => $remote_file
+    );
+
+    my $res = $acct->put;
+
+    die "Unable to push to remote server [$remote_account] : " . 
+        $acct->error . "\n" unless $res;
+
+    print "Pushed file to $res\n" if $verbose;
+}
+
+# at this point, $file contains CSV, because it was already there or 
+# because we just fetched it from the remote account
 open(FILE, $file) or
     die "Unable to open response file: '$file' : $!\n";
+
+binmode(FILE, ":utf8");
     
 my $e = $o->editor(connect => 1);