Adding collection-hq scripts
authorBill Erickson <berickxx@gmail.com>
Wed, 8 Apr 2015 15:12:13 +0000 (11:12 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/utility-scripts/collection_hq/crontab [new file with mode: 0644]
KCLS/utility-scripts/collection_hq/extract.sh [new file with mode: 0755]
KCLS/utility-scripts/collection_hq/get_bibs.sql [new file with mode: 0644]
KCLS/utility-scripts/collection_hq/get_items.sql [new file with mode: 0644]
KCLS/utility-scripts/collection_hq/send-email.pl [new file with mode: 0755]

diff --git a/KCLS/utility-scripts/collection_hq/crontab b/KCLS/utility-scripts/collection_hq/crontab
new file mode 100644 (file)
index 0000000..fcba1ca
--- /dev/null
@@ -0,0 +1,4 @@
+
+# change FTPUSER AND FTPPASS to suit
+0 2 20 * * cd /home/opensrf/Evergreen/KCLS/misc-scripts/collectionHQ && FTPUSER=<user> && FTPPASS=<pass> ./extract.sh
+
diff --git a/KCLS/utility-scripts/collection_hq/extract.sh b/KCLS/utility-scripts/collection_hq/extract.sh
new file mode 100755 (executable)
index 0000000..d00946f
--- /dev/null
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+LIBRARYNAME="KINGCOUNTY"
+DATE=`date +%Y%m%d`
+FILE="$LIBRARYNAME""$DATE".DAT
+# FTPUSER=""" -- passed via command line
+# FTPPASS="" -- passed via command line
+FTPSITE="ftp.collectionhq.com"
+EMAILFROM="collectionHQ@kcls.org"
+EMAILTO="amoroni@kcls.org,bbonner@kcls.org,mcarlson@kcls.org"
+
+function get_data_from_sql {
+  echo The extract for $DATE has begun. | ./send-email.pl --from "$EMAILFROM" --to "$EMAILTO" --subject "collectionHQ extraction has begun"
+  date
+  echo "Pre-warming the cache"
+  echo Fetching bibs...
+  psql -A -t -U evergreen < get_bibs.sql 2>&1 | cut -c8- | perl -ne 'if (m/^[0-9]/) { print STDERR; } else { print; }' > bibs-$DATE.txt
+  date
+  echo Fetching items...
+  psql -A -t -U evergreen < get_items.sql 2>&1 | cut -c8- | perl -ne 'if (m/^[0-9]/) { print STDERR; } else { print; }' > items-$DATE.txt
+  date
+  echo done.
+}
+
+function format_data {
+  echo "##HEADER##,##DAT##,##${DATE}##,##${LIBRARYNAME}##,,,##USA##" > $FILE
+  cat bibs-$DATE.txt >> $FILE
+  cat items-$DATE.txt >> $FILE
+  NUMBIBS=`wc -l bibs-$DATE.txt | cut -d' ' -f1`
+  NUMITEMS=`wc -l items-$DATE.txt | cut -d' ' -f1`
+  echo "##TRAILER##,$NUMBIBS,$NUMITEMS" >> $FILE
+}
+
+function upload_data {
+  gzip --best $FILE
+  ftp -v -n $FTPSITE <<END_SCRIPT
+quote USER $FTPUSER
+quote PASS $FTPPASS
+binary
+put $FILE.gz
+quit
+END_SCRIPT
+}
+
+function clean_up {
+  bzip2 bibs-$DATE.txt
+  bzip2 items-$DATE.txt
+  mkdir -p ~/chq_archive
+  mv bibs-$DATE.txt.bz2 items-$DATE.txt.bz2 $FILE.gz ~/chq_archive/
+  echo The extract for $DATE has finished. We uploaded data on $NUMBIBS bibs and $NUMITEMS items. | \
+    ./send-email.pl --from "$EMAILFROM" --to "$EMAILTO" --subject "collectionHQ extraction has finished"
+}
+
+get_data_from_sql && \
+      format_data && \
+      upload_data && \
+         clean_up #.
+exit
diff --git a/KCLS/utility-scripts/collection_hq/get_bibs.sql b/KCLS/utility-scripts/collection_hq/get_bibs.sql
new file mode 100644 (file)
index 0000000..4af4b0c
--- /dev/null
@@ -0,0 +1,22 @@
+--    Copyright (C) 2011-2012 Equinox Software Inc.
+--    Ben Ostrowsky <ben@esilibrary.com>
+--    Galen Charlton <gmc@esilibrary.com>
+--
+--    Original version sponsored by the King County Library System
+--  
+--    This program is free software; you can redistribute it and/or modify
+--    it under the terms of the GNU General Public License as published by
+--    the Free Software Foundation; either version 2 of the License, or
+--    (at your option) any later version.
+--
+--    This program is distributed in the hope that it will be useful,
+--    but WITHOUT ANY WARRANTY; without even the implied warranty of
+--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--    GNU General Public License for more details.
+--  
+--    You should have received a copy of the GNU General Public License
+--    along with this program; if not, write to the Free Software
+--    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+SET statement_timeout = 0;
+SELECT collectionHQ.write_bib_rows_to_stdout('KINGCOUNTY',1);
diff --git a/KCLS/utility-scripts/collection_hq/get_items.sql b/KCLS/utility-scripts/collection_hq/get_items.sql
new file mode 100644 (file)
index 0000000..0dd52c2
--- /dev/null
@@ -0,0 +1,22 @@
+--    Copyright (C) 2011-2012 Equinox Software Inc.
+--    Ben Ostrowsky <ben@esilibrary.com>
+--    Galen Charlton <gmc@esilibrary.com>
+--
+--    Original version sponsored by the King County Library System
+--  
+--    This program is free software; you can redistribute it and/or modify
+--    it under the terms of the GNU General Public License as published by
+--    the Free Software Foundation; either version 2 of the License, or
+--    (at your option) any later version.
+--
+--    This program is distributed in the hope that it will be useful,
+--    but WITHOUT ANY WARRANTY; without even the implied warranty of
+--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+--    GNU General Public License for more details.
+--  
+--    You should have received a copy of the GNU General Public License
+--    along with this program; if not, write to the Free Software
+--    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+SET statement_timeout = 0;
+SELECT collectionHQ.write_item_rows_to_stdout('KINGCOUNTY',1);
diff --git a/KCLS/utility-scripts/collection_hq/send-email.pl b/KCLS/utility-scripts/collection_hq/send-email.pl
new file mode 100755 (executable)
index 0000000..1b0a024
--- /dev/null
@@ -0,0 +1,53 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Email::Sender::Simple qw(sendmail);
+use Email::Simple;
+use Email::Simple::Creator;
+use Email::Sender::Transport::SMTP qw(); 
+use Getopt::Long;
+
+my $to = 'esi@localhost.localdomain';
+my $from = $to;
+my $subject = "";
+my $body = "";
+
+my $result = GetOptions (
+       "to=s"   => \$to,
+       "from=s" => \$from,
+       "subject=s" => \$subject
+);
+
+my $transport = Email::Sender::Transport::SMTP->new ({
+       host => 'smtp.kcls.org',
+       port => 25
+});
+
+while (<>) { $body .= $_; }
+
+my $email = Email::Simple->create(
+       header => [
+               To      => $to,
+               From    => $from,
+               Subject => $subject,
+       ],
+       body => $body,
+);
+
+sendmail($email, { transport => $transport });
+
+=head1 NAME
+
+send-email.pl
+
+=head1 USAGE
+
+echo Hello world! | \
+
+       send-email.pl \
+
+               --from '"Ben Ostrowsky" <collectionHQ@kcls.org>' \
+
+               --to '"Ben Ostrowsky" <ben@esilibrary.com>, "Someone Else" <someone@else>' \
+
+               --subject "Stuff"