Guest User

Untitled

a guest
Nov 21st, 2011
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.26 KB | None | 0 0
  1. #!perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use DBI;             # SQL quoting hack
  7. use XML::Simple;     # Easy XML handling
  8. use URI::QueryParam; # URL mangling
  9. use LWP::UserAgent;  # Provides the interface to the web
  10.  
  11. my $movie_name = $ARGV[0] ||
  12.     die "Please provide the movie name as the first argument";
  13.  
  14. # Build up our request URI
  15. my $uri = URI->new( 'http://www.imdbapi.com' );
  16. $uri->query_param( r => 'XML' );
  17. $uri->query_param( t => $movie_name );
  18.  
  19. # Get the data
  20. my $response = LWP::UserAgent->new->get( $uri );
  21. die "Couldn't get [$uri]" unless $response->is_success;
  22.  
  23. # This is the magical list of fields specified in the original, and apparently
  24. # the order in which we need to insert in to the table?
  25. my @fields = qw/
  26.     title year rated released genre director writer actors plot poster runtime
  27.     rating votes imdb
  28. /;
  29.  
  30. # Parse the XML we received moments ago
  31. my $data = XML::Simple->new->XMLin( $response->content );
  32.  
  33. my $sql = "INSERT INTO movie_collection VALUES ( NULL, " .
  34.     # Take all of the fields. For each, look it up in the XML data structure,
  35.     # and then quote it properly for a SQL command
  36.     ( join ',', map {
  37.         DBD::_::db->quote( $data->{'movie'}->{$_} || '' )
  38.     } @fields ) .
  39.  
  40.     # Throw in the timestamp
  41.     time() . ');';
  42.  
  43. print $sql . "\n";
  44.  
Advertisement
Add Comment
Please, Sign In to add comment