diff options
author | Guilhem Moulin <guilhem.moulin@ens-lyon.org> | 2012-01-14 13:00:59 +0100 |
---|---|---|
committer | Guilhem Moulin <guilhem.moulin@ens-lyon.org> | 2012-01-14 13:00:59 +0100 |
commit | 414dbdb795210c3caf1e29c95b02da188fd7d155 (patch) | |
tree | 774ecd1c8da3dc26a0d8bf5b7641712dcb4c73fc | |
parent | 1efbaead3ddd09c4e96d27bbf9ecedc657125c50 (diff) |
database stuff
-rwxr-xr-x | videoadd.pl | 88 |
1 files changed, 61 insertions, 27 deletions
diff --git a/videoadd.pl b/videoadd.pl index d6a0ff2..fb978ad 100755 --- a/videoadd.pl +++ b/videoadd.pl @@ -13,16 +13,16 @@ use File::Basename qw /basename/; use File::Spec::Functions; use File::Copy qw /move/; use Env qw /HOME/; -use strict; use Switch qw /Perl6/; +use strict; ################################################################################ # Configuration my $confile = catfile ($HOME, '.videodb.rc'); -die "Can't read `" .$confile. "'\n" unless -f $confile; +die "Error: Can't read `" .$confile. "'.\n" unless -f $confile; my %config = do $confile; -die "Error in `" .$confile. "'\n" if $@ || not %config; +die "Error in `" .$confile. "'.\n" if $@ || not %config; map { exists $config{$_} || die "Error: Missing `${_}'.\n" } qw /videodir driver database hostname user port password videodata @@ -45,16 +45,16 @@ $config{url} =~ s/\/*$//; ################################################################################ -my $seen; -my $search; -my $sort = 1; +my $ignoredb_flag; +my $sort_flag = 1; my %options; GetOptions( "seen" => sub { $options{seen} = 1 } - , "s|search=s"=> \$search + , "s|search=s"=> sub { $imdb{crit} = $_[1] } # , "u|update=s"=> update id/filename , "o=s" => sub { my ($k,$v) = split /=/, $_[1], 2; $options{lc $k} = $v; } - , "dont-sort" => sub { undef $sort } + , "dont-sort" => sub { undef $sort_flag } + , "ignore-db" => \$ignoredb_flag , "q|quiet=s" => sub { open LOG, '>', '/dev/null' or die "Cannot open `/dev/null': $!" } , "man" => sub { pod2usage(-exitstatus => 0, -verbose => 2) } @@ -68,14 +68,26 @@ my %new; ################################################################################ +# Connection to the database + +my $dbh; +unless ( defined $ignoredb_flag ) { + # Connect to database + my $dsn = "DBI:$config{driver}:database=$config{database};host=$config{hostname};port=$config{port}"; + $dbh = DBI->connect($dsn, $config{user}, $config{password}) + or die "Error: Can't connect do database.\n"; + $dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; +} + + +################################################################################ # Look on-line for information on the movie -if (defined $search) { +if (defined $imdb{crit}) { # Look up the title/ID on IMDB my $single; my $movie; - $imdb{crit} = $search; do { # Bug, see https://rt.cpan.org/Public/Bug/Display.html?id=71429 @@ -139,7 +151,7 @@ foreach (keys %options) { ################################################################################ -# Run mplayer on the given file to get A/V drivers, etc. +# Run mplayer on the given file to get A/V codecs, etc. if ( defined($file) ) { $new{filename} = basename ($file); @@ -150,9 +162,8 @@ if ( defined($file) ) { my @cmd = ('mplayer', '-identify', '-ao', 'null', '-vo', 'null', '-frames', '0', $file); - open my $NULL, '+<', '/dev/null' or die "Can't open `/dev/null': $!"; - *NULL = $NULL; - open3 '<&NULL', my $OUT, '>&NULL', @cmd or die "Can't run mplayer"; + open NULL, '+<', '/dev/null' or die "Can't open `/dev/null': $!"; + open3 '<&NULL', my $OUT, ">&NULL", @cmd or die "Can't run mplayer"; my (@alang, @slang); @@ -186,15 +197,47 @@ if ( defined($file) ) { } }; $new{language} = lc join (', ', @alang) - unless $#alang < 0 and defined $search; + unless $#alang < 0 and defined $imdb{crit}; $new{custom1} = lc join (', ', @slang); } ################################################################################ +# Insertion into the database + +unless ( defined $ignoredb_flag ) { + my $INSERT = "INSERT INTO $config{videodata} + SET mediatype = 14"; + if (defined $config{filedate}) { + $INSERT .= ", FROM_UNIXTIME($config{filedate})"; + delete $config{filedate}; + } + + while (my ($k,$v) = each %new) { + $INSERT .= ", " .$k. " = " .$dbh->quote ($v); + } +# $dbh->do($INSERT) or die "Can't insert: $!\n"; + + my $ids = $dbh->selectall_arrayref ( "SELECT id FROM $config{videodata} + WHERE filename = ? ", + undef, $new{filename} ); + if ($#$ids == 0) { + print LOG $config{url}, "/show.php?id=", $ids->[0]->[0], "\n" + or die "Can't print: $!"; + } + else { + warn "Warning: Something weird happened during the INSERT.\n"; + } + + $dbh->disconnect; +} + + + +################################################################################ # Sort the file -if ( defined ($file) and defined ($sort) ) { +if ( defined ($file) and defined ($sort_flag) ) { if (defined $new{director}) { move ( $file, catfile ( $directors, $new{director}, $new{filename} ) ) or warn "Warning: Cannot move file: $!.\n"; @@ -211,19 +254,10 @@ if ( defined ($file) and defined ($sort) ) { ################################################################################ -while (my ($k,$v) = each %new) { - print $k, ": ", $v, "\n" or die "Can't print: $!"; -} - - - -################################################################################ - exit 0; -# Useless, but Perl doesn't see that this filehandle is used more than -# one time -close NULL; # automatically closed by `open3' +# Useless, but Perl doesn't see that this filehandle is used more than once +close NULL; # Automatically closed by `open3' ################################################################################ |