summaryrefslogtreecommitdiff
path: root/rhimport.pm
blob: 254166041527cf6335e769913ccdf8b0bc2b8232 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl -w
#
#
#  rhimport
#
#  Copyright (C) 2009 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhimport.
#
#  rhimport 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 3 of the License, or
#  any later version.
#
#  rhimport 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 rhimport. If not, see <http://www.gnu.org/licenses/>.
#

use strict;

package rhimport;

sub get_dropboxes
{
  my $dbh = shift;
  my $user = shift;

  my $sql = qq{select USER_PERMS.GROUP_NAME,DROPBOXES.PATH from USER_PERMS, DROPBOXES where USER_PERMS.USER_NAME='$user' and DROPBOXES.GROUP_NAME=USER_PERMS.GROUP_NAME;};
  my $sth = $dbh->prepare($sql);
  $sth->execute();
  my @allowed_dbs;
  while(my ($group, $path) = $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";
    }
    elsif($name =~ /^pool\/(.*)$/) {
      $name = "Pool - $1";
    }
    elsif($name =~ /^sondersendungen\/(.*)$/) {
      $name = "Sondersendungen - $1";
    }
    my $perm = {};
    $perm->{'GROUP'} = $group;
    $perm->{'PATH'} = $path;
    $perm->{'NAME'} = $name;
    push @allowed_dbs, $perm;
  }
  $sth->finish();

  return sort { uc($a->{'NAME'}) cmp uc($b->{'NAME'}) } @allowed_dbs;
}

sub get_cart_range
{
  my $dbh = shift;
  my $group = shift;

  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();
  $sth->finish();

  return ($low_cart, $high_cart);
}

sub get_used_carts
{
  my $dbh = shift;
  my $group = shift;

  my ($low_cart, $high_cart) = get_cart_range($dbh, $group);
  my @carts;
  return @carts;
}

sub clear_carts
{
  my $dbh = shift;
  my $group = shift;
  my ($low_cart, $high_cart) = get_cart_range($dbh, $group);

  print "clearing Carts $low_cart - $high_cart\n";
  my $cart = $low_cart;
  while($cart <= $high_cart) {
    my $sql = qq{select CUT_NAME from CUTS where CART_NUMBER='$cart';};
    my $sth = $dbh->prepare($sql);
    $sth->execute();
    while(my $cut_name = $sth->fetchrow_array()) {
      my $filename = "/var/snd/$cut_name.wav";

      ### call unlink!!!
      print " - deleting $filename\n";
    }
    $sth->finish();
    
#    my $sql = qq{delete from CUTS where CART_NUMBER='$cart';};
#    my $sth = $dbh->prepare($sql);
#    $sth->execute();
#    $sth->finish();

#    my $sql = qq{delete from CARTS where NUMBER='$cart';};
#    my $sth = $dbh->prepare($sql);
#    $sth->execute();
#    $sth->finish();

    $cart++;
  }
}

sub import_playlist
{
  my ($playlist, $dropbox, $progress_cb) = @_;

  print "Starting import from playlist $playlist to $dropbox";
  my @entries;
  open (FILE, $playlist) or die "can't open playlist $playlist: $!";
  while (my $entry = <FILE>) {
    next if($entry =~ /^#/);
    next if($entry =~ /^\s/);
    $entry =~ s/\n$//;
    push @entries, $entry;    
  }
  close(FILE);
  my $num_entries = scalar(@entries);
  print " ($num_entries Files in list)\n";
  
  my $cnt = 0;
  my $ret = 0;
  for my $entry ( @entries ) {
    if($progress_cb) {
      $progress_cb->($cnt, $num_entries, $entry);
    }

    $ret = import_file($entry, $dropbox);
    last if($ret != 0);

    $cnt++;
  }

  if($progress_cb) {
    $progress_cb->($num_entries, $num_entries, "Import abgeschlossen!");
  }

  return $ret;
}

sub import_single
{
  my ($file, $dropbox, $progress_cb) = @_;
  print "Starting import from file $file to $dropbox\n";
  if($progress_cb) {
    $progress_cb->(0, 1, $file);
  }
  my $ret = import_file($file, $dropbox, $progress_cb);

  if($progress_cb) {
    $progress_cb->(1, 1, "Import abgeschlossen!");
  }
  return $ret;
}

sub import_file
{
  my ($file, $dropbox) = @_;

  print " - importing $file to $dropbox .. ";
  $| = 1;

  sleep(1);
  print "Ok\n";

  return 0;
}

1;