From 6fb972eec6953a40d57b76b7894952abdc4437d2 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 22 Jul 2015 15:34:05 +0200 Subject: refactoring diff --git a/rhimport b/rhimport index 05bf4c9..5cfd4b2 100755 --- a/rhimport +++ b/rhimport @@ -22,7 +22,6 @@ # use strict; - use Getopt::Long; use lib '/usr/local/share/rhimport/'; diff --git a/rhimport-btl b/rhimport-btl index e212793..939d763 100755 --- a/rhimport-btl +++ b/rhimport-btl @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use LWP::Simple; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-btl.stat"; my $RSS_URL = "http://www.btlonline.org/rss/btl128.xml"; my $PV_ID = '221'; @@ -58,12 +52,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "betweenlin"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found more or less than one Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -108,13 +106,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -156,7 +154,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-dn b/rhimport-dn index 47933f4..4f6275a 100755 --- a/rhimport-dn +++ b/rhimport-dn @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use Time::localtime; use XML::Feed; @@ -36,11 +35,6 @@ use IPC::Open3; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-dn.stat"; my $FILES_RSS_URL = "http://www.democracynow.org/podcast-stations.xml"; my $HEADLINES_RSS_URL = "http://www.democracynow.org/podcast.xml"; @@ -64,7 +58,11 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "democracyn"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); @@ -96,7 +94,7 @@ for my $href ( @allowed_dbs ) { if($dropbox eq "") { print "no dropbox for day in question\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -122,13 +120,13 @@ for my $entry ($files_feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $path[-1]) { print "Already downloaded file of day in question\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $path[-1]; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -219,7 +217,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-fe b/rhimport-fe index fbcaca1..266708a 100755 --- a/rhimport-fe +++ b/rhimport-fe @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use File::Touch; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-fe.stat"; my $NOTE_FILE = $ENV{'HOME'} . "/rhimport-fe.last_note"; my $NEW_FILE = $ENV{'HOME'} . "/rhimport-fe.is_new"; @@ -84,19 +78,23 @@ $group .= "Mi" if($dow == 3); $group .= "Do" if($dow == 4); $group .= "Fr" if($dow == 5); -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $dropbox = ""; my $to_cart = 0; if(scalar(@allowed_dbs) == 0) { print "no dropbox for day in question\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } if(scalar(@allowed_dbs) != 1) { print "found more or less than one Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -123,13 +121,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -182,7 +180,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-fw b/rhimport-fw index 3a3f6e5..45b0b2c 100755 --- a/rhimport-fw +++ b/rhimport-fw @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-fw.stat"; my $LAST_FILE = $ENV{'HOME'} . "/rhimport-fw.last"; my $RSS_URL = "http://cba.fro.at/seriesrss/263965?c=Kfs2IoV2Wmd"; @@ -59,14 +53,18 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "freiewelle"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $idx = 0; my $idx_reb = 1; if(scalar(@allowed_dbs) != 2) { print "found more or less than 2 Dropboxes for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[$idx]->{'PATH'}; @@ -95,7 +93,7 @@ my $current_stat = `cat $STAT_FILE`; my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id) { print "Already downloaded current file\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -134,13 +132,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded current file\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -193,7 +191,7 @@ $log .= $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox_reb, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-mz b/rhimport-mz index 60b91f3..f3c4535 100755 --- a/rhimport-mz +++ b/rhimport-mz @@ -23,7 +23,6 @@ use strict; use Switch; -use DBI; use Date::Calc; use Time::localtime; use IO::Handle; @@ -33,11 +32,6 @@ use File::Slurp; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my @STATIC_FILES = ($ENV{'HOME'} . "/frontex", $ENV{'HOME'} . "/maribor" ); @@ -85,11 +79,15 @@ switch($dow) { else { print("invalid day of week?!\n"); exit 1; } } -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found no or more than one Dropboxes for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -99,7 +97,7 @@ my $import_date = sprintf("%04d-%02d-%02d", @import_day); my $current_date = `cat $STAT_FILE`; if($current_date eq $import_date) { print "Already downloaded file of day in question\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -220,7 +218,7 @@ foreach my $file (@files) { $cart++; } -$dbh->disconnect(); +rhimport::closedb($dbh); if(!$ret) { print "\nImport Error:\n"; diff --git a/rhimport-nw b/rhimport-nw index 3285224..367ef6b 100755 --- a/rhimport-nw +++ b/rhimport-nw @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-nw.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/262514?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -58,12 +52,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "netwatcher"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found more or less than 1 Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -117,13 +115,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -169,7 +167,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-o94n b/rhimport-o94n index 10989a8..0ce0654 100755 --- a/rhimport-o94n +++ b/rhimport-o94n @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-o94n.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/264131?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -58,7 +52,11 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "o94"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); @@ -74,7 +72,7 @@ for my $db (@allowed_dbs) { } if($to_cart == 0) { print "no dropbox found for Tuesday?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -136,13 +134,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -188,7 +186,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-oi b/rhimport-oi index 3ed4e5b..714ef8e 100755 --- a/rhimport-oi +++ b/rhimport-oi @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-oi.stat"; my $LAST_FILE = $ENV{'HOME'} . "/rhimport-oi.last"; my $RSS_URL = "http://npla.de/onda/feed/serie=3"; @@ -58,12 +52,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "ondainfo"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found more or less than one Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -90,7 +88,7 @@ my $current_stat = `cat $STAT_FILE`; my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id) { print "Already downloaded current file\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -114,7 +112,7 @@ for my $entry ($feed->entries) { $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -156,7 +154,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-ra b/rhimport-ra index ec30851..acf764d 100755 --- a/rhimport-ra +++ b/rhimport-ra @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-ra.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/262430?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -58,14 +52,18 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "RadioATTAC"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $idx = 0; my $idx_reb = 1; if(scalar(@allowed_dbs) != 2) { print "found more or less than 2 Dropboxes for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[$idx]->{'PATH'}; @@ -119,13 +117,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -178,7 +176,7 @@ $log .= $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox_reb, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-rs b/rhimport-rs index d0d7337..f2ca868 100755 --- a/rhimport-rs +++ b/rhimport-rs @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use LWP::Simple; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-rs.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/262433?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -60,7 +54,11 @@ $user =~ s/\n//; my $group = "radioStimm"; my %week_table = ( 1 => 2, 2 => 6, 3 => 2, 4 => 6 ); -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $dropbox = ""; @@ -73,7 +71,7 @@ foreach(@allowed_dbs) { } if(!$to_cart) { print "no dropbox found!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -113,7 +111,7 @@ print "day of latest original broadcast before next Radio Helsinki broadcast: " if(Date::Calc::Delta_Days(@broadcast_date, @today) <= 0) { print "File won't be available by now!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -153,13 +151,13 @@ for my $entry ($feed->entries) { my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id && $current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -205,7 +203,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-sm b/rhimport-sm index f48b809..d3f5beb 100755 --- a/rhimport-sm +++ b/rhimport-sm @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use LWP::Simple; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-sm.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/263673?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -59,12 +53,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "soundsmove"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found more or less than one Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -78,7 +76,7 @@ if(($curweek != 1 && $curweek != 4) || ($curweek == 1 && $dow > 3) || ($curweek == 4 && $dow <= 3)) { print "File won't be available by now!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } print "today: " . Date::Calc::Date_to_Text(@today) . "\n"; @@ -100,7 +98,7 @@ print "day of latest original broadcast before next Radio Helsinki broadcast: " if(Date::Calc::Delta_Days(@broadcast_date, @today) <= 0) { print "File won't be available by now!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -140,13 +138,13 @@ for my $entry ($feed->entries) { my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id && $current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -192,7 +190,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-sv b/rhimport-sv index b148e89..8702e25 100755 --- a/rhimport-sv +++ b/rhimport-sv @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use LWP::Simple; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-sv.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/262457?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -59,12 +53,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "svetjevas"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found more or less than one Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -77,7 +75,7 @@ my $dow = Date::Calc::Day_of_Week(@today); if(($curweek != 3 && $curweek != 4) || ($curweek == 4 && $dow > 2)) { print "File won't be available by now!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } print "today: " . Date::Calc::Date_to_Text(@today) . "\n"; @@ -99,7 +97,7 @@ print "day of latest original broadcast before next Radio Helsinki broadcast: " if(Date::Calc::Delta_Days(@broadcast_date, @today) <= 0) { print "File won't be available by now!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -139,13 +137,13 @@ for my $entry ($feed->entries) { my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id && $current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -191,7 +189,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-tr b/rhimport-tr index d86bdb9..3a4ca76 100755 --- a/rhimport-tr +++ b/rhimport-tr @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-tr.stat"; my $RSS_URL = "http://cba.fro.at/series/tierrechtsradio/feed?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -58,13 +52,17 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "tierrecht"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $idx = 0; if(scalar(@allowed_dbs) != 1) { print "found more or less than 1 Dropbox for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[$idx]->{'PATH'}; @@ -73,7 +71,7 @@ my $to_cart = $allowed_dbs[$idx]->{'TO_CART'}; my @today = Date::Calc::Standard_to_Business(Date::Calc::Today()); if($today[2] == 5) { print "please don't run this script on fridays!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my @broadcast_day = Date::Calc::Business_to_Standard($today[0], $today[1], 5); @@ -119,13 +117,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -171,7 +169,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-ut b/rhimport-ut index 327abff..34b4dbb 100755 --- a/rhimport-ut +++ b/rhimport-ut @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -36,11 +35,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-ut.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/264016?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -58,14 +52,18 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "uton"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); my $idx = 0; my $idx_reb = 1; if(scalar(@allowed_dbs) != 2) { print "found more or less than 2 Dropboxes for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[$idx]->{'PATH'}; @@ -119,13 +117,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -178,7 +176,7 @@ $log .= $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox_reb, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-zf b/rhimport-zf index c780540..c4b9365 100755 --- a/rhimport-zf +++ b/rhimport-zf @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use XML::Feed; use XML::Feed::Entry; @@ -37,11 +36,6 @@ use File::Touch; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-zf.stat"; my $NOTE_FILE = $ENV{'HOME'} . "/rhimport-zf.last_note"; my $NEW_FILE = $ENV{'HOME'} . "/rhimport-zf.is_new"; @@ -60,7 +54,11 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "zipfm"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); @@ -87,7 +85,7 @@ for my $href ( @allowed_dbs ) { if($dropbox eq "") { print "no dropbox for day in question\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } @@ -111,13 +109,13 @@ for my $entry ($feed->entries) { my $current_file = `cat $STAT_FILE`; if($current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -171,7 +169,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; diff --git a/rhimport-zfw b/rhimport-zfw index f6ea83c..4bffb61 100755 --- a/rhimport-zfw +++ b/rhimport-zfw @@ -22,7 +22,6 @@ # use strict; -use DBI; use Date::Calc; use Time::localtime; use XML::Feed; @@ -37,11 +36,6 @@ use HTML::Entities; use lib '/usr/local/share/rhimport/'; use rhimport; -my $DBHOST = "airplay"; -my $DBUSER = "rivendell"; -my $DBPW = "lldriven"; -my $DB = "rivendell"; - my $STAT_FILE = $ENV{'HOME'} . "/rhimport-zfw.stat"; my $RSS_URL = "http://cba.fro.at/seriesrss/263764?c=Kfs2IoV2Wmd"; $XML::Feed::MULTIPLE_ENCLOSURES=1; @@ -59,12 +53,16 @@ my $user = `/usr/bin/id -un`; $user =~ s/\n//; my $group = "zipfmwla"; -my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr"; +my ($dbh, $errorstring) = rhimport::opendb(); +if(!defined $dbh) { + print "$errorstring\n"; + exit 1; +} my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group); if(scalar(@allowed_dbs) != 1) { print "found no or more than one Dropboxes for this group?!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } my $dropbox = $allowed_dbs[0]->{'PATH'}; @@ -122,13 +120,13 @@ for my $entry ($feed->entries) { my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/; if($current_id eq $id && $current_file eq $ff->output_file) { print "Already downloaded file of today\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 0; } $out_file = $ff->output_file; if(!rhimport::check_file_extension($out_file)) { print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - $dbh->disconnect(); + rhimport::closedb($dbh); exit 1; } @@ -174,7 +172,7 @@ my $import_log; ($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb); $log .= $import_log; -$dbh->disconnect(); +rhimport::closedb($dbh); unlink $file; -- cgit v0.10.2