Version 3 of Retrieving movie information from IMDB

Updated 2007-08-21 00:01:13 by MJ

MJ - The following application allows you to get some basic information about a movie from IMDB. It can probably be improved upon and is fragile, but it's a start. When searching you can open a browser to the IMDB page (only on windows)

 package require http
 package require tdom
 package require tk

 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)
         set ::url $url
         .bi configure -state normal
 # 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]]
 }

 proc go {url} {
     exec cmd /c start {} $url &
 }

 label .lq -text Query:
 entry .q -textvariable query
 label .lt -text Title:
 entry .t -textvariable title
 label .lr -text Rating:
 entry .r -textvariable rating
 label .lv -text Votes
 entry .v -textvariable votes
 label .ls -text Outline
 entry .s -textvariable outline
 label .lu -text URL
 entry .u -textvariable url
 grid .lq .q -sticky ew
 grid .lt .t -sticky ew
 grid .lr .r -sticky ew
 grid .lv .v -sticky ew
 grid .ls .s -sticky ew
 grid .lu .u -sticky ew
 frame .f1
 grid .f1 -column 1 -sticky ew
 button .f1.bs -text Search -command {imdb $::query}
 button .f1.bi -text "Goto IMDB" -state disable -command {go $::url} 
 grid .f1.bs .f1.bi
 grid columnconfigure . 1 -weight 1
 grid columnconfigure .f1 {0 1} -weight 1