import imdb
import random

from funcs import logThis

# Picks a random movie and returns information about it
def movieFunc():
	try:
		logThis("Creating IMDb object")
		ia = imdb.IMDb()

		try:
			logThis("Picking a movie")
			movs = open("resources/movies.txt", "r")
			movlist = movs.read().split("\n")
			mov = random.choice(movlist)
			movs.close()
		except:
			logThis("Problem picking the movie (error code 801)")
			return("error","804","","")

		try:
			logThis("Searching for "+mov)
			s_result = ia.search_movie(mov)
		except:
			logThis("Couldn't find on imdb (error code 802)")
			return("error","802","","")

		try:
			logThis("Getting the data")
			movie = s_result[0]
			ia.update(movie)
			cast = movie['cast']
			pcast = ""
			for x in range(3):
				if cast[x]:
					pcast += cast[x]['name']+", "
		except:
			logThis("Couldn't extract data (error code 803)")
			return("error","803","","")

		logThis("Successfully ran !movie")
		return(movie['title'], movie['plot'][0].split("::")[0], movie['cover url'].replace("150","600").replace("101","404"), pcast[:-2])
	except:
		logThis("Something bad happened... (error code 801)")
		return("error","801","","")
