import urllib2
import cookielib
import sys
import json

class smovie():
      title = ''
      imdbId = ''
      #other info

def search_movie(request, titolo):
    result = []
    r = smovie()
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10')]
    titolo = titolo.replace(' ', '+')
    url = "http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=" + titolo     #imdb API
    html = opener.open(url).read()
    d = json.loads(html)
    for x in d['title_popular']:   #you can loop also in title_exact or title_substring for other films
        r = smovie()
        r.title = x['title']  #title
        stringa = x['id']     #imdbid
        #fetch info you are interested in
        s = stringa.replace('t', '')    #imdbid in imdbpy format
        r.imdbId = s
        result.append(r)

    return result
