summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-02-18 16:35:44 +0100
committerGuilhem Moulin <guilhem.moulin@ens-lyon.org>2012-02-18 16:35:44 +0100
commit7c9349b8bd161be2b1cb2b6942f8e46cd196a7f7 (patch)
tree8349ed61f063454fa7ef1aed8be1996e4507ae67
parentd276c7f789011397e3d8dd4a52188d8b6d2d56d8 (diff)
update
-rwxr-xr-xvideoadd.pl147
1 files changed, 114 insertions, 33 deletions
diff --git a/videoadd.pl b/videoadd.pl
index 99add62..36ebf85 100755
--- a/videoadd.pl
+++ b/videoadd.pl
@@ -162,14 +162,20 @@ Copyright 2012 Guilhem Moulin. See the source for copying conditions.
my $ignoredb_flag;
my $seen_flag;
+my $unseen_flag;
my $sort_flag = 1;
my %options;
+my @only;
+my @exclude;
GetOptions( "s|seen" => \$seen_flag
+ , "u|unseen" => \$unseen_flag
, "ignore-db" => \$ignoredb_flag
, "dont-sort" => sub { undef $sort_flag }
, "i|imdb=s" => sub { $imdb{crit} = $_[1] }
, "o=s" => sub { my ($k,$v) = split /=/, $_[1], 2;
$options{lc $k} = $v; }
+ , "only=s" => sub { @only = &keywords (split /,/, $_[1]) }
+ , "exclude=s" => sub { @exclude = &keywords (split /,/, $_[1]) }
, "q|quiet=s" => sub { open LOG, '>', '/dev/null'
or die "Cannot open `/dev/null': $!" }
, "man" => sub { pod2usage(-exitstatus => 0, -verbose => 2) }
@@ -196,16 +202,15 @@ unless ( defined $ignoredb_flag ) {
}
my $id;
-my $imdbid;
my $file = &getfile ($ARGV[0]);
my %new = ( owner_id => $config{userid}
, customs => {});
-exit 0;
################################################################################
# Look on-line for information on the movie
+# TODO: don't look on-line if it's a db update with eg, only=video_codec
if (defined $imdb{crit}) {
# Look up the title/ID on IMDB
@@ -271,7 +276,7 @@ elsif (defined $file) {
# Override imdDB's information with the ones provided
foreach my $k (keys %options) {
- die "Error: Key `" .$k. "' is not allowed.\n"
+ die "Error: Unknown key `" .$k. "'.\n"
unless grep { $_ eq $k }
qw/title language imdbid year imgurl genres
director country plot rating istv/;
@@ -283,6 +288,7 @@ foreach my $k (keys %options) {
################################################################################
# Run mplayer on the given file to get A/V codecs, etc.
+# TODO: don't run mplayer if it's a db update with eg, only=title
if ( defined($file) ) {
$new{filename} = basename ($file);
$new{filesize} = (stat $file)[7];
@@ -351,7 +357,7 @@ unless ( defined $ignoredb_flag ) {
# Get the (first found) mediatype for files on the hard disk
my $mediatype = $dbh->selectrow_arrayref( "SELECT id FROM $config{prefix}mediatypes
WHERE name = ?", undef, 'HDD' )
- or die "Can't select: $!\n";
+ or die "Can't select. (Unknown mediatype `HDD'?)\n";
# Get the genre IDs
my @genres;
@@ -379,51 +385,84 @@ unless ( defined $ignoredb_flag ) {
}
delete $new{genres};
- my $INSERT = "INSERT INTO $config{prefix}videodata
- SET created = NOW()";
- if (defined $new{filedate}) {
- $INSERT .= ", filedate = FROM_UNIXTIME($new{filedate})";
+ my @SET;
+ if (&include('filedate') && defined $new{filedate}) {
+ push @SET, "filedate = FROM_UNIXTIME($new{filedate})";
delete $new{filedate};
}
- $INSERT .= ", mediatype = $mediatype->[0]"
+ push @SET, "mediatype = $mediatype->[0]"
if defined ($mediatype) and defined ($mediatype->[0]);
while (my ($k,$v) = each %new) {
- $INSERT .= ", " .$k. " = " .$dbh->quote ($v) if defined $v;
+ next unless &include ($k);
+ push @SET, $k. " = " .$dbh->quote ($v) if defined $v;
+ }
+
+ my $ok = 1;
+ if (defined $id) {
+ # We got an already existing movie
+ push @SET, "lastupdate = NOW()";
+ $dbh->do( "UPDATE $config{prefix}videodata
+ SET " .join (', ', @SET). "
+ WHERE id = $id" )
+ or die "Can't update: $!\n";
+
+ if (&include ('genres')) {
+ $dbh->do( "DELETE FROM $config{prefix}videogenre
+ WHERE video_id = $id" )
+ or die "Can't delete: $!\n";
+ }
+ if (defined $unseen_flag) {
+ $dbh->do( "DELETE FROM $config{prefix}userseen
+ WHERE video_id = $id
+ AND user_id = $config{userid}" )
+ or die "Can't delete: $!\n";
+ }
}
- $dbh->do($INSERT) or die "Can't insert: $!\n";
+ else {
+ # We got a fresh movie to insert
+ push @SET, "created = NOW()";
+ $dbh->do( "INSERT INTO $config{prefix}videodata
+ SET " .join (', ', @SET) )
+ or die "Can't insert: $!\n";
+
+ my $ids = $dbh->selectall_arrayref ( "SELECT id FROM $config{prefix}videodata
+ WHERE filename = ? ",
+ undef, $new{filename} )
+ or die "Can't select: $!\n";
- my $ids = $dbh->selectall_arrayref ( "SELECT id FROM $config{prefix}videodata
- WHERE filename = ? ",
- undef, $new{filename} )
- or die "Can't select: $!\n";
-
- # Retrive the freshly added ID, and update the userseen & videogenre
- # tables
- if ($#$ids == 0) {
- my $id = $ids->[0]->[0];
- if ($seen_flag) {
+ if ($#$ids == 0) {
+ $id = $ids->[0]->[0];
+ }
+ else {
+ $ok = 0;
+ warn "Warning: Something weird happened during the INSERT. You should probably check the sanity of your collection.\n";
+ }
+ }
+
+ if ($ok) {
+ # Update the userseen & videogenre tables
+ if (defined $seen_flag) {
$dbh->do( "INSERT INTO $config{prefix}userseen
SET video_id = $id,
user_id = $config{userid}" )
or die "Can't insert: $!\n";
}
- my $sth_insgenre = $dbh->prepare( "INSERT INTO $config{prefix}videogenre
- SET video_id = $id,
- genre_id = ?" )
- or die "Error: " .$dbh->errstr;
- foreach (@genres) {
- $sth_insgenre->execute ($_);
- die $sth_insgenre->errstr if $sth_insgenre->err;
+ if (&include ('genres')) {
+ my $sth_insgenre = $dbh->prepare( "INSERT INTO $config{prefix}videogenre
+ SET video_id = $id,
+ genre_id = ?" )
+ or die "Error: " .$dbh->errstr;
+ foreach (@genres) {
+ $sth_insgenre->execute ($_);
+ die $sth_insgenre->errstr if $sth_insgenre->err;
+ }
+ $sth_insgenre->finish;
}
- $sth_insgenre->finish;
print LOG "Check it out! ", $config{url}. "/show.php?id=" .$id, "\n"
or die "Can't print: $!";
}
- else {
- warn "Warning: Something weird happened during the INSERT. You should probably check the sanity of your collection.\n";
- }
$dbh->disconnect;
}
@@ -544,13 +583,21 @@ sub getfile {
my $res = $dbh->selectall_arrayref( $SELECT ) or die "Can't select: $!\n";
+ # TODO: print the list and ask the user to choose instead?
die "Error: Multiple entries found in the database. Try to refine your search.\n"
if $#$res > 0;
if ($#$res == 0) {
$id = $res->[0]->[0];
- $imdbid = $res->[0]->[1];
+ my $imdbid = $res->[0]->[1];
print STDERR "Updating ID $id...\n";
+
+ unless (defined $imdb{crit}) {
+ warn "Warning: No imdbID found for ID $id. Try `--imdb'.\n"
+ unless defined $imdbid;
+ $imdb{crit} = $imdbid;
+ }
+
return catfile($config{videodir}, 'MOVIES', $res->[0]->[2]);
}
@@ -559,3 +606,37 @@ sub getfile {
unless -f $crit;
return $crit;
}
+
+sub keywords {
+ my @ret;
+ foreach my $k (@_) {
+ if (lc $k eq 'imdb') {
+ push @ret, qw/title subtitle language imdbid year imgurl
+ director actors country plot genres rating/;
+ }
+ elsif (lc $k eq 'file') {
+ push @ret, qw/filename filesize filedate
+ video_codec audio_codec video_width video_height
+ runtime language/;
+ }
+ else {
+ die "Error: Unknown key `" .$k. "'.\n"
+ unless grep { $_ eq $k }
+ qw/title subtitle language imdbid year imgurl
+ director actors country plot genres rating
+ filename filesize filedate video_codec
+ audio_codec video_width video_height runtime
+ custom1 custom2 custom3 custom4/;
+ push @ret, lc $k;
+ }
+ }
+ return @ret;
+}
+
+sub include {
+ # Returns 1 if the key should be included, 0 otherwise
+ my $k = $_[0];
+ return 0 if grep {$_ eq $k} @exclude;
+ return 0 if @only and not (grep {$_ eq $k} @only);
+ return 1;
+}