| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Getopt::Long; | ||
| 5 | |||
| 6 | use IMDB::Local; | ||
| 7 | |||
| 8 | my $opt_help; | ||
| 9 | my $opt_imdbDir; | ||
| 10 | my $opt_import; | ||
| 11 | my $opt_quiet=0; | ||
| 12 | my $opt_force=0; | ||
| 13 | my @opt_download; | ||
| 14 | my $opt_optimize=0; | ||
| 15 | |||
| 16 | GetOptions('help' => \$opt_help, | ||
| 17 | 'imdbdir=s' => \$opt_imdbDir, | ||
| 18 | 'import=s' => \$opt_import, | ||
| 19 | 'optimize' => \$opt_optimize, | ||
| 20 | 'quiet' => \$opt_quiet, | ||
| 21 | 'force' => \$opt_force, | ||
| 22 | 'download:s' => \@opt_download | ||
| 23 | ) or usage(0); | ||
| 24 | |||
| 25 | if ( $opt_help ) { | ||
| 26 | die "no usage implemented"; | ||
| 27 | } | ||
| 28 | |||
| 29 | if ( ! defined($opt_imdbDir) ) { | ||
| 30 | die "imdbdir flag is required"; | ||
| 31 | } | ||
| 32 | |||
| 33 | # lets put list files below the imdbDir | ||
| 34 | my $listsDir="$opt_imdbDir/lists"; | ||
| 35 | |||
| 36 | if ( @opt_download ) { | ||
| 37 | use IMDB::Local::Download; | ||
| 38 | |||
| 39 | if ( ! -d $listsDir ) { | ||
| 40 | mkdir($listsDir, 0777) or die "cannot mkdir $listsDir: $!"; | ||
| 41 | } | ||
| 42 | |||
| 43 | my $n=new IMDB::Local::Download('listsDir' => $listsDir, | ||
| 44 | 'verbose' => !$opt_quiet); | ||
| 45 | |||
| 46 | if ( $opt_download[0] eq 'all' ) { | ||
| 47 | # no args give, get everything | ||
| 48 | $n->download($opt_force); | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | for my $type (@opt_download) { | ||
| 52 | if ( !grep(/^$type$/i, $n->listFiles) ) { | ||
| 53 | warn("invalid list file '$type', must be one of ".join(',', $n->listFiles)); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | $n->downloadListFile($type, $opt_force); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( defined($opt_import) ) { | ||
| 63 | my $n=new IMDB::Local('listsDir' => $listsDir, | ||
| 64 | 'imdbDir' => $opt_imdbDir, | ||
| 65 | 'verbose' => !$opt_quiet, | ||
| 66 | 'showProgressBar' => !$opt_quiet); | ||
| 67 | |||
| 68 | if ( $opt_import eq 'all' ) { | ||
| 69 | for my $type ( $n->listTypes() ) { | ||
| 70 | my $ret=$n->importList($type); | ||
| 71 | if ( $ret == 0 ) { | ||
| 72 | if ( $n->{errorCountInLog} == 0 ) { | ||
| 73 | #$n->status("list import $type succeeded with no errors") if ( ! $opt_quiet ); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | $n->status("list import $type succeeded with $n->{errorCountInLog} errors in $n->{imdbDir}/stage-$type.log"); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | elsif ( $n->{errorCountInLog} == 0 ) { | ||
| 80 | #$n->status("list import $type failed (with no logged errors)"); | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | $n->status("list import $type failed with $n->{errorCountInLog} errors in $n->{imdbDir}/stage-$type.log"); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # trigger optimize process | ||
| 88 | $opt_optimize=1; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | my $type=$opt_import; | ||
| 92 | |||
| 93 | my $ret=$n->importList($type); | ||
| 94 | if ( $ret == 0 ) { | ||
| 95 | if ( $n->{errorCountInLog} == 0 ) { | ||
| 96 | $n->status("list import $type succeeded with no errors") if ( ! $opt_quiet ); | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $n->status("list import $type succeeded with $n->{errorCountInLog} errors in $n->{imdbDir}/stage-$type.log"); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | elsif ( $n->{errorCountInLog} == 0 ) { | ||
| 103 | $n->status("list import $type failed (with no logged errors)"); | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | $n->status("list import $type failed with $n->{errorCountInLog} errors in $n->{imdbDir}/stage-$type.log"); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | if ( $opt_optimize ) { | ||
| 112 | my $n=new IMDB::Local('listsDir' => $listsDir, | ||
| 113 | 'imdbDir' => $opt_imdbDir, | ||
| 114 | 'verbose' => !$opt_quiet, | ||
| 115 | 'showProgressBar' => !$opt_quiet); | ||
| 116 | |||
| 117 | $n->status("optimizing IMDB::Local database - $n->{imdbDir}..."); | ||
| 118 | $n->status("Note: Optimizing will take several minutes, please be patient"); | ||
| 119 | $n->optimize(); | ||
| 120 | } | ||
| 121 | |||
| 122 | exit(0); |