SHARE
TWEET

Untitled

a guest Nov 21st, 2011 2,132 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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_quote = \&DBD::_::db::quote;
  34. my $fields = join(',', map { $sql_quote->( $data->{'movie'}->{$_} || '' } @fields;
  35. my $sql = "INSERT INTO movie_collection VALUES ( NULL, $fields, @{[ time() ]} );"
  36.  
  37. print $sql . "\n";
  38.  
  39.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top