summaryrefslogtreecommitdiff
path: root/contrib/at-vdradmin2xxv.pl
blob: 6d41b554ea6d265885c1b547f364119e67d575eb (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI; #Database independent interface for Perl
use DBD::mysql; #MySQL driver for the Perl5 Database Interface (DBI)
use Getopt::Std;

my $db = "xxv";
my $host = "localhost";
my $port = "3306";
my $usr = "root";
my $pwd = "";


my $Usage = qq{
Import or Export Autotimer from XXV to or from a VDRAdmin file
Usage: $0 [options] file

Options:
          -H <hostname>  : Host name of the MySQL server (default: '$host')
          -P <port>      : Port number of MySQL server (default: '$port')
          -d <database>  : XXV database name (default: '$db')
          -u <user>      : Username (default: '$usr')
          -p <password>  : Password (default: '$pwd')
          -e             : Export to file (default: Import from file)
          -h             : Help

};

use vars qw/ %opt /;
my $dbh;

die $Usage if (!$ARGV[0] || !getopts("d:H:P:u:p:eh", \%opt) || $opt{h} );


$db   = $opt{d} if($opt{d});
$host = $opt{H} if($opt{H});
$port = $opt{P} if($opt{P});
$usr =  $opt{u} if($opt{u});
$pwd =  $opt{p} if($opt{p});

my $filename  = "@ARGV";

# ------------------
sub ChannelArray {
# ------------------
    my $field = shift || return undef;
    my $sql = sprintf('select %s, POS from CHANNELS order by POS', $field);
    my $erg = $dbh->selectall_arrayref($sql);
    return $erg;
}

# ------------------
sub PosToChannel {
# ------------------
    my $pos = shift || return undef;
    my $sql = sprintf('select Id from CHANNELS where POS = "%lu"', $pos);
    my $erg = $dbh->selectall_arrayref($sql);
    return $erg->[0][0];
}

# ------------------
sub _insert {
# ------------------
    my $data = shift || return;

    if(ref $data eq 'HASH') {
        my ($names, $vals, $kenn);
        map {
            push(@$names, $_);
            push(@$vals, $data->{$_}),
            push(@$kenn, '?'),
        } sort keys %$data;

        my $sql = sprintf("REPLACE INTO AUTOTIMER (%s) VALUES (%s)",
                join(', ', @$names),
                join(', ', @$kenn),
        );
        my $sth = $dbh->prepare( $sql );
        $sth->execute( @$vals );
    } else {
        my $sth = $dbh->prepare('REPLACE INTO AUTOTIMER VALUES (?,?,?,?,?,?,?,?,?)');
        $sth->execute( @$data );
    }
}

# ------------------
# Name:  autotimerimport
# Descr: Read a file for import autotimer export list.
# Usage: autotimerimport($filename);
# ------------------
sub autotimerimport {
    my $AT_FILENAME = shift || die("No filename to import! Use $0 'vdradmind.at'\n");

    my $cnt = "0";
    open(AT_FILE, "$AT_FILENAME") || return die sprintf("Can't open file '%s' : %s\n",$AT_FILENAME,$!);
    while(<AT_FILE>) {
        chomp;
        next if ($_ eq "");

        my($active, $pattern, $section, $start, $stop, $episode, $prio,
            $lft, $channel, $directoy) = split(/\:/, $_);

        my $data;
        $data->{Id} = 0;
        if($active eq "1") { $data->{Activ} = 'y'; }
	      else               { $data->{Activ} = 'n'; }

        my @vals;
        $data->{Done} = 'timer';
        $data->{Search} = $pattern;
        push(@vals, "title")
            if($section & 1);
        push(@vals, "subtitle")
            if($section & 2);
        push(@vals, "description")
            if($section & 4);
        $data->{InFields} = join(',', @vals);
        if( $channel ne "") {
            if (my $chid = &PosToChannel($channel)) {
                $data->{Channels} = $chid;
            }
        }
        $data->{Priority} = $prio;
        $data->{Lifetime} = $lft;
        $data->{Dir} = $directoy;
        $data->{Start} = $start;
        $data->{Stop} = $stop;
        # autotimerimport don't support episode, i belief "1" means File = title~subtitle / "0" means File = title
        # $data->{Episode} = $episode; 

        &_insert($data);
        ++$cnt;
    }
    close (AT_FILE);
    print STDERR sprintf("%d Autotimer read from file %s.\n",$cnt,$AT_FILENAME);

    return 0;
}

# ------------------
# Name:  autotimerexport
# Descr: Read autotimer from database and save this to a export file.
# Usage: autotimerexport($filename);
# ------------------
sub autotimerexport {
    my $AT_FILENAME = shift || die("No filename to export! Use $0 -e 'vdradmind.at'\n");

    die sprintf("File to store autotimer '%s' already exist! The file can't be overwritten. Please remove the file manually.\n",$AT_FILENAME)
        if(-e $AT_FILENAME);

    my $sql = "select Activ, Search, InFields, Channels, Dir, Start, Stop, Priority, Lifetime from AUTOTIMER order by Id";
    my $atlist = $dbh->selectall_arrayref($sql);

    die "No autotimer data are to export available!\n"
        if((!$atlist) || (scalar $atlist <= 0));

    my $cnt = "0";
    open(AT_FILE, ">$AT_FILENAME") || die sprintf("Can't open file '%s' : %s\n",$AT_FILENAME,$!);
    foreach my $data (@$atlist) {
        if($data->[1] ne "" and $data->[2] ne "") {

        my $active = "0";
        my $pattern = $data->[1];
        my $section = "0";
        my $start = "";
        my $stop = "";
        my $episode = "0"; # autotimerexport don't support episode, i belief "1" means File = title~subtitle / "0" means File = title
        my $prio = "";
        my $lft = "";
        my $channel = "";
        my $directoy = "";

        $active = "1" if($data->[0] eq "y");

        $section |= 1 if ($data->[2]=~/^.*title.*$/);
        $section |= 2 if ($data->[2]=~/^.*subtitle.*$/);
        $section |= 4 if ($data->[2]=~/^.*description.*$/);

        if( $data->[3]) {
            foreach my $ch (@{&ChannelArray('Id')}) {
                if ($data->[3] eq $ch->[0]) { # vdradmin suppport only one Channel
                    $channel = $ch->[1];
                    last;
                }
            }
        }

        $directoy = $data->[4] if($data->[4]);
        $start = $data->[5] if($data->[5] and $data->[5] ne $data->[6]); # Exclude if start and stop if same value e.g. 0000
        $stop = $data->[6] if($data->[6] and $data->[5] ne $data->[6]);
        $prio = $data->[7] if($data->[7]);
        $lft = $data->[8] if($data->[8]);

        print AT_FILE sprintf("%s:%s:%s:%s:%s:%s:%s:%s:%s:%s\n",
            $active,$pattern,$section,$start,$stop,$episode,$prio,$lft,$channel,$directoy)
            || die sprintf("Can't write file '%s' : %s\n",$AT_FILENAME,$!);
        ++$cnt;
        }
    }
    close (AT_FILE);
    
    print STDERR sprintf("%d Autotimer write to file %s\n",$cnt,$AT_FILENAME);

    return 0;
}
    

$dbh = DBI->connect("DBI:mysql:database=$db;host=$host;port=$port", $usr, $pwd,{ 
  PrintError => 1,
  AutoCommit => 1,
}) || die $DBI::errstr;

exit 1
  unless($dbh);

exit &autotimerexport($filename)
    if($opt{e});
exit &autotimerimport($filename)
    if(!$opt{e});