def get_related_movie(a_class, name, repo: AbstractRepository):
    if a_class == 'director':
        movies = repo.get_movies_by_director(name)
    else:
        movies = repo.get_movies_by_actor(name)

    return movies
def search(search_text, repo: AbstractRepository):
    result_dict = {}
    search_text = search_text.strip().lower()
    count = 0
    #find related movie
    for movie in repo.get_movies():
        if search_text in movie.title.lower():
            count += 1
            if "Movie" not in result_dict:
                result_dict["Movie"] = [movie]
            else:
                result_dict["Movie"].append(movie)
    #find related actor
    for actor in repo.get_actors():
        if search_text in actor.actor_full_name.lower():
            count += 1
            if "Actor" not in result_dict:
                result_dict["Actor"] = [actor]
            else:
                result_dict["Actor"].append(actor)
    #find related director
    for director in repo.get_directors():
        if search_text in director.director_full_name.lower():
            count += 1
            if "Director" not in result_dict:
                result_dict["Director"] = [director]
            else:
                result_dict["Director"].append(director)
    return result_dict, count
def get_10_movie(condition, repo: AbstractRepository):
    movies = []
    #generate 10 random movies
    if condition == 'random':
        random_rank_list = random.sample(
            range(12,
                  repo.get_number_of_movies() + 1), 10)
        for rank in random_rank_list:
            movies.append(repo.get_movie(rank))

    #all genres
    else:
        all_genre_movies = []
        for genre in condition:
            all_genre_movies += [
                movie for movie in repo.get_movies_by_genre(genre.genre_name)
                if movie not in all_genre_movies
            ]
        if len(all_genre_movies) >= 10:
            random_index_list = random.sample(range(len(all_genre_movies)), 10)
        else:  #less than 10 movies among all the genres
            random_index_list = random.sample(range(len(all_genre_movies)),
                                              len(all_genre_movies))
        for index in random_index_list:
            movies.append(all_genre_movies[index])

    if movies is []:
        raise NonExistentMovieException

    return movies
Exemple #4
0
def get_reviews_for_movie(movie_rank, repo: AbstractRepository):
    movie = repo.get_movie(movie_rank)
    if movie is None:
        raise NonExistentMovieException
    reviews = repo.get_reviews()
    review_list = []
    for review in reviews:
        if review.movie == movie:
            review_list.append(review)
    return reviews_to_dict(review_list)
def get_random_movies(quantity, repo:AbstractRepository):
    movie_count = repo.get_total_number_of_movies_in_repo()

    if quantity >= movie_count:
        # Reduce the quantity of movie ids to generate if the repository has an insufficient number of movies
        quantity = movie_count - 1

    # Pick distinct and random movie
    random_ids = random.sample(range(1, movie_count), quantity)
    movies = repo.get_movies_by_index(random_ids)

    return movies_to_dict(movies)
Exemple #6
0
def add_user(username: str, password: str, repo: AbstractRepository):
    # Check that the given username is available.
    user = repo.get_user(username)
    if user is not None:
        raise NameNotUniqueException

    # Encrypt password so that the database doesn't store passwords 'in the clear'.
    password_hash = generate_password_hash(password)

    # Create and store the new User, with password encrypted.
    user = User(username, password_hash)
    repo.add_user(user)
Exemple #7
0
def get_random_movies(quantity, repo: AbstractRepository):
    movie_count = repo.get_number_of_movies()

    if quantity >= movie_count:
        # Reduce quantity of ranks to generate if repo has insufficient number of movies.
        quantity = movie_count - 1

    # Pick distinct and random movies.
    random_ranks = random.sample(range(1, movie_count), quantity)
    movies = repo.get_movies_by_rank(random_ranks)

    return movies_to_dict(movies)
Exemple #8
0
def get_movies_by_release_year(year, repo: AbstractRepository):
    movies = repo.get_movies_by_release_year(target_year=year)

    movies_dto = list()
    prev_year = next_year = None
    if len(movies) > 0:
        prev_year = repo.get_release_year_of_previous_movie(movies[0])
        next_year = repo.get_release_year_of_next_movie(movies[0])

        # Convert Movies to dictionary form.
        movies_dto = movies_to_dict(movies)

    return movies_dto, prev_year, next_year
Exemple #9
0
def add_review(rank: int, review_txt: str, rating: int, username: str,
               repo: AbstractRepository):
    # Check that the movie exists.
    movie = repo.get_movie(rank)
    if movie is None:
        raise NonExistentMovieException
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create review.
    review = Review(movie, review_txt, rating)

    # Update the repository.
    repo.add_review(review)
Exemple #10
0
def search_movie_by_actor_and_director(actor_fullname: str, director_fullname: str, repo: AbstractRepository):
    movies = repo.search_movies_by_actor_and_director(actor_fullname, director_fullname)
    if len(movies) == 0:
        raise NoSearchResultsException

    movies_as_dict = movies_to_dict(movies)
    return movies_as_dict
Exemple #11
0
def get_movie(movie_id: int, repo: AbstractRepository):
    movie = repo.get_movie_by_index(movie_id)

    if movie is None:
        raise NonExistentMovieException

    return movie_to_dict(movie)
Exemple #12
0
def get_movies_by_id(id_list, repo: AbstractRepository):
    movies = repo.get_movies_by_index(id_list)

    # Convert Movies to dictionary form
    movies_as_dict = movies_to_dict(movies)

    return movies_as_dict
