movie search script for eggdrop bot using api

movie search script for eggdrop bot using api

Recently another script I used to search for film information got broken. After a quick look around I found the nice people over at OMDb (http://www.omdbapi.com/) who have a good API to expose things like IMDB rating and other information regarding movies. Its a simple script (as usual) but hopefully as useful as more traditional IMDB script with a bit less chance of being broken so often. You can grab the script [here]

Example screenshot of the script in action:
movie-script

Code looks something like this:

#########################################################################################
# Name          m00nie::movie
# Description   Uses omdbapi to grab info about a film title then spam it into the chan
#   
# Version       1.1 - Https links, adding functionality to specify year and some other 
#			    small fixes. 
#               1.0 - Initial release
# Website       https://www.m00nie.com
# Notes         Create ypu own API keys here: http://www.omdbapi.com
# 		requires .chanset #chan +movie
#########################################################################################
namespace eval m00nie {
   namespace eval movie {
    package require json
    bind pub - !movie m00nie::movie::search
    bind pub - !imdb m00nie::movie::search
    variable version "1.1"
    setudef flag movie
    # Setup your own app and keys at the URL above
    variable key "---GET-YOUR-OWN---"

proc search {nick uhost hand chan text} {
    if {[channel get $chan movie]} {
        putlog "m00nie::movie::search is running"

	# First off lets look for a year at the end of the string. If there is one subtract it and set the variable
	set year ""
	if { [regexp {\s+[0-9]{4}$} $text] } {
		regexp {[0-9]{4}$} $text year
		regsub {[0-9]{4}$} $text "" text
		putlog "m00nie::movie::search Year has been specified: $year, text is now: $text"
	} else {
		putlog "m00nie::movie::search Year was NOT specified"
	}

	
	# Format and grab info from site	
	regsub -all {\s+} $text "%20" text
	set response [getinfo "http://www.omdbapi.com/?t=$text&y=$year&apikey=$m00nie::movie::key"]
	
	# Check for errors like movie not found
	if {[dict get $response Response] == "False"} {
		putlog "m00nie::movie::search connected to API ok but got error: [dict get $response Error]"
		puthelp "PRIVMSG $chan :\002Error:\002 [dict get $response Error]"
		return
	}

	# Good/valid response so parse and spam into chan
	foreach var { Title Year Rated Released Runtime Genre Actors Plot Metascore imdbRating imdbID } {
		set $var [dict get $response $var]
	}
	set imdburl "https://www.imdb.com/title/$imdbID/"
	putlog "m00nie::movie::search Title: $Title ($Year) $Rated | $Runtime | $Genre | $Released"
	putlog "m00nie::movie::search Cast: $Actors"
	putlog "m00nie::movie::search Plot: $Plot"
	putlog "m00nie::movie::search Metascore: $Metascore, imdb score: $imdbRating"
	putlog "m00nie::movie::search $imdburl"
	puthelp "PRIVMSG $chan :\002$Title ($Year)\002 $Rated \002|\002 $Runtime \002|\002 $Genre \002|\002 $Released"
	puthelp "PRIVMSG $chan :\002Cast:\002 $Actors"
	puthelp "PRIVMSG $chan :\002Plot:\002 $Plot"
	puthelp "PRIVMSG $chan :\002Metascore:\002 $Metascore, \002IMDB Rating:\002 $imdbRating"
	puthelp "PRIVMSG $chan :$imdburl"
    }
}

proc getinfo { url } {
    for { set i 1 } { $i <= 5 } { incr i } {
            set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
            if {[string length rawpage] > 0} { break }
        }
        putlog "m00nie::movie::getinfo Rawpage length is: [string length $rawpage]"
        if {[string length $rawpage] == 0} { error "omdbapi returned ZERO no data :( or we couldnt connect properly" }
        set ids [dict get [json::json2dict $rawpage]]
    putlog "m00nie::movie::getinfo IDS are $ids"
    return $ids

}
}
}
putlog "m00nie::movie $m00nie::movie::version loaded"

As ever any comment, questions or suggestions are more than welcome

m00nie