diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-07-23 16:56:31 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-07-23 16:56:31 (GMT) |
commit | d9df4264e66e6091538ab24c41fad20131526460 (patch) | |
tree | dc09937184619221e13e279ae0e9906be93ee370 /rhimport.pm | |
parent | ec10a98e49866edf037443504f892cd578d28038 (diff) |
fetch dropboxes and listing allowed shows works now
Diffstat (limited to 'rhimport.pm')
-rw-r--r-- | rhimport.pm | 179 |
1 files changed, 119 insertions, 60 deletions
diff --git a/rhimport.pm b/rhimport.pm index 3451bcd..3cdcf62 100644 --- a/rhimport.pm +++ b/rhimport.pm @@ -117,86 +117,145 @@ sub closedb $dbh->disconnect(); } -sub get_dropboxes +sub get_token { - my ($dbh, $user, $group) = @_; + my ($dbh, $username) = @_; - my $sql = ""; - if($group) { - $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.PATH,DROPBOXES.TO_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME='$user' and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.GROUP_NAME='$group';}; - } else { - $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.PATH,DROPBOXES.TO_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME='$user' and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME;}; + my $sql = qq{select PASSWORD from USERS where LOGIN_NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, "Database Error: " . $dbh->errstr); + + $sth->execute($username) + or return (undef, "Database Error: " . $sth->errstr); + + my ($token) = $sth->fetchrow_array; + $sth->finish(); + + unless(defined $token) { + return (undef, "user '" . $username . "' not known by rivendell") + } + return ($token, 'OK', 'success'); +} + + +sub get_showtitle_and_log +{ + my ($dbh, $showid) = @_; + + my $sql = qq{select TITLE,MACROS from CART where NUMBER = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, undef, "Database Error: " . $dbh->errstr); + + $sth->execute($showid) + or return (undef, undef, "Database Error: " . $sth->errstr); + + my ($title, $macros) = $sth->fetchrow_array; + $sth->finish(); + + unless(defined $title) { + return (undef, undef, "Show with ID=" . $showid . " not found!") + } + unless(defined $macros) { + return (undef, undef, "Show with ID=" . $showid . " has no macro!"); } - my $sth = $dbh->prepare($sql); - $sth->execute(); + + unless($macros =~ /^LL 1 ([^ ]+) 0\!$/) { + return (undef, undef, "Show with ID=" . $showid . " has invalid macro: '" . $macros . "'"); + } + my $log = $1; + + return ($title, $log); +} + +sub get_dropboxes +{ + my ($dbh, $username) = @_; + + my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.TO_CART,DROPBOXES.NORMALIZATION_LEVEL,DROPBOXES.AUTOTRIM_LEVEL,DROPBOXES.SET_USER_DEFINED,GROUPS.DEFAULT_LOW_CART,GROUPS.DEFAULT_HIGH_CART,GROUPS.DESCRIPTION from USER_PERMS, DROPBOXES, GROUPS where USER_PERMS.USER_NAME=? and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME and DROPBOXES.GROUP_NAME=GROUPS.NAME and DROPBOXES.STATION_NAME=?;}; + + my $sth = $dbh->prepare($sql) + or return (undef, "Database Error: " . $dbh->errstr); + + $sth->execute($username, 'import-dropbox') # TODO: hardcoded value + or return (undef, "Database Error: " . $sth->errstr); + my @allowed_dbs; - while(my ($group, $path, $to_cart, $desc) = $sth->fetchrow_array()) { - $path =~ s/\/\*$//; - my $name = $path; - $name =~ s/^\/programm\///; - if($name =~ /^([0-9]{2}-[A-Za-z]+)\/([0-9]{2})([0-9]{2})-([01]{4})-([0-9]{3})-(.*)$/) { - $name = "$1 - $2:$3 - $6 ($4, $5)"; - } - elsif($name =~ /^([0-9]{2}-[A-Za-z]+)\/programmvorschau_(.*)$/) { - $name = "Programmvorschau - $1 - $2"; - } - elsif($name =~ /^([0-9]{2}-[A-Za-z]+)\/jingle$/ || $name =~ /^jingles\/(.*)$/) { - $name = "Jingles - $1"; - } - elsif($name =~ /^pool\/pool(.*)$/) { - $name = "Pool $1 - $desc"; - } - elsif($name =~ /^pool\/(.*)$/) { - $name = "Pool - $1"; - } - elsif($name =~ /^sondersendungen\/(.*)$/) { - $name = "Sondersendungen - $1"; + while(my ($group, $to_cart, $normlevel, $trimlevel, $params, $lowcart, $highcart, $groupdesc) = $sth->fetchrow_array()) { + my @p = split(';', $params); + + next unless($p[0] eq "S"); + + my $entry = {}; + $entry->{'GROUP'} = $group; + $entry->{'GROUPDESC'} = $groupdesc; + $entry->{'GROUPLOWCART'} = int $lowcart; + $entry->{'GROUPHIGHCART'} = int $highcart; + $entry->{'NORMLEVEL'} = int $normlevel; + $entry->{'TRIMLEVEL'} = int $trimlevel; + $entry->{'PARAM'} = $params; + $entry->{'TYPE'} = 'show'; + $entry->{'SHOWID'} = $to_cart; + + my ($title, $log, $status, $errorstring) = get_showtitle_and_log($dbh, $to_cart); + unless (defined $title && defined $log) { + return (undef, $status, $errorstring); } - elsif($name =~ /^autoimport\/(.*)$/) { - $name = "autoimport - $1"; - } - my $perm = {}; - $perm->{'GROUP'} = $group; - $perm->{'PATH'} = $path; - $perm->{'TO_CART'} = $to_cart; - $perm->{'NAME'} = $name; - push @allowed_dbs, $perm; + $entry->{'SHOWTITLE'} = $title; + $entry->{'SHOWLOG'} = $log; + + $entry->{'SHOWRHYTHM'} = $p[1]; + $entry->{'SHOWDOW'} = int $p[2]; + $entry->{'SHOWDOW'} = 0 unless $entry->{'SHOWDOW'} < 7; + substr($p[3], 2, 0) = ':'; + $entry->{'SHOWSTARTTIME'} = $p[3]; + $entry->{'SHOWLEN'} = int $p[4]; + + push @allowed_dbs, $entry; } $sth->finish(); - return sort { uc($a->{'NAME'}) cmp uc($b->{'NAME'}) } @allowed_dbs; + return @allowed_dbs; } -sub get_cart_range +sub get_show_carts { - my ($dbh, $group) = @_; + my ($dbh, $logname, $group_low_cart, $group_high_cart) = @_; - my $sql = qq{select DEFAULT_LOW_CART,DEFAULT_HIGH_CART from GROUPS where NAME='$group';}; - my $sth = $dbh->prepare($sql); - $sth->execute(); - my @carts; - my ($low_cart, $high_cart) = $sth->fetchrow_array(); + my $sql = qq{select LOG_EXISTS from LOGS where NAME = ?;}; + my $sth = $dbh->prepare($sql) + or return (undef, "Database Error: " . $dbh->errstr); + + $sth->execute($logname) + or return (undef, "Database Error: " . $sth->errstr); + + my $log_exists = $sth->fetchrow_array; $sth->finish(); - return ($low_cart, $high_cart); -} + if(!defined $log_exists && $log_exists eq 'Y') { + return (undef, "log $logname does not exist") + } -sub get_used_carts -{ - my ($dbh, $group) = @_; + $logname=~s/ /_/g; + $logname = $dbh->quote_identifier($logname . '_LOG'); + $sql = qq{select COUNT,CART_NUMBER from $logname order by COUNT;}; + + $sth = $dbh->prepare($sql) + or return (undef, "Database Error: " . $dbh->errstr); + + $sth->execute() + or return (undef, "Database Error: " . $sth->errstr); - my ($low_cart, $high_cart) = get_cart_range($dbh, $group); my @carts; + while(my ($count, $cart) = $sth->fetchrow_array()) { + if($cart >= $group_low_cart && $cart <= $group_high_cart) { + push @carts, $cart; + } + } + $sth->finish(); + return @carts; } -sub get_num_carts -{ - my ($dbh, $group) = @_; - - my ($low_cart, $high_cart) = get_cart_range($dbh, $group); - return $high_cart - $low_cart + 1; -} sub clear_carts { |