Exemple #13
0
def add_review(review_text: str, username: str, movie_id: int, rating: int, repo: AbstractRepository):
    # Check that the movie exists.
    movie = repo.get_movie_by_index(movie_id)
    if movie is None:
        raise NonExistentMovieException

    # Check that the user exists
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create review
    review = make_review(review_text=review_text, user=user, movie=movie, rating=rating)

    # Update the repository
    repo.add_review(review)
Exemple #14
0
def search_movie_by_title(title:str, repo:AbstractRepository):
    movies = repo.search_movie_by_title(title)
    if len(movies) == 0:
        raise NoSearchResultsException

    movies_as_dict = movies_to_dict(movies)
    return movies_as_dict
Exemple #15
0
def get_reviews_for_movie(movie_id, repo: AbstractRepository):
    movie = repo.get_movie_by_index(movie_id)

    if movie is None:
        raise NonExistentMovieException

    return reviews_to_dict(movie.reviews)
Exemple #16
0
def get_reviews_for_movie(movie_id, repo: AbstractRepository):
    article = repo.get_movie(movie_id)

    if article is None:
        raise NonExistentMovieException

    return reviews_to_dict(article.comments)
Exemple #17
0
def add_review(movie_id: int, review_text: str, username: str,
               repo: AbstractRepository):
    # Check that the movie exists.
    movie = repo.get_movie(movie_id)
    if movie is None:
        raise NonExistentMovieException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create review.
    review = make_review(review_text, user, movie)

    # Update the repository.
    repo.add_review(review)
Exemple #18
0
def get_top_6_movies_by_revenue(repo:AbstractRepository):
    movies = repo.get_top_6_highest_revenue_movies()
    if len(movies) == 0:
        raise NoSearchResultsException

    movies_as_dict = movies_to_dict(movies)
    return movies_as_dict
Exemple #19
0
def search_movie_by_actor_fullname(actor_fullname: str, repo: AbstractRepository):

    movies = repo.get_movies_played_by_an_actor(actor_fullname=actor_fullname)
    if len(movies) == 0:
        raise NonExistentActorException

    movies_as_dict = movies_to_dict(movies)
    return movies_as_dict
Exemple #20
0
def authenticate_user(username: str, password: str, repo: AbstractRepository):
    authenticated = False

    user = repo.get_user(username)
    if user is not None:
        authenticated = check_password_hash(user.password, password)
    if not authenticated:
        raise AuthenticationException
def search_movie_directed_by_director_fullname(director_fullname: str,
                                               repo: AbstractRepository):
    movies = repo.get_movies_directed_by_a_director(
        director_fullname=director_fullname)
    if len(movies) == 0:
        raise NonExistentDirectorException
    movies_as_dict = movies_to_dict(movies)
    return movies_as_dict
def add_review(rate: int, rank: int, review_text: str, username: str,
               repo: AbstractRepository):
    # Check that the article exists.
    movie = repo.get_movie(int(rank))
    print(movie)
    if movie is None:
        raise NonExistentMovieException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(timestamp)
    # Create comment.
    review = make_review(review_text, user, movie, timestamp, rate)
    # Update the repository.
    repo.add_review(review)
def get_all_genres(repo: AbstractRepository):
    genres = []

    for genre in repo.get_genres():
        genres.append(genre)

    if genres is []:
        raise NonExistentGenreException

    return sorted(genres)
def get_all_years(repo: AbstractRepository):
    years = []

    for year in repo.get_years():
        years.append(year)

    if years is []:
        raise NonExistentYearException

    return sorted(years, reverse=True)
def get_11_movie(repo: AbstractRepository):
    movies = []

    for rank in range(1, 12):
        movies.append(repo.get_movie(rank))

    if movies is [] or len(movies) != 11:
        raise NonExistentMovieException

    return movies
def get_all_movie(condition, repo: AbstractRepository):
    movies = []

    if condition[0] != 'All' and condition[1] != 'All':
        movies_genre = repo.get_movies_by_genre(condition[0])
        movies_year = repo.get_movies_by_year(condition[1])
        if len(movies_year) < len(movies_genre):
            for movie in movies_year:
                if movie in movies_genre:
                    movies.append(movie)
        else:
            for movie in movies_genre:
                if movie in movies_year:
                    movies.append(movie)

    elif condition == ['All', 'All']:
        for rank in range(1, repo.get_number_of_movies() + 1):
            movies.append(repo.get_movie(int(rank)))

    else:
        index = condition.index('All')
        if index == 0:
            movies = repo.get_movies_by_year(condition[1])
        else:
            movies = repo.get_movies_by_genre(condition[0])

    if movies == []:
        return 'None'

    return movies
Exemple #27
0
def get_user(username: str, repo: AbstractRepository):
    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    return user_to_dict(user)
Exemple #28
0
def get_movie_ids_for_genre(genre_name: str, repo: AbstractRepository):
    movie_ids = repo.get_movie_indexes_for_genre(genre_name)

    return movie_ids
Exemple #29
0
def get_oldest_movie(repo: AbstractRepository):
    movie = repo.get_oldest_movie()
    return movie_to_dict(movie)
Exemple #30
0
def get_latest_year(repo:AbstractRepository):

    return repo.get_latest_year()