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 test_create_object_from_omdb_data(): omdb_data = { 'Title': 'Iron Man 3', 'Year': '2013', 'Runtime': '130 min', 'Genre': 'Action, Adventure, Sci-Fi', 'Director': 'Shane Black', 'Actors': 'Robert Downey Jr., Gwyneth Paltrow, Don Cheadle,' ' Guy Pearce', 'Writer': 'Drew Pearce (screenplay by), Shane Black (screenplay by), ' 'Stan Lee (based on the Marvel comic book by), Don Heck ' '(based on the Marvel comic book by), Larry Lieber (based on' ' the Marvel comic book by), Jack Kirby (based on the Marvel' ' comic book by), Warren Ellis (based on the "Extremis" ' 'mini-series written by), Adi Granov (based on the ' '"Extremis" mini-series illustrated by)', 'Language': 'English', 'Country': 'USA', 'Awards': 'Nominated for 1 Oscar. Another 19 wins & 62 nominations.', 'imdbRating': '7.2', 'imdbVotes': '716,950', 'BoxOffice': '$408,992,272', 'imdbID': 'tt1300854' } test_movie = Movie.create_object_from_omdb_data(omdb_data) assert test_movie.title == 'Iron Man 3' assert test_movie.year == 2013 assert test_movie.runtime == 130 assert test_movie.genre == 'Action, Adventure, Sci-Fi' assert test_movie.director == 'Shane Black' assert test_movie.actors == 'Robert Downey Jr., Gwyneth Paltrow, ' \ 'Don Cheadle, Guy Pearce' assert test_movie.writer.startswith('Drew Pearce (screenplay by)') assert test_movie.writer.endswith('mini-series illustrated by)') assert test_movie.language == 'English' assert test_movie.country == 'USA' assert test_movie.oscars_won == 0 assert test_movie.oscar_nominations == 1 assert test_movie.awards_won == 19 assert test_movie.award_nominations == 62 assert test_movie.imdb_rating == 7.2 assert test_movie.imdb_votes == 716_950 assert test_movie.box_office == 408_992_272 assert test_movie.imdb_id == 'tt1300854'