def movie_from_wikipedia_input(wikipedia_input): movie = Movies() directors, writers, actors, genres = [], [], [], [] wikipedia_id = wikipedia_id_from_input(wikipedia_input) if wikipedia_id: try: wikipedia_dict = get_wikipedia_dict(wikipedia_id) if wikipedia_dict.get("Response") == True: movie.WikipediaId = wikipedia_id else: return {"error_msg": "Invalid"} except Exception: return {"error_msg": "Wikipedia 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_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, }
def view_list(request): try: logged_in_profile_info = { } permission_response = check_and_get_session_info(request, logged_in_profile_info) if permission_response != True: return permission_response if not logged_in_profile_info['id']: set_msg(request, 'Action Failed!', 'You must be logged in to perform that action', 'warning') return redirect('webapp.views.profile.login') type_dict = get_type_dict() if request.GET.get('add') and request.GET.get('i') and request.GET.get('r'): '''***************************************************************************** Add movie given imdb identifier (rotten tomatoes id required as well) and redirect to movie page by way of association functions if success otherwise back to search with errors PATH: webapp.views.movie.view_list; METHOD: none; PARAMS: get - add,i,r; MISC: none; *****************************************************************************''' try: has_error = False error_text = None movie = Movies () # Start movie from imdb id res_dict = movie_from_imdb_input(request.GET.get('i')) if res_dict.get('movie'): movie = res_dict.get('movie') else: has_error = True error_text = 'IMDb ID Validation: ' + res_dict.get('error_msg') raise ValidationError('') # Get wikipedia id if resolved res = wikipedia_movie_from_title(movie) if res.get('movie'): movie.WikipediaId = res.get('movie').WikipediaId # Set rotten tomatoes, and wikipedia id movie.RottenTomatoesId = request.GET.get('r') test = movie_from_rottentomatoes_input(movie.RottenTomatoesId) if test.get('movie'): # Use better cast list from rotten tomatoes res_dict['actors'] = test.get('actors') else: has_error = True error_text = 'Rotten Tomatoes ID Validation: ' + test.get('error_msg') raise ValidationError('') movie.full_clean() movie.save() create_properties(movie, res_dict.get('directors'), res_dict.get('writers'), res_dict.get('actors'), res_dict.get('genres'), logged_in_profile_info['username']) movie_logger.info(movie.UrlTitle + ' Create Success by ' + logged_in_profile_info['username']) # Redirect to 'add association' function and add option 'seen' if already present res = redirect('webapp.views.movie.view', urltitle=movie.UrlTitle) res['Location'] += '?assoc=1&add=1' if request.GET.get('seen'): res['Location'] += '&seen=1' return res except ValidationError as e: urltitle = movie.UrlTitle if movie.UrlTitle else 'Unknown' movie_logger.info(urltitle + ' Create Failure by ' + logged_in_profile_info['username']) error_msg = None if has_error: error_msg = {'Error' : error_text} else: error_msg = e.message_dict for key in error_msg: error_msg[key] = str(error_msg[key][0]) return render_to_response('movie/search.html', {'header' : generate_header_dict(request, 'Search'), 'success' : False, 'results' : error_msg}, RequestContext(request)) elif logged_in_profile_info['admin'] and request.GET.get('add'): if request.method == 'POST': if request.POST.get('api_search_term'): '''***************************************************************************** Return results from imdb, netflix, rottentomatoes, and wikipedia searches as options to add movie PATH: webapp.views.movie.view_list; METHOD: post; PARAMS: get - add, post - api_search_term; MISC: logged_in_profile.IsAdmin; *****************************************************************************''' api_search_term = request.POST.get('api_search_term') api_search_length = int(request.POST.get('api_search_length')) if request.POST.get('api_search_length') and request.POST.get('api_search_length').isdigit() else 5 api_search_length = api_search_length if api_search_length > 0 and api_search_length <= 25 else 5 res_dict = movies_from_apis_term(api_search_term, api_search_length) imdb_possibilities = res_dict.get('imdb_movies') for movie in imdb_possibilities: movie.UrlTitle = imdb_link_for_movie(movie) netflix_possibilities = res_dict.get('netflix_movies') for movie in netflix_possibilities: movie.UrlTitle = netflix_link_for_movie(movie) rottentomatoes_possibilities = res_dict.get('rottentomatoes_movies') for movie in rottentomatoes_possibilities: movie.UrlTitle = rottentomatoes_link_for_movie(movie) wikipedia_possibilities = res_dict.get('wikipedia_movies') for movie in wikipedia_possibilities: movie.UrlTitle = wikipedia_link_for_movie(movie) return render_to_response('movie/add.html', {'header' : generate_header_dict(request, 'Add Movie'), 'api_search_term' : api_search_term, 'api_search_length' : api_search_length, 'imdb_possibilities' : imdb_possibilities, 'netflix_possibilities' : netflix_possibilities, 'rottentomatoes_possibilities' : rottentomatoes_possibilities, 'wikipedia_possibilities' : wikipedia_possibilities}, RequestContext(request)) else: '''***************************************************************************** Add movie given user input of imdb, netflix, rottentomatoes, and wikipedia urls or ids and redirect to movie page PATH: webapp.views.movie.view_list; METHOD: post; PARAMS: get - add; MISC: logged_in_profile.IsAdmin; *****************************************************************************''' res_dict = movie_from_inputs(request.POST.get('imdb_url'), request.POST.get('netflix_url'), request.POST.get('rottentomatoes_id'), request.POST.get('wikipedia_id')) if res_dict.get('success'): try: movie = res_dict.get('movie') movie.full_clean() movie.save() create_properties(movie, res_dict.get('directors'), res_dict.get('writers'), res_dict.get('actors'), res_dict.get('genres'), logged_in_profile_info['username']) movie_logger.info(movie.UrlTitle + ' Create Success by ' + logged_in_profile_info['username']) return redirect('webapp.views.movie.view', urltitle=movie.UrlTitle) except ValidationError as e: urltitle = movie.UrlTitle if movie.UrlTitle else 'Unknown' movie_logger.info(urltitle + ' Create Failure by ' + logged_in_profile_info['username']) # Restore values for add page movie.ImdbId = request.POST.get('imdb_url') movies.RottenTomatoesId = request.POST.get('rottentomatoes_id') movies.NetflixId = request.POST.get('netflix_url') movies.WikipediaId = request.POST.get('wikipedia_id') error_msg = e.message_dict for key in error_msg: error_msg[key] = str(error_msg[key][0]) return render_to_response('movie/add.html', {'header' : generate_header_dict(request, 'Add Movie'), 'error_msg' : error_msg, 'movie' : movie, 'links' : generate_links_dict(movie)}, RequestContext(request)) else: movie_logger.info('Movie Create Failure by ' + logged_in_profile_info['username']) # Restore values for add page movie = res_dict.get('movie') movie.ImdbId = request.POST.get('imdb_url') movie.RottenTomatoesId = request.POST.get('rottentomatoes_id') movie.NetflixId = request.POST.get('netflix_url') movie.WikipediaId = request.POST.get('wikipedia_id') return render_to_response('movie/add.html', {'header' : generate_header_dict(request, 'Add Movie'), 'movie' : movie, 'error_msg' : res_dict.get('error_list'), 'links' : generate_links_dict(movie)}, RequestContext(request)) else: '''***************************************************************************** Display admin add movie page PATH: webapp.views.movie.view_list; METHOD: none; PARAMS: get - add; MISC: logged_in_profile.IsAdmin; *****************************************************************************''' return render_to_response('movie/add.html', {'header' : generate_header_dict(request, 'Add Movie')}, RequestContext(request)) else: '''***************************************************************************** Display movie list page PATH: webapp.views.movie.view_list; METHOD: none; PARAMS: none; MISC: none; *****************************************************************************''' movies, paginated_movies = [], None movie_list = Movies.objects.all().order_by('-Year', 'Title') length = int(request.GET.get('length')) if request.GET.get('length') and request.GET.get('length').isdigit() else 25 length = length if length <= 100 else 100 paginator = Paginator(movie_list, length) page = request.GET.get('page') try: paginated_movies = paginator.page(page) except PageNotAnInteger: paginated_movies = paginator.page(1) except EmptyPage: paginated_movies = paginator.page(paginator.num_pages) # Get all associations with logged in profile to correctly display links (seen later as well) for movie in paginated_movies: try: association = Associations.objects.get(ProfileId = logged_in_profile_info['id'], ConsumeableId = movie, ConsumeableTypeId = type_dict['CONSUMEABLE_MOVIE']) movies.append((movie, True)) except Exception: movies.append((movie, False)) return render_to_response('movie/view_list.html', {'header' : generate_header_dict(request, 'Movie List'), 'movies' : movies, 'page' : paginated_movies}, RequestContext(request)) except Exception: movie_logger.error('Unexpected error: ' + str(sys.exc_info()[0])) return render_to_response('500.html', {'header' : generate_header_dict(request, 'Error')}, RequestContext(request))