import mechanize,urllib,sys,os
br=mechanize.Browser()
br.set_handle_robots(False)
s=sys.argv[1]
q=s.split(" ")
link="http://www.imdb.com/find?ref_=nv_sr_fn&q="
for i in q:
    link+=i
    link+="+"
link+="&s=tt"

br.open(link)
count=0
g=br.response().read()
imdb=None
name=None
year=None
f=open("temp_movie","w")
for i in br.links():
    count+=1
if count==121:
    print f.write("No matches Found\n")
else:
    try:
	br.follow_link(nr=69)
	imdb=br.response().read()
	name=imdb.split("Title:")[1]
	name=name.split("</strong>")[0].split("<strong>")[1]
	#print "Name of the movie:"+name
	year=imdb.split("Title:")[1].split("</strong>")[1].split("</p>")[0].split('(')[1].split(')')[0]
	#print "Name of the movie:"+name+" ("+year+")"
	imdbRating=imdb.split('<strong><span itemprop="ratingValue">')[1].split('</span></strong><span class="mellow">')[0]
	f.write("Eye am dee bee RATING is : "+imdbRating+" out of 10\n") #Just to improve the pronounciationn.
	synopsis=imdb.split('<p itemprop="description">')[1].split("</p>")[0]
	f.write(synopsis.strip()+"\n")
    except Exception as e:
	f.write("No matches found.\n")

f.close()

os.system('cat temp_movie | espeak -s 150 -w temp_movie.wav')
os.system('omxplayer temp_movie.wav')
os.system("rm temp_movie.wav && rm temp_movie")
