def load_movies(): data_path = os.path.join("movie", "adapters", "data") # print(data_path, '#'*10) moive_list = [] director1 = Director("Joss Whedon") director2 = Director("Anthony Russo") actor1 = Actor("Robert Downey") actor2 = Actor("Chris Evans") genre1 = Genre("fiction") genre2 = Genre("action") for data_row in read_csv_file(os.path.join(data_path, "news_movies.csv")): # print(data_row, '#'*5) movie_key = int(data_row[0]) # Create Moive object. movie = Movie( title=data_row[1], # date=date.fromisoformat(data_row[2]), release_year=data_row[2], # image_hyperlink=data_row[5], id=movie_key, ) # add realted director if movie_key == 1: movie.director = director1 movie.add_actor(actor1) movie.add_actor(actor2) movie.add_genre(genre1) movie.add_genre(genre2) elif movie_key == 2: movie.director = director1 movie.add_actor(actor1) movie.add_actor(actor2) movie.add_genre(genre1) movie.add_genre(genre2) elif movie_key == 3: movie.director = director2 movie.add_actor(actor1) movie.add_actor(actor2) movie.add_genre(genre1) movie.add_genre(genre2) elif movie_key == 4: movie.director = director2 movie.add_actor(actor1) movie.add_actor(actor2) movie.add_genre(genre1) movie.add_genre(genre2) moive_list.append(movie) print(movie, "in MemoryRepository", movie.director) return moive_list
def load_movies_and_ids(data_path: str, repo: MemoryRepository): ids = dict() for row in read_csv_file(os.path.join(data_path, 'moviefile.csv')): movie = Movie(row[1], int(row[6])) movie.id = int(row[0]) movie.description = row[3] movie.runtime_minutes = int(row[7]) director = Director(row[4]) repo.add_director(director) movie.director = director parsed_genres = row[2].split(',') for genre_string in parsed_genres: genre = Genre(genre_string) movie.add_genre(genre) repo.add_genre(genre) parsed_actors = row[5].split(',') for actor_string in parsed_actors: actor = Actor(actor_string) repo.add_actor(actor) movie.add_actor(actor) # Add the Movie to the repository. repo.add_movie(movie) repo.add_movie_index(movie)
def load_all(file_name: str, repo: MemoryRepository): with open(file_name, mode='r', encoding='utf-8-sig') as csvfile: movie_file_reader = csv.DictReader(csvfile) for row in movie_file_reader: movie = Movie(row['Title'], int(row['Year'])) movie.description = row['Description'] movie.runtime_minutes = int(row['Runtime (Minutes)']) director = Director(row['Director']) repo.add_director(director) movie.director = director parsed_genres = row['Genre'].split(',') for genre_string in parsed_genres: genre = Genre(genre_string) repo.add_genre(genre) movie.add_genre(genre) parsed_actors = row['Actors'].split(',') for actor_string in parsed_actors: actor = Actor(actor_string) repo.add_actor(actor) movie.add_actor(actor) repo.add_movie(movie)
def read_csv_file(filename: str, repo: MemoryRepository): with open(filename, encoding='utf-8-sig') as infile: rows = csv.DictReader(infile) for row in rows: title = row['Title'] description = row['Description'] year = int(row['Year']) director = row['Director'] D = Director(director.strip()) genres = row['Genre'].split(',') movie = Movie(title, year) movie.director = D movie.description = description for genre in genres: G = Genre(genre.strip()) repo.add_genre(G) movie.genres.append(G) repo.add_director(D) actors = row['Actors'].split(',') for actor in actors: A = Actor(actor.strip()) repo.add_actor(A) movie.actors.append(A) # runtime = int(row['Runtime (Minutes)']) movie.runtime_minutes = runtime repo.add_movie(movie)
def test_repository_can_retrieve_movie(in_memory_repo): movie = in_memory_repo.get_movie(1) # Check that the Article has the expected title. assert movie.title == 'Guardians of the Galaxy' # Check that the Article is commented as expected. review_one = [review for review in movie.reviews if review.review_text == 'Oh no, COVID-19 has hit New Zealand'][0] review_two = [review for review in movie.reviews if review.review_text == 'Yeah Freddie, bad news'][0] assert review_one.user.user_name == 'fmercury' assert review_two.user.user_name == "thorke" # Check that the Article is tagged as expected. assert movie.is_genred_by(Genre('Action')) assert movie.is_genred_by(Genre('Adventure')) assert movie.is_genred_by(Genre('Sci-Fi')) assert movie.is_acted_by(Actor('Chris Pratt')) assert movie.is_acted_by(Actor('Vin Diesel')) assert movie.is_acted_by(Actor('Bradley Cooper')) assert movie.is_acted_by(Actor('Zoe Saldana'))
def actor(): return Actor('Emma Stone')
def load_movies_and_genres(data_path: str, repo: MemoryRepository): genres = dict() actors = dict() directors = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): movie_key = int(data_row[0]) movie_genres = data_row[2].split(',') movie_actors = data_row[5].split(',') movie_director = data_row[4].strip() for genre in movie_genres: genre = genre.strip() if genre not in genres.keys(): genres[genre] = list() genres[genre].append(movie_key) for actor in movie_actors: actor = actor.strip() if actor not in actors.keys(): actors[actor] = list() actors[actor].append(movie_key) if movie_director not in directors.keys(): directors[movie_director] = list() directors[movie_director].append(movie_key) movie = Movie(title=data_row[1], description=data_row[3], year=data_row[6], runtime_minutes=data_row[7], rating=data_row[8], votes=data_row[9], revenue_millions=data_row[10], meta_score=data_row[11], id=movie_key) repo.add_movie(movie) for genre_name in genres.keys(): genre = Genre(genre_name) for movie_id in genres[genre_name]: movie = repo.get_movie(movie_id) make_genre_association(movie, genre) repo.add_genre(genre) for actor_name in actors.keys(): actor = Actor(actor_name) for movie_id in actors[actor_name]: movie = repo.get_movie(movie_id) make_actor_association(movie, actor) repo.add_actor(actor) for director_name in directors.keys(): director = Director(director_name) for movie_id in directors[director_name]: movie = repo.get_movie(movie_id) make_director_association(movie, director) repo.add_director(director)
def load_movies_and_genres_and_actors_and_directors(data_path: str, repo: MemoryRepository): genres = dict() actors = dict() directors = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): movie_key = int(data_row[0]) number_of_genres = 2 movie_genres = sorted(data_row[number_of_genres].strip().split(',')) number_of_actors = 5 movie_actors = sorted(data_row[number_of_actors].strip().split(',')) number_of_directors = 4 movie_directors = sorted(data_row[number_of_directors].strip().split(',')) # Add any new tags; associate the current article with tags. for genre in movie_genres: if genre not in genres.keys(): genres[genre] = list() genres[genre].append(movie_key) # del data_row[number_of_genres] for actor in movie_actors: if actor not in actors.keys(): actors[actor] = list() actors[actor].append(movie_key) for director in movie_directors: if director not in directors.keys(): directors[director] = list() directors[director].append(movie_key) # Create Article object. movie = Movie( name=data_row[1], year1=int(data_row[6]), rank=int(data_row[0]) ) # Add the Article to the repository. repo.add_movie(movie) # Create Tag objects, associate them with Articles and add them to the repository. for genre_name in genres.keys(): genre = Genre(genre_name) for movie_rank in genres[genre_name]: movie = repo.get_movie(movie_rank) make_genre_association(movie, genre) repo.add_genre(genre) for actor_name in actors.keys(): actor = Actor(actor_name) for movie_rank in actors[actor_name]: movie = repo.get_movie(movie_rank) make_actor_association(movie, actor) repo.add_actor(actor) for director_name in directors.keys(): director = Director(director_name) for movie_rank in directors[director_name]: movie = repo.get_movie(movie_rank) make_director_association(movie, director) repo.add_director(director)
def test_repository_filter_actor_invalid(in_memory_repo): assert len(in_memory_repo.movie_filter_actor(Actor('abc1234'))) == 0
def test_repository_filter_actor(in_memory_repo): assert len(in_memory_repo.movie_filter_actor(Actor('Vin Diesel'))) > 0
def actor(): return Actor('Chris Pratt')
def movie(): mv = Movie("test", 2016) mv.add_actor(Actor('actor1')) mv.director = Director('director1') return mv