This code gives me info of a movie and the movies directed/acted by corresponding director and actors. It is working as the way I expected but I included lots of try
and except
statements and as a result my code is slow. Is there anyway to speed this up?
from imdb import IMDb
def rating(name):
p1 = ia.get_movie(ia.search_movie(str(name))[0].getID())
return p1
ia = IMDb()
a = ['Gravity', 'Argo']
i = 0
for i in a:
movie_list = ia.search_movie(i)
# print movie_list
k = movie_list[0].getID()
movie = ia.get_movie(k)
movie_rating = movie['rating']
print "Name of the movie: ", movie
for i in movie['director']:
print "Director: ", i
director = ia.search_person(i["name"])[0]
ia.update(director)
print "Movies directed by %s:" % director
for movie_name in director["director movie"]:
p3 = rating(movie_name)
try:
p3['rating']
print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
except:
print movie_name, "unrated", "\t", p3['year'], "\t", p3['genre'][0]
actor = movie['cast']
print "Cast: "
for ie in actor[:2]:
print "\t", ie
for j in ia.search_person(str(ie))[:1]:
full_person = ia.get_person(j.getID(), info=["filmography"])
try:
# b = full_person["actor"]
bb = full_person["actor"]
except:
bb = full_person["actress"]
print "Movies Acted by %s:" % j
for movie_name in bb:
p3 = rating(movie_name)
try:
p3['rating']
try:
p3['year']
try:
p3['genre']
print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
except:
print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", "no genre"
except:
try:
p3['genre']
print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", p3['genre'][0]
except:
print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", "no genre"
except:
try:
p3['year']
try:
p3['genre']
print movie_name, "\t", "No rating", "\t", p3['year'], "\t", p3['genre'][0]
except:
print movie_name, "\t", "No rating", "\t", p3['year'], "\t", "no genre"
except:
try:
p3['genre']
print movie_name, "\t", "No rating", "\t", "No release year", "\t", p3['genre'][0]
except:
print movie_name, "\t", "No rating", "\t", "No release year", "\t", "no genre"
else:
print "Movies Acted by %s:" % j
for movie_name in bb:
p3 = rating(movie_name)
try:
p3['rating']
try:
p3['year']
try:
p3['genre']
print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", p3['genre'][0]
except:
print movie_name, "\t", p3['rating'], "\t", p3['year'], "\t", "no genre"
except:
try:
p3['genre']
print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", p3['genre'][0]
except:
print movie_name, "\t", p3['rating'], "\t", "No release year", "\t", "no genre"
except:
try:
p3['year']
try:
p3['genre']
print movie_name, "\t", "No rating", "\t", p3['year'], "\t", p3['genre'][0]
except:
print movie_name, "\t", "No rating", "\t", p3['year'], "\t", "no genre"
except:
try:
p3['genre']
print movie_name, "\t", "No rating", "\t", "No release year", "\t", p3['genre'][0]
except:
print movie_name, "\t", "No rating", "\t", "No release year", "\t", "no genre"