def show_movies_with_condition(args): """Get a list of movies that match the given condition.""" cnx = connection(DATABASE) c = cnx.cursor() movies = Movie.load_all(c) if args.condition == 'oscar_nominated_no_win': movie_list = [ i for i in movies if i.oscars_won == 0 and i.oscar_nominations > 0 ] columns = ['oscar_nominations'] elif args.condition == 'high_awards_win_rate': movie_list = [ i for i in movies if i.awards_won > 0 and i.awards_won >= i.award_nominations ] columns = ['awards_won', 'award_nominations'] elif args.condition == 'high_box_office': movie_list = [ i for i in movies if i.box_office is not None and i.box_office > 100_000_000 ] columns = ['box_office'] result = Result(columns, movie_list) result.display() c.close() cnx.close()
def add_new_movie(args): """Add new movie to the database.""" title_or_imdb_id = replace_underscores(args.movie_identifier) try: omdb = OmdbApiResponse(title_or_imdb_id, args.imdb_id) except URLError: print('Unable to receive data from OMDb API. ' 'Check your internet connection.') else: if omdb.response: cnx = connection(DATABASE) c = cnx.cursor() check_db = Movie.load_by_imdb_id(c, omdb.movie_data['imdbID']) if check_db is None: movie = Movie.create_object_from_omdb_data(omdb.movie_data) m = movie.save(c) if m: print(f'Movie: {movie.title} has been successfully saved ' f'to the database') else: print(f'Movie: {omdb.movie_data["Title"]} already in the ' f'database') cnx.commit() c.close() cnx.close() else: print(f'Movie: {title_or_imdb_id} not found.')
def high_scores(args): """Show high scores for movies in database.""" cnx = connection(DATABASE) c = cnx.cursor() categories = [ 'runtime', 'box_office', 'awards_won', 'award_nominations', 'oscars_won', 'imdb_rating' ] first_column_data = [ 'Runtime (min)', 'Box Office ($)', 'Awards Won', 'Award Nominations', 'Oscars', 'IMDB Rating' ] data = [] for i in range(len(categories)): top_movie = Movie.load_movie_with_max_attribute(c, categories[i]) row_data = [ first_column_data[i], top_movie.title, getattr(top_movie, categories[i]) ] data.append(tuple(row_data)) result = Result(columns=['Movie', 'Value'], movie_list=[]) result.data = data result.display(first_col='CATEGORY') c.close() cnx.close()
def sort_movies(args): """Get all movies and sort them by the given attribute(s).""" cnx = connection(DATABASE) c = cnx.cursor() movies = Movie.load_all(c, args.order, *args.column) result = Result(args.column, movies) result.display() c.close() cnx.close()
def filter_by_parameter(args): """Get a list of movies filter by the given parameter.""" cnx = connection(DATABASE) c = cnx.cursor() column = args.parameter if column == 'actor': column += 's' value = replace_underscores(args.value) movies = Movie.load_with_filter(c, column, value) result = Result([column], movies) result.display() c.close() cnx.close()
def movie_details(args): """Show all information about a movie.""" cnx = connection(DATABASE) c = cnx.cursor() movie_title_or_id = replace_underscores(args.movie_identifier) if args.imdb_id: movie = Movie.load_by_imdb_id(c, movie_title_or_id) else: movie = Movie.load_by_title(c, movie_title_or_id) movie_data = vars(movie) columns = list(movie_data.keys()) columns.remove('_Movie__id') result = Result(columns=columns, movie_list=[movie]) result.display_single_movie() c.close() cnx.close()
def compare_movies(args): """Compare two movies by the given attribute.""" cnx = connection(DATABASE) c = cnx.cursor() movies_to_compare = [] attribute = args.category for title in args.movie_title: movie = Movie.load_by_title(c, replace_underscores(title)) if movie is not None: movies_to_compare.append(movie) else: print(f'Movie not found: {title}') if len(movies_to_compare) == 2: try: movie = max(movies_to_compare, key=attrgetter(attribute)) except TypeError: print('No necessary data for at least one of the movies') else: result = Result([attribute], [movie]) result.display() c.close() cnx.close()