Version 0 of Retrieving movie information from IMDB

Updated 2007-08-20 22:51:21 by MJ

MJ - The following proc allows you to get some basic information about a movie from IMDB. It can probably be improved upon, but it's a start.

 package require http
 package require tdom

 proc imdb {input} {
  set ua "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7e"
  http::config -useragent $ua

  # don't manually build query strings http can do it better
  set query [http::formatQuery btnI "I'm Feeling Lucky" q "imdb $input"]
  set http [http::geturl http://www.google.com/search?$query]

  # get HTTP header info
  upvar 0 $http arr
  array set meta $arr(meta)

  # redirection url        
  set url $meta(Location)

  # cleanup
  http::cleanup $http

  set http [http::geturl $url]
  set html [http::data $http]
  http::cleanup $http

  set doc [dom parse -html $html]
  set title [[$doc selectNodes {/html/head/title[1]}] asText]
  set outline [$doc selectNodes {//div[h5="Plot Summary:"]/text()}]
  if {$outline eq {}} {
     set outline [$doc selectNodes {//div[h5="Plot Outline:"]/text()}]        
  }
  set outline [string trim [$outline asText]]
  set rating [[$doc selectNodes {//div[@class="general rating"]/b[2]} ] asText]
  set votes [[$doc selectNodes {//div[@class="general rating"]/small/a[1]} ] asText]
  $doc delete
  set votes [string map {, {}} [lindex [split $votes] 0]]          
  return [list title $title rating $rating votes $votes outline $outline] 
 }