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()

		logThis("Picking a movie")
		movs = open("resources/movies.txt", "r")
		movlist = movs.read().split("\n")
		mov = random.choice(movlist)
		movs.close()

		logThis("Searching for "+mov)
		s_result = ia.search_movie(mov)

		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']+", "

		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...")
		return("error","","","")
