From 89dd19a10ba4ee0c2cb469994cf2a5166e5b2722 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 1 Mar 2012 02:20:02 +0100 Subject: prompt before update, --force --- videoadd.pl | 140 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 100 insertions(+), 40 deletions(-) diff --git a/videoadd.pl b/videoadd.pl index bb70614..e5e1971 100755 --- a/videoadd.pl +++ b/videoadd.pl @@ -7,7 +7,7 @@ # See http://sam.zoy.org/wtfpl/COPYING for more details. -$VERSION = "0.2, 18 February 2012"; +$VERSION = "0.3, 01 March 2012"; use Getopt::Long qw/:config noauto_abbrev nogetopt_compat no_ignore_case permute bundling auto_version auto_help/; @@ -55,7 +55,7 @@ videoadd.pl - add movies to your collection =head1 SYNOPSIS -B [B<-s>] [B<-u>] [B<--ignore-db>] [B<--dont-move>] [B<-i> I] +B [B<-s>] [B<-u>] [B<--ignore-db>] [B<--dont-move>] [B<--force>] [B<-i> I] [B<-o> I...] [B<--only=>I...] [B<--exclude=>I...] [B<-q>] (I|I|I<id>|I<IMDBID>) @@ -94,8 +94,9 @@ is set), which in turn is put in the right place of the above hierarchy (unless B<--dont-move> is set). The argument is matched against the file names, titles, IDs and IMDBIDs -of your database. If an entry is found, then the database is merely updated -with the new data, and the hierachy is unchanged. +of the database. If matching entries are found, then the user is +prompted to choose an entry to update. In that case, the hierachy +remains unchanged. =head1 OPTIONS @@ -120,6 +121,12 @@ Do not put the new movie in I<DIRECTORS/director/movie>, with a symlink I<MOVIES/movie> -> I<../DIRECTORS/director/movie>. Note that it may break the sanity of your collection. +=item [B<--force>] + +Do not ask the user before uptading existing entries in the database. If +more that one matching entry is found while [B<--force>] is set, +B<videoadd.pl> exists with a non-zero status. + =item B<-i> I<crit>, B<--imdb=>I<crit> Search for the given criterion (e.g., a movie title or an IMDB ID) on @@ -204,6 +211,7 @@ Copyright 2012 Guilhem Moulin. See the source for copying conditions. my $ignoredb_flag; my $seen_flag; +my $force_update_flag; my $unseen_flag; my $move_flag = 1; my %options; @@ -213,6 +221,7 @@ GetOptions( "s|seen" => \$seen_flag , "u|unseen" => \$unseen_flag , "ignore-db" => \$ignoredb_flag , "dont-move" => sub { undef $move_flag } + , "force" => \$force_update_flag , "i|imdb=s" => sub { $imdb{crit} = $_[1] } , "o|option=s"=> sub { my ($k,$v) = split /=/, $_[1], 2; $options{lc $k} = $v; } @@ -260,7 +269,7 @@ if ( defined ($imdb{crit}) and &runIMDB() ) { do { $movie = new IMDB::Film(%imdb); - die "Error: " .$movie->error. "\n" + die "IMDB: " .$movie->error. "\n" unless $movie->status; my @matches = @{$movie->matched}; @@ -268,18 +277,18 @@ if ( defined ($imdb{crit}) and &runIMDB() ) { # Got several results; Print them, and ask the user to pick up an ID die "No result found.\n" unless defined $matches[0]->{id}; - $imdb{crit} = $matches[0]->{id}; - + print "Found " .(1+$#matches). " matching entries:\n" + or die "Can't print: $!"; + my @options; foreach ( @matches ) { print $_->{id}. ' - ' .$_->{title}. "\n" or die "Can't print: $!"; + push @options, $_->{id}; } for (my $i=0; $i<72; $i++) {print '=' or die "Can't print: $!"}; print "\n" or die "Can't print: $!"; - print "Choose an ID above: [$matches[0]->{id}] " - or die "Can't print: $!"; - if ( <STDIN> =~ /(.+)/ ) { $imdb{crit} = $1 }; + $imdb{crit} = &ask ( \@options, "Choose an ID above:", $options[0] ); } else { $single = 1; @@ -509,17 +518,12 @@ if ( defined ($file) and defined ($move_flag) ) { my $dir = catdir ( $directors, $new{director} ); unless (-d $dir) { print STDERR "Directory `$dir' does not exist. "; - until (-d $dir) { - print STDERR "Should I create it? (Y/n) "; - my $a = lc <STDIN>; - chomp $a; - if ($a eq 'y' or $a eq '') { - mkdir $dir - or die "Error: Cannot mkdir `$dir': $!\n"; - } - elsif ($a eq 'n') { - exit 0; - } + my $answer = &askYN( "Should I create it?", 'y' ); + if ($answer eq 'y') { + mkdir $dir or die "Error: Cannot mkdir `$dir': $!\n"; + } + else { + exit 0; } } @@ -612,7 +616,7 @@ sub getfile { my $crit = $_[0]; return $crit if defined $ignoredb_flag; - my $SELECT = "SELECT id, imdbid, filename FROM $config{prefix}videodata + my $SELECT = "SELECT id, imdbid, filename, title, year FROM $config{prefix}videodata WHERE "; if ($crit =~ /^[0-9]+$/) { $SELECT .= "id = $crit OR imdbid = 'imdb:$crit'"; @@ -624,25 +628,48 @@ sub getfile { $SELECT .= "title LIKE " .$dbh->quote ('%'.$crit.'%'); } - 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]; - 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; + my $res = $dbh->selectall_hashref( $SELECT, 'id' ) + or die "Can't select: $!\n"; + # We have a new file to add to the database + if (scalar (keys $res) > 0) { + my $answer; + if (defined $force_update_flag) { + die "Error: Non-single matching entry found. Try to refine your search.\n" + if scalar (keys $res) > 1; + $answer = $res->{(keys $res)[0]}->{id}; + } + else { + print "Found " .scalar (keys $res). " matching entries:\n" + or die "Can't print: $!"; + my @options; + foreach my $id (keys $res) { + printf "%04d - ", $res->{$id}->{id} or die "Can't printf: $!"; + print $res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n" + or die "Can't print: $!"; + push @options, sprintf ("%04d", $res->{$id}->{id}); + } + print "a - Ignore the matching entries, and add the new movie.\n" + or die "Can't print: $!"; + push @options, 'a'; + for (my $i=0; $i<72; $i++) {print '=' or die "Can't print: $!"}; + print "\n" or die "Can't print: $!"; + + $answer = &ask ( \@options, "Choose an ID above:", $options[0] ); + } + + $answer =~ s/^0+//; + unless ($answer eq 'a') { + $id = $res->{$answer}->{id}; + my $imdbid = $res->{$answer}->{imdbid}; + unless (defined $imdb{crit}) { + warn "Warning: No imdbID found for ID $id. Try `--imdb'.\n" + unless defined $imdbid; + $imdbid =~ s/^imdbid:// if defined $imdbid; + $imdb{crit} = $imdbid; + } + undef $move_flag; + return catfile( $config{videodir}, 'MOVIES', $res->{$answer}->{filename} ); } - undef $move_flag; - - return catfile($config{videodir}, 'MOVIES', $res->[0]->[2]); } # We have a new file to add to the database @@ -706,3 +733,36 @@ sub runMplayer { } return 0; } + +# Ask a question interactively +sub ask { + my ($options, $question, $default, $pdefault) = @_; + my $answer; + + do { + print "\a" or die "Can't print: $!"; # bell + print $question ." [" or die "Can't print: $!"; + if (defined $pdefault) { + print $pdefault or die "Can't print: $!"; + } + else { + print $default or die "Can't print: $!"; + } + print "] " or die "Can't print: $!"; + + $answer = $default; + if ( <STDIN> =~ /(.+)/ ) { $answer = $1 }; + undef $answer unless grep { $answer eq $_ } @$options; + } + until (defined $answer); + + return $answer; +} + +# Ask a Yes/No question +sub askYN { + my ($question, $default) = @_; + my $pdefault = "Yn"; + $pdefault = "yN" if $default eq 'n'; + return &ask ( ['y','n'], $question, $default, $pdefault ); +} -- cgit v1.2.3