def movie_from_imdb_input(imdb_input): directors, writers, actors, genres = [], [], [], [] movie = Movies() imdb_id = imdb_id_from_input(imdb_input) if imdb_id: try: imdb_dict = get_imdb_dict(imdb_id) if imdb_dict.get("Response") == "True": movie.ImdbId = imdb_id if imdb_dict.get("Title"): movie.Title = imdb_dict.get("Title") if imdb_dict.get("Year"): movie.Year = int(imdb_dict.get("Year")) if imdb_dict.get("Runtime") and imdb_dict.get("Runtime") != "N/A": runtime_str = imdb_dict.get("Runtime") # Convert [/d+] min to minutes try: runtime = int(runtime_str[: runtime_str.find(" ")]) except Exception: pass movie.Runtime = str(runtime) if imdb_dict.get("Director"): cur_directors = imdb_dict.get("Director").split(", ") for i in range(len(cur_directors)): directors.append({"name": cur_directors[i]}) if imdb_dict.get("Writer"): cur_writers = imdb_dict.get("Writer").split(", ") for i in range(len(cur_writers)): cur_writer = cur_writers[i] cur_writer = cur_writer[: cur_writer.find("(")] if cur_writer.find("(") >= 0 else cur_writer writers.append({"name": cur_writer}) if imdb_dict.get("Actors"): cur_actors = imdb_dict.get("Actors").split(", ") for i in range(len(cur_actors)): actors.append({"name": cur_actors[i]}) if imdb_dict.get("Genre"): cur_genres = imdb_dict.get("Genre").split(", ") for i in range(len(cur_genres)): genres.append(cur_genres[i]) else: return {"error_msg": "Invalid"} except Exception: return {"error_msg": "IMDb API failed, please try again."} else: return {"error_msg": "Could not be parsed from input."} return {"movie": movie, "directors": directors, "writers": writers, "actors": actors, "genres": genres}
def movie_from_netflix_input(netflix_input): movie = Movies() directors, writers, actors, genres = [], [], [], [] netflix_id = netflix_id_from_input(netflix_input) if netflix_id: try: dom = get_netflix_dom(netflix_id) try: if dom.get("Response") == False: return {"error_msg": "Invalid"} except Exception: pass movie.NetflixId = netflix_id if dom.getElementsByTagName("title") and dom.getElementsByTagName("title")[0]: title = dom.getElementsByTagName("title")[0].getAttribute("regular") if title == "": title = dom.getElementsByTagName("title")[0].getAttribute("short") if dom.getElementsByTagName("release_year") and dom.getElementsByTagName("release_year")[0]: movie.Year = int(dom.getElementsByTagName("release_year")[0].childNodes[0].data) if dom.getElementsByTagName("runtime") and dom.getElementsByTagName("runtime")[0]: runtime = int(dom.getElementsByTagName("runtime")[0].childNodes[0].data) movie.Runtime = int(runtime / 60) for node in dom.getElementsByTagName("link"): if node.getAttribute("title") == "directors" or node.getAttribute("title") == "cast": # Retrive directors and actors by following cast links in dom is_cast = node.getAttribute("title") == "cast" href = node.getAttribute("href") person_dom = get_netflix_dom(None, href) for person in person_dom.getElementsByTagName("person"): if not is_cast: directors.append({"name": person.getElementsByTagName("name")[0].childNodes[0].data}) else: actors.append({"name": person.getElementsByTagName("name")[0].childNodes[0].data}) for node in dom.getElementsByTagName("category"): if node.getAttribute("scheme") == "http://api.netflix.com/categories/genres": genre = node.getAttribute("label") # Only add single word genres if len(genre.split(" ")) == 1: genres.append(genre) except Exception: return {"error_msg": "Netflix API failed, please try again."} else: return {"error_msg": "Could not be parsed from input."} return {"movie": movie, "directors": directors, "writers": writers, "actors": actors, "genres": genres}
def movie_from_rottentomatoes_input(rottentomatoes_input): movie = Movies() directors, writers, actors, genres = [], [], [], [] rottentomatoes_id = rottentomatoes_id_from_input(rottentomatoes_input) if rottentomatoes_id: try: movie_dict = get_rottentomatoes_dict(rottentomatoes_id) try: if movie_dict.get("Response") == False: return {"error_msg": "Invalid"} except Exception: pass movie.RottenTomatoesId = str(movie_dict.get("id")) movie.Title = movie_dict.get("title") movie.Year = movie_dict.get("year") if movie_dict.get("runtime"): runtime_str = str(movie_dict.get("runtime")) if movie_dict.get("abridged_directors"): for j in range(len(movie_dict.get("abridged_directors"))): directors.append({"name": movie_dict.get("abridged_directors")[j].get("name")}) if movie_dict.get("abridged_cast"): for j in range(len(movie_dict.get("abridged_cast"))): actors.append( { "name": movie_dict.get("abridged_cast")[j].get("name"), "id": movie_dict.get("abridged_cast")[j].get("id"), } ) if movie_dict.get("genres"): for j in range(len(movie_dict.get("genres"))): genres.append(movie_dict.get("genres")[j]) except Exception: return {"error_msg": "Rotten Tomatoes API failed, please try again."} else: return {"error_msg": "Could not be parsed from input."} return {"movie": movie, "directors": directors, "writers": writers, "actors": actors, "genres": genres}
def movie_from_inputs(imdb_input, netflix_input, rottentomatoes_input, wikipedia_input): imdb_movie, rottentomatoes_movie, netflix_movie, wikipedia_movie = None, None, None, None directors, writers, actors, genres = [], [], [], [] movie = Movies() error_list = {} success = True if imdb_input and len(imdb_input) > 0: imdb_res = movie_from_imdb_input(imdb_input) if imdb_res.get("movie"): imdb_movie = imdb_res.get("movie") movie.ImdbId = imdb_res.get("movie").ImdbId movie.Title = imdb_res.get("movie").Title movie.Year = imdb_res.get("movie").Year movie.Runtime = imdb_res.get("movie").Runtime directors = imdb_res.get("directors") writers = imdb_res.get("writers") actors = imdb_res.get("actors") genres = imdb_res.get("genres") else: error_list["ImdbId"] = imdb_res.get("error_msg") success = False # Imdb success required for now else: error_list["ImdbId"] = "This field cannot be blank." success = False if netflix_input and len(netflix_input) > 0: netflix_res = movie_from_netflix_input(netflix_input) if netflix_res.get("movie"): netflix_movie = netflix_res.get("movie") movie.NetflixId = netflix_movie.NetflixId else: error_list["NetflixId"] = netflix_res.get("error_msg") success = False # If no netflix input, get netflix movie given imdb movie elif movie.Title and movie.Year: netflix_res = netflix_movie_from_title(movie) if netflix_res.get("movie"): netflix_movie = netflix_res.get("movie") movie.NetflixId = netflix_movie.NetflixId if rottentomatoes_input and len(rottentomatoes_input) > 0: rottentomatoes_res = movie_from_rottentomatoes_input(rottentomatoes_input) if rottentomatoes_res.get("movie"): rottentomatoes_movie = rottentomatoes_res.get("movie") movie.RottenTomatoesId = rottentomatoes_movie.RottenTomatoesId # Use better cast list from rotten tomatoes actors = rottentomatoes_res.get("actors") else: error_list["RottenTomatoesId"] = rottentomatoes_res.get("error_msg") success = False # If no rotten tomatoes input, get rotten tomatoes movie given imdb movie elif movie.Title and movie.Year: rottentomatoes_res = rottentomatoes_movie_from_title(movie) if rottentomatoes_res.get("movie"): rottentomatoes_movie = rottentomatoes_res.get("movie") movie.RottenTomatoesId = rottentomatoes_movie.RottenTomatoesId # Use better cast list from rotten tomatoes actors = rottentomatoes_res.get("actors") # Rotten Tomatoes success required for now else: error_list["RottenTomatoesId"] = "This field cannot be blank." success = False # Use given wikipedia input with no validation, maybe include in future if wikipedia_input and len(wikipedia_input) > 0: wikipedia_res = movie_from_wikipedia_input(wikipedia_input) if wikipedia_res.get("movie"): wikipedia_movie = wikipedia_res.get("movie") movie.WikipediaId = wikipedia_movie.WikipediaId else: error_list["WikipediaId"] = wikipedia_res.get("error_msg") success = False elif movie.Title and movie.Year: wikipedia_res = wikipedia_movie_from_title(movie) if wikipedia_res.get("movie"): wikipedia_movie = wikipedia_res.get("movie") movie.WikipediaId = wikipedia_movie.WikipediaId return { "success": success, "movie": movie, "directors": directors, "writers": writers, "actors": actors, "genres": genres, "error_list": error_list, }