def read_csv_file(self):
     with open(self.__file_name, mode='r', encoding='utf-8-sig') as csvfile:
         movie_file_reader = csv.DictReader(csvfile)
         #index = 0
         genre_dict = {}
         director_dict = {}
         actor_dict = {}
         for row in movie_file_reader:
             movie_id = int(row['id'])
             movie_title = row['Title']
             release_year = int(row['Year'])
             movie = Movie(movie_id, movie_title, release_year)
             self.__movies.add(movie)
             genre = row["Genre"]
             genres_list = genre.split(",")
             for genre_name in genres_list:
                 if genre_name not in genre_dict:
                     genre_dict[genre_name] = [movie]
                 else:
                     genre_dict[genre_name] = genre_dict[genre_name] + [movie]
             des = row['Description']
             movie.set_des(des)
             the_director = row["Director"]
             if the_director not in director_dict:
                 director_dict[the_director] = [movie]
             else:
                 director_dict[the_director] = director_dict[the_director] + [movie]
             actor_list = row["Actors"]
             for actor in actor_list.split(","):
                 actor = actor.strip()
                 if actor not in actor_dict:
                     actor_dict[actor] = [movie]
                 else:
                     actor_dict[actor] = actor_dict[actor] + [movie]
         for genre_name in genre_dict:
             genre = Genre(genre_name)
             for movie in genre_dict[genre_name]:
                 genre.add_movie(movie)
                 movie.add_genre(genre)
             self.__genres.add(genre)
         for director_name in director_dict:
             director = Director(director_name)
             for movie in director_dict[director_name]:
                 director.add_dir_movie(movie)
                 movie.director = director
             self.__directors.add(director)
         for actor_name in actor_dict:
             actor = Actor(actor_name)
             for movie in actor_dict[actor_name]:
                 actor.add_joined_movie(movie)
                 movie.add_actor(actor)
             self.__actors.add(actor)
Пример #2
0
def load_movies_and_genres(data_path: str, repo: MemoryRepository):
    filename = os.path.join(data_path, 'Data1000Movies.csv')
    with open(filename, mode='r', encoding='utf-8-sig') as csvfile:
        movie_file_reader = csv.DictReader(csvfile)
        genres = dict()
        actors = dict()
        directors = dict()
        for row in movie_file_reader:
            movie_id = int(row['id'])
            movie_title = row['Title']
            release_year = int(row['Year'])

            genre = row["Genre"]
            genres_list = genre.split(",")
            for i in genres_list:
                if i not in genres.keys():
                    genres[i] = list()
                genres[i].append(movie_id)

            movie = Movie(movie_id, movie_title, release_year)
            des = row['Description']
            movie.set_des(des)
            repo.add_movie(movie)
            the_director = row["Director"]
            if the_director not in directors:
                directors[the_director] = [movie_id]
            else:
                directors[the_director] = directors[the_director] + [movie_id]
            actor_list = row["Actors"]
            for actor in actor_list.split(","):
                actor = actor.strip()
                if actor not in actors:
                    actors[actor] = [movie_id]
                else:
                    actors[actor] = actors[actor] + [movie_id]

        for director_name in directors:
            director_obj = Director(director_name)
            all_movie = directors[director_name]
            for movie_id in all_movie:
                movie = repo.get_movie_by_id(movie_id)
                movie.director = director_obj
                director_obj.add_dir_movie(movie)
            repo.add_director(director_obj)

        for actor_name in actors:
            actor_obj = Actor(actor_name)
            all_movie = actors[actor_name]
            for movie_id in all_movie:
                movie = repo.get_movie_by_id(movie_id)
                movie.actors.append(actor_obj)
                actor_obj.add_joined_movie(movie)
            repo.add_actor(actor_obj)

        for genre_name in genres.keys():
            genre = Genre(genre_name)
            for movie_id in genres[genre_name]:
                movie = repo.get_movie_by_id(movie_id)
                make_genre_association(movie, genre)
                # movie.add_genre(genre)
                # genre.add_movie(movie)
            repo.add_genre(genre)