Advertisement
Guest User

frater

a guest
Aug 18th, 2009
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 21.46 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #
  4. # This perl script is intended to perform movie data lookups based on
  5. # the popular www.imdb.com website
  6. #
  7. # Author: Tim Harvey (tharvey AT alumni.calpoly DOT edu)
  8. # Modified: Andrei Rjeousski
  9. # Modified: Chris Alexander
  10. # Modified: Michele Tavella
  11. #
  12. # v1.1
  13. # - Added amazon.com covers and improved handling for imdb posters
  14. # v1.2
  15. #     - when searching amazon, try searching for main movie name and if nothing
  16. #       is found, search for informal name
  17. #     - better handling for amazon posters, see if movie title is a substring
  18. #       in the search results returned by amazon
  19. #     - fixed redirects for some movies on impawards
  20. # v1.3
  21. #     - fixed search for low res images (imdb changed the page layout)
  22. #     - added cinemablend poster search
  23. #     - added nexbase poster search
  24. #     - removed amazon.com searching for now
  25. #
  26.  
  27. # changes:
  28. # 4-22-2008:
  29. #   Added AKA fields
  30. # 2-10-2008:
  31. #   Changed default to query. If single result
  32. #    is returned complete the lookup.
  33. #   Added IMDB uid return
  34. # 10-26-2007:
  35. #   Added release date (in ISO 8601 form) to output
  36. # 9-10-2006: Anduin Withers
  37. #   Changed output to utf8
  38.  
  39. use LWP::Simple;
  40. use HTML::Entities;
  41. use URI::Escape;
  42. use Term::ANSIColor;
  43.  
  44. eval "use DateTime::Format::Strptime"; my $has_date_format = $@ ? 0 : 1;
  45.  
  46. use vars qw($opt_h $opt_r $opt_d $opt_i $opt_v $opt_D $opt_P $opt_N);
  47. use Getopt::Std;
  48.  
  49. $title = "IMDB Query";
  50. $version = "v1.3.6";
  51. $author = "Tim Harvey, Andrei Rjeousski, Chris Alexander";
  52.  
  53. my @countries = qw(USA UK Canada Japan);
  54.  
  55. binmode(STDOUT, ":utf8");
  56.  
  57. # display usage
  58. sub usage {
  59.    print "usage: $0 -hdrviPDN <query>\n";
  60.    print "       -h           help\n";
  61.    print "       -d           debug\n";
  62.    print "       -r           dump raw query result data only\n";
  63.    print "       -v           display version\n";
  64.    print "       -i           display info\n";
  65.    print "\n";
  66.    print "       -P <movieid>  get movie poster\n";
  67.    print "       -D <movieid>  get movie data\n";
  68.    print "       -N            return the movieid\n";
  69.    exit(-1);
  70. }
  71.  
  72. # display 1-line of info that describes the version of the program
  73. sub version {
  74.    print "$title ($version) by $author\n"
  75. }
  76.  
  77. # display 1-line of info that can describe the type of query used
  78. sub info {
  79.    print "Performs queries using the www.imdb.com website.\n";
  80. }
  81.  
  82. # display detailed help
  83. sub help {
  84.    version();
  85.    info();
  86.    usage();
  87. }
  88.  
  89. sub trim {
  90.    my ($str) = @_;
  91.    $str =~ s/^\s+//;
  92.    $str =~ s/\s+$//;
  93.    return $str;
  94. }
  95.  
  96. # returns text within 'data' between 'beg' and 'end' matching strings
  97. sub parseBetween {
  98.    my ($data, $beg, $end)=@_; # grab parameters
  99.  
  100.    my $ldata = lc($data);
  101.    my $start = index($ldata, lc($beg)) + length($beg);
  102.    my $finish = index($ldata, lc($end), $start);
  103.    if ($start != (length($beg) -1) && $finish != -1) {
  104.       my $result = substr($data, $start, $finish - $start);
  105.       # return w/ decoded numeric character references
  106.       # (see http://www.w3.org/TR/html4/charset.html#h-5.3.1)
  107.       decode_entities($result);
  108.       return $result;
  109.    }
  110.    return "";
  111. }
  112.  
  113. # get Movie Data
  114. sub getMovieData {
  115.    my ($movieid)=@_; # grab movieid parameter
  116.    if (defined $opt_d) { printf(" looking for movie id: '%s'\n", $movieid);}
  117.  
  118.    my $name_link_pat = qr'<a href="/name/[^"]*">([^<]*)</a>'m;
  119.  
  120.    # get the search results  page
  121.    my $request = "http://www.imdb.com/title/tt" . $movieid . "/";
  122.    if (defined $opt_d) { printf("# request: '%s'\n", $request); }
  123.    my $response = get $request;
  124.    if (defined $opt_r) { printf("%s", $response); }
  125.  
  126.    # parse title and year
  127.    my $year = "";
  128.    my $title = parseBetween($response, "<title>", "</title>");
  129.    if ($title =~ m#(.+) \((\d+).*\)#) # Note some years have a /II after them?
  130.    {
  131.       $title = $1;
  132.       $year = $2;
  133.    }
  134.    elsif ($title =~ m#(.+) \(\?\?\?\?\)#)
  135.    {
  136.       $title = $1;
  137.    }
  138.  
  139.    # parse director
  140.    my $data = parseBetween($response, ">Director:</h5>", "</div>");
  141.    if (!length($data)) {
  142.       $data = parseBetween($response, ">Directors:</h5>", "</div>");
  143.    }
  144.    my $director = join(",", ($data =~ m/$name_link_pat/g));
  145.  
  146.    # parse writer
  147.    # (Note: this takes the 'first' writer, may want to include others)
  148.    $data = parseBetween($response, ">Writers <a href=\"/wga\">(WGA)</a>:</h5>", "</div>");
  149.    if (!length($data)) {
  150.          $data = parseBetween($response, ">Writer:</h5>", "</div>");
  151.    }
  152.    if (!length($data)) {
  153.          $data = parseBetween($response, ">Writers:</h5>", "</div>");
  154.    }
  155.    my $writer = join(",", ($data =~ m/$name_link_pat/g));
  156.  
  157.    # parse release date
  158.    my $releasedate = '';
  159.    if ($has_date_format) {
  160.       my $dtp = new DateTime::Format::Strptime(pattern => '%d %b %Y',
  161.          on_error => 'undef');
  162.       my $dt = $dtp->parse_datetime(parseBetween($response,
  163.             ">Release Date:</h5> ", "<a "));
  164.       if (defined($dt)) {
  165.          $releasedate = $dt->strftime("%F");
  166.       }
  167.    }
  168.  
  169.    # parse plot
  170.    my $plot = parseBetween($response, ">Plot Outline:</h5> ", "</div>");
  171.    if (!$plot) {
  172.       $plot = parseBetween($response, ">Plot Summary:</h5> ", "</div>");
  173.    }
  174.  
  175.    if ($plot) {
  176.       # replace name links in plot (example 0388795)
  177.       $plot =~ s/$name_link_pat/$1/g;
  178.  
  179.       # replace title links
  180.       my $title_link_pat = qr!<a href="/title/[^"]*">([^<]*)</a>!m;
  181.      $plot =~ s/$title_link_pat/$1/g;
  182.  
  183.      # plot ends at first remaining link
  184.      my $plot_end = index($plot, "<a ");
  185.      if ($plot_end != -1) {
  186.         $plot = substr($plot, 0, $plot_end);
  187.      }
  188.      $plot = trim($plot);
  189.   }
  190.  
  191.   # parse user rating
  192.   my $userrating = parseBetween($response, ">User Rating:</b>", "</b>");
  193.   $userrating = parseBetween($userrating, "<b>", "/");
  194.  
  195.   # parse MPAA rating
  196.   my $ratingcountry = "USA";
  197.   my $movierating = trim(parseBetween($response, ">MPAA</a>:</h5>", "</div>"));
  198.   if (!$movierating) {
  199.       $movierating = parseBetween($response, ">Certification:</h5>", "</div>");
  200.       $movierating = parseBetween($movierating, "certificates=$ratingcountry",
  201.                                   "/a>");
  202.       $movierating = parseBetween($movierating, ">", "<");
  203.   }
  204.  
  205.   # parse movie length
  206.   my $rawruntime = trim(parseBetween($response, ">Runtime:</h5>", "</div>"));
  207.   my $runtime = trim(parseBetween($rawruntime, "", " min"));
  208.   for my $country (@countries) {
  209.      last if ($runtime =~ /^-?\d/);
  210.      $runtime = trim(parseBetween($rawruntime, "$country:", " min"));
  211.   }
  212.  
  213.   # parse cast
  214.   #  Note: full cast would be from url:
  215.   #    www.imdb.com/title/<movieid>/fullcredits
  216.   my $cast = "";
  217.   $data = parseBetween($response, "Cast overview, first billed only",
  218.                               "/table>");
  219.   if ($data) {
  220.      $cast = join(',', ($data =~ m/$name_link_pat/g));
  221.   }
  222.  
  223.  
  224.   # parse genres
  225.   my $lgenres = "";
  226.   $data = parseBetween($response, "<h5>Genre:</h5>","</div>");
  227.   if ($data) {
  228.      my $genre_pat = qr'/Sections/Genres/(?:[a-z ]+/)*">([^<]+)<'im;
  229.      $lgenres = join(',', ($data =~ /$genre_pat/g));
  230.   }
  231.  
  232.   # parse countries
  233.   $data = parseBetween($response, "Country:</h5>","</div>");
  234.   my $country_pat = qr'/Sections/Countries/[A-Z]+/">([^<]+)</a>'i;
  235.   my $lcountries = join(",", ($data =~ m/$country_pat/g));
  236.  
  237.   # output fields (these field names must match what MythVideo is looking for)
  238.   print color("blue"),"Title:",color("reset")," $title\n";
  239.   print color("blue"),"Year:",color("reset")," $year\n";
  240.   print color("blue"),"ReleaseDate:",color("reset")," $releasedate\n";
  241.   print color("blue"),"Director:",color("reset")," $director\n";
  242.   print color("blue"),"Plot:",color("reset")," $plot\n";
  243.   print color("blue"),"UserRating:",color("reset")," $userrating\n";
  244.   print color("blue"),"MovieRating:",color("reset")," $movierating\n";
  245.   print color("blue"),"Runtime:",color("reset")," $runtime\n";
  246.   print color("blue"),"Writers:",color("reset")," $writer\n";
  247.   print color("blue"),"Cast:",color("reset")," $cast\n";
  248.   print color("blue"),"Genres:",color("reset")," $lgenres\n";
  249.   print color("blue"),"Countries:",color("reset")," $lcountries\n";
  250.   print color("blue"),"IMDB UID:",color("reset")," $movieid\n";
  251.   print color("blue"),"IMDB URL:",color("reset")," $request\n";
  252.  
  253.   # Michele
  254.   my $aka_request = $request . "releaseinfo#akas";
  255.    print color("red"),"IMDB AKA:",color("reset")," $aka_request\n";
  256.  
  257.    my $aka_response = get $aka_request;
  258.    my $aka_data = parseBetween($aka_response, "Also Known As (AKA)", "/table>");
  259.    my @aka_raw = grep(/td/, split('\n', $aka_data));
  260.    if(@aka_raw % 2 == 0 and @aka_raw > 0) {
  261.       print color("red"),"Also Known As:",color("reset"), "\n";
  262.       for(my $i = 0; $i < @aka_raw; $i += 2) {
  263.          my $aka_country = $aka_raw[$i+1];
  264.          my $aka_title = $aka_raw[$i];
  265.          $aka_country =~ s/<.*?td>//g;
  266.          $aka_title =~ s/<.*?td>//g;
  267.          print color("green"),"  $aka_country:",color("reset")," $aka_title\n";
  268.       }
  269.    } else {
  270.       print "Error: AKAs == " . @aka_raw . "\n";
  271.    }
  272.    # /Michele
  273. }
  274.  
  275. # dump Movie Poster
  276. sub getMoviePoster {
  277.    my ($movieid)=@_; # grab movieid parameter
  278.    if (defined $opt_d) { printf("# looking for movie id: '%s'\n", $movieid);}
  279.  
  280.    # get the search results  page
  281.    my $request = "http://www.imdb.com/title/tt" . $movieid . "/posters";
  282.    if (defined $opt_d) { printf("# request: '%s'\n", $request); }
  283.    my $response = get $request;
  284.    if (defined $opt_r) { printf("%s", $response); }
  285.  
  286.    if (!defined $response) {return;}
  287.  
  288.    my $uri = "";
  289.  
  290.    # look for references to impawards.com posters - they are high quality
  291.    my $site = "http://www.impawards.com";
  292.    my $impsite = parseBetween($response, "<a href=\"".$site, "\">".$site);
  293.  
  294.    # jersey girl fix
  295.    $impsite = parseBetween($response, "<a href=\"http://impawards.com","\">http://impawards.com") if ($impsite eq "");
  296.  
  297.    if ($impsite) {
  298.       $impsite = $site . $impsite;
  299.  
  300.       if (defined $opt_d) { print "# Searching for poster at: ".$impsite."\n"; }
  301.       my $impres = get $impsite;
  302.       if (defined $opt_d) { printf("# got %i bytes\n", length($impres)); }
  303.       if (defined $opt_r) { printf("%s", $impres); }
  304.  
  305.       # making sure it isnt redirect
  306.       $uri = parseBetween($impres, "0;URL=..", "\">");
  307.       if ($uri ne "") {
  308.          if (defined $opt_d) { printf("# processing redirect to %s\n",$uri); }
  309.          # this was redirect
  310.          $impsite = $site . $uri;
  311.          $impres = get $impsite;
  312.       }
  313.  
  314.       # do stuff normally
  315.       $uri = parseBetween($impres, "<img SRC=\"posters/", "\" ALT");
  316.       # uri here is relative... patch it up to make a valid uri
  317.       if (!($uri =~ /http:(.*)/ )) {
  318.          my $path = substr($impsite, 0, rindex($impsite, '/') + 1);
  319.          $uri = $path."posters/".$uri;
  320.       }
  321.       if (defined $opt_d) { print "# found ipmawards poster: $uri\n"; }
  322.    }
  323.  
  324.    # try looking on nexbase
  325.    if ($uri eq "" && $response =~ m/<a href="([^"]*)">([^"]*?)nexbase/i) {
  326.       if ($1 ne "") {
  327.          if (defined $opt_d) { print "# found nexbase poster page: $1 \n"; }
  328.          my $cinres = get $1;
  329.          if (defined $opt_d) { printf("# got %i bytes\n", length($cinres)); }
  330.          if (defined $opt_r) { printf("%s", $cinres); }
  331.  
  332.          if ($cinres =~ m/<a id="photo_url" href="([^"]*?)" ><\/a>/i) {
  333.             if (defined $opt_d) { print "# nexbase url retreived\n"; }
  334.             $uri = $1;
  335.          }
  336.       }
  337.    }
  338.  
  339.    # try looking on cinemablend
  340.    if ($uri eq "" && $response =~ m/<a href="([^"]*)">([^"]*?)cinemablend/i) {
  341.       if ($1 ne "") {
  342.          if (defined $opt_d) { print "# found cinemablend poster page: $1 \n"; }
  343.          my $cinres = get $1;
  344.          if (defined $opt_d) { printf("# got %i bytes\n", length($cinres)); }
  345.          if (defined $opt_r) { printf("%s", $cinres); }
  346.  
  347.          if ($cinres =~ m/<td align=center><img src="([^"]*?)" border=1><\/td>/i) {
  348.             if (defined $opt_d) { print "# cinemablend url retreived\n"; }
  349.             $uri = "http://www.cinemablend.com/".$1;
  350.          }
  351.       }
  352.    }
  353.  
  354.    # if the impawards site attempt didn't give a filename grab it from imdb
  355.    if ($uri eq "") {
  356.        if (defined $opt_d) { print "# looking for imdb posters\n"; }
  357.        my $host = "http://posters.imdb.com/posters/";
  358.  
  359.        $uri = parseBetween($response, $host, "\"><td><td><a href=\"");
  360.        if ($uri ne "") {
  361.            $uri = $host.$uri;
  362.        } else {
  363.           if (defined $opt_d) { print "# no poster found\n"; }
  364.        }
  365.    }
  366.  
  367.  
  368.  
  369.    my @movie_titles;
  370.    my $found_low_res = 0;
  371.    my $k = 0;
  372.  
  373.    # no poster found, take lowres image from imdb
  374.    if ($uri eq "") {
  375.       if (defined $opt_d) { print "# looking for lowres imdb posters\n"; }
  376.       my $host = "http://www.imdb.com/title/tt" . $movieid . "/";
  377.       $response = get $host;
  378.  
  379.       # Better handling for low resolution posters
  380.       #
  381.       if ($response =~ m/<a name="poster".*<img.*src="([^"]*).*<\/a>/ig) {
  382.          if (defined $opt_d) { print "# found low res poster at: $1\n"; }
  383.          $uri = $1;
  384.          $found_low_res = 1;
  385.       } else {
  386.          if (defined $opt_d) { print "# no low res poster found\n"; }
  387.          $uri = "";
  388.       }
  389.  
  390.       if (defined $opt_d) { print "# starting to look for movie title\n"; }
  391.  
  392.       # get main title
  393.       if (defined $opt_d) { print "# Getting possible movie titles:\n"; }
  394.       $movie_titles[$k++] = parseBetween($response, "<title>", "<\/title>");
  395.       if (defined $opt_d) { print "# Title: ".$movie_titles[$k-1]."\n"; }
  396.  
  397.       # now we get all other possible movie titles and store them in the titles array
  398.       while($response =~ m/>([^>^\(]*)([ ]{0,1}\([^\)]*\)[^\(^\)]*[ ]{0,1}){0,1}\(informal title\)/g) {
  399.          $movie_titles[$k++] = trim($1);
  400.          if (defined $opt_d) { print "# Title: ".$movie_titles[$k-1]."\n"; }
  401.       }
  402.  
  403.    }
  404.  
  405.    print "$uri\n";
  406. }
  407.  
  408. # dump Movie list:  1 entry per line, each line as 'movieid:Movie Title'
  409. sub getMovieList {
  410.    my ($filename, $options)=@_; # grab parameters
  411.  
  412.    # If we wanted to inspect the file for any reason we can do that now
  413.  
  414.    #
  415.    # Convert filename into a query string
  416.    # (use same rules that Metadata::guesTitle does)
  417.    my $query = $filename;
  418.    $query = uri_unescape($query);  # in case it was escaped
  419.    # Strip off the file extension
  420.    if (rindex($query, '.') != -1) {
  421.       $query = substr($query, 0, rindex($query, '.'));
  422.    }
  423.    # Strip off anything following '(' - people use this for general comments
  424.    if (rindex($query, '(') != -1) {
  425.       $query = substr($query, 0, rindex($query, '('));
  426.    }
  427.    # Strip off anything following '[' - people use this for general comments
  428.    if (rindex($query, '[') != -1) {
  429.       $query = substr($query, 0, rindex($query, '['));
  430.    }
  431.  
  432.    # IMDB searches do better if any trailing ,The is left off
  433.    $query =~ /(.*), The$/i;
  434.    if ($1) { $query = $1; }
  435.  
  436.    # prepare the url
  437.    $query = uri_escape($query);
  438.    if (!$options) { $options = "" ;}
  439.    if (defined $opt_d) {
  440.       printf("# query: '%s', options: '%s'\n", $query, $options);
  441.    }
  442.  
  443.    # get the search results  page
  444.    #    some known IMDB options are:
  445.    #         type=[fuzy]         looser search
  446.    #         from_year=[int]     limit matches to year (broken at imdb)
  447.    #         to_year=[int]       limit matches to year (broken at imdb)
  448.    #         sort=[smart]        ??
  449.    #         tv=[no|both|only]   limits between tv and movies (broken at imdb)
  450.    #$options = "tt=on;nm=on;mx=20";  # not exactly clear what these options do
  451.    my $request = "http://www.imdb.com/find?q=$query;$options";
  452.    if (defined $opt_d) { printf("# request: '%s'\n", $request); }
  453.    my $response = get $request;
  454.    if (defined $opt_r) {
  455.       print $response;
  456.       exit(0);
  457.    }
  458.  
  459.    # check to see if we got a results page or a movie page
  460.    #    looking for 'add=<movieid>" target=' which only exists
  461.    #    in a movie description page
  462.    my $movienum = parseBetween($response, "add=", "\" ");
  463.    if (!$movienum) {
  464.       $movienum = parseBetween($response, ";add=", "'");
  465.    }
  466.    if ($movienum) {
  467.       if ($movienum !~ m/^[0-9]+$/) {
  468.          if (defined $opt_d) {
  469.             printf("# Error: IMDB movie number ($movienum), isn't.\n");
  470.          }
  471.          exit(0);
  472.       }
  473.  
  474.       if (defined $opt_d) { printf("# redirected to movie page\n"); }
  475.       my $movietitle = parseBetween($response, "<title>", "</title>");
  476.       $movietitle =~ m#(.+) \((\d+)\)#;
  477.       $movietitle = $1;
  478.       print "$movienum:$movietitle\n";
  479.       exit(0);
  480.    }
  481.  
  482.    # extract possible matches
  483.    #    possible matches are grouped in several catagories:
  484.    #        exact, partial, and approximate
  485.    my $popular_results = parseBetween($response, "<b>Popular Titles</b>",
  486.                                               "</table>");
  487.    my $exact_matches = parseBetween($response, "<b>Titles (Exact Matches)</b>",
  488.                                               "</table>");
  489.    my $partial_matches = parseBetween($response, "<b>Titles (Partial Matches)</b>",
  490.                                               "</table>");
  491.    my $approx_matches = parseBetween($response, "<b>Titles (Approx Matches)</b>",
  492.                                                "</table>");
  493.    # parse movie list from matches
  494.    my $beg = "<tr>";
  495.    my $end = "</tr>";
  496.    my $count = 0;
  497.    my @movies;
  498.  
  499.    # single result id storage
  500.    my $single;
  501.  
  502. #   my $data = $exact_matches.$partial_matches;
  503.    my $data = $popular_results.$exact_matches;
  504.    # resort to partial matches if no exact
  505.    if ($data eq "") { $data = $partial_matches; }
  506.    # resort to approximate matches if no exact or partial
  507.    if ($data eq "") { $data = $approx_matches; }
  508.    if ($data eq "") {
  509.       if (defined $opt_d) { printf("# no results\n"); }
  510.       return;
  511.    }
  512.    my $start = index($data, $beg);
  513.    my $finish = index($data, $end, $start);
  514.    my $year;
  515.    my $type;
  516.    my $title;
  517.    while ($start != -1 && $start < length($data)) {
  518.       $start += length($beg);
  519.       my $entry = substr($data, $start, $finish - $start);
  520.       $start = index($data, $beg, $finish + 1);
  521.       $finish = index($data, $end, $start);
  522.  
  523.       my $title = "";
  524.       my $year = "";
  525.       my $type = "";
  526.       my $movienum = "";
  527.  
  528.       # Some titles are identical, IMDB indicates this by appending /I /II to
  529.       # the release year.
  530.       #   e.g. "Mon meilleur ami" 2006/I vs "Mon meilleur ami" 2006/II
  531.       if ($entry =~ m/<a href="\/title\/tt(\d+)\/.*\">(.+)<\/a> \((\d+)\/?[a-z]*\)(?: \((.+)\))?/i) {
  532.           $movienum = $1;
  533.           $title = $2;
  534.           $year = $3;
  535.           $type = $4 if ($4);
  536.       } else {
  537.            if (defined $opt_d) {
  538.                print("Unrecognized entry format ($entry)\n");
  539.            }
  540.            next;
  541.       }
  542.  
  543.       my $skip = 0;
  544.  
  545.       # fix broken 'tv=no' option
  546.       if ($options =~ /tv=no/) {
  547.          if ($type eq "TV") {
  548.             if (defined $opt_d) {printf("# skipping TV program: %s\n", $title);}
  549.             $skip = 1;
  550.          }
  551.       }
  552.       if ($options =~ /tv=only/) {
  553.          if ($type eq "") {
  554.             if (defined $opt_d) {printf("# skipping Movie: %s\n", $title);}
  555.             $skip = 1;
  556.          }
  557.       }
  558.       # fix broken 'from_year=' option
  559.       if ($options =~ /from_year=(\d+)/) {
  560.          if ($year < $1) {
  561.             if (defined $opt_d) {printf("# skipping b/c of yr: %s\n", $title);}
  562.             $skip = 1;
  563.          }
  564.       }
  565.       # fix broken 'to_year=' option
  566.       if ($options =~ /to_year=(\d+)/) {
  567.          if ($year > $1) {
  568.             if (defined $opt_d) {printf("# skipping b/c of yr: %s\n", $title);}
  569.             $skip = 1;
  570.          }
  571.       }
  572.  
  573.       # option to strip out videos (I think that's what '(V)' means anyway?)
  574.       if ($options =~ /video=no/) {
  575.          if ($type eq "V") {
  576.             if (defined $opt_d) {
  577.                 printf("# skipping Video program: %s\n", $title);
  578.             }
  579.             $skip = 1;
  580.          }
  581.       }
  582.  
  583.       # (always) strip out video game's (why does IMDB give these anyway?)
  584.       if ($type eq "VG") {
  585.          if (defined $opt_d) {printf("# skipping videogame: %s\n", $title);}
  586.          $skip = 1;
  587.       }
  588.  
  589.       # add to array
  590.       if (!$skip) {
  591.           my $moviename = $title;
  592.           if ($year ne "") {
  593.               $moviename .= " ($year)";
  594.           }
  595.  
  596. #         $movies[$count++] = $movienum . ":" . $title;
  597.          $movies[$count++] = $movienum . ":" . $moviename;
  598.          $single = $movienum;
  599.       }
  600.    }
  601.  
  602.    # display the singularity results
  603.    if($count == 1 && defined $opt_N) { print "$single\n"; }
  604.    elsif($count == 1) { getMovieData($single); }
  605.    else {
  606.       # display array of values
  607.       for $movie (@movies) { print "$movie\n"; }
  608.    }
  609. }
  610.  
  611. #
  612. # Main Program
  613. #
  614.  
  615. # parse command line arguments
  616. getopts('ohrdivDPN');
  617.  
  618. # print out info
  619. if (defined $opt_v) { version(); exit 1; }
  620. if (defined $opt_i) { info(); exit 1; }
  621.  
  622. # print out usage if needed
  623. if (defined $opt_h || $#ARGV<0) { help(); }
  624.  
  625. if (defined $opt_D) {
  626.    # take movieid from cmdline arg
  627.    $movieid = shift || die "Usage : $0 -D <movieid>\n";
  628.    getMovieData($movieid);
  629. }
  630.  
  631. elsif (defined $opt_P) {
  632.    # take movieid from cmdline arg
  633.    $movieid = shift || die "Usage : $0 -P <movieid>\n";
  634.    getMoviePoster($movieid);
  635. }
  636.  
  637. else{
  638.    # take query from cmdline arg
  639.    $options = shift || die "Usage : $0 <query>\n";
  640.    $query = shift;
  641.    if (!$query) {
  642.       $query = $options;
  643.       $options = "";
  644.    }
  645.    getMovieList($query, $options);
  646. }
  647.  
  648. # vim: set expandtab ts=3 sw=3 :
  649.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement