Esempio n. 1
0
    def __init__(self):
        self.movie_metadata = dict()
        for index, row in df_movies.iterrows():
            try:
                with open(DATA_PATH_JSON + str(row[MOVIE_ID]) + '.json',
                          encoding="UTF-8") as json_file:
                    data = json.load(json_file)
                    directors = set()
                    languages = set()
                    actors = set()
                    genres = set()
                    keywords = set()
                    if IMDB_COL in data:
                        imdb = data[IMDB_COL]
                        Recommender.add_to_collection(imdb[DIRECTORS_COL],
                                                      directors)
                        languages.add(imdb['originalLanguage'])
                        actor_str = imdb[ACTORS_COL]
                        actor_str = str(actor_str).replace("['Stars: ", "")
                        actor_str = str(actor_str).replace(
                            " | See full cast and crew »']", "")
                        for actor in str(actor_str).split(", "):
                            actors.add(actor)
                        Recommender.add_to_collection(imdb[GENRES_COL], genres)

                    if MOVIELENS in data:
                        movielens_node = data[MOVIELENS]
                        Recommender.add_to_collection(
                            movielens_node[DIRECTORS_COL], directors)
                        Recommender.add_to_collection(
                            movielens_node[LANGUAGES_COL], languages)
                        Recommender.add_to_collection(
                            movielens_node[ACTORS_COL], actors)
                        Recommender.add_to_collection(
                            movielens_node[GENRES_COL], genres)
                    if TMDB_COL in data:
                        tmdb = data[TMDB_COL]
                        for actor in tmdb[CREDITS_COL][CAST]:
                            actors.add(actor[NAME])
                        for crew in tmdb[CREDITS_COL][CREW]:
                            if crew[JOB] == DIRECTOR:
                                directors.add(crew[NAME])
                        for language in tmdb[SPOKEN_LANGUAGES]:
                            languages.add(language[NAME])
                        for genre in tmdb[GENRES_COL]:
                            genres.add(genre[NAME])
                        for keyword in tmdb[KEYWORDS_COL]:
                            keywords.add(keyword[NAME])
                    self.movie_metadata[row[MOVIE_ID]] = {
                        DIRECTORS_COL: directors,
                        LANGUAGES_COL: languages,
                        ACTORS_COL: actors,
                        GENRES_COL: genres,
                        KEYWORDS_COL: keywords
                    }
            except FileNotFoundError:
                logging.error(
                    f'[init] - No metadata for movie id <{str(row[MOVIE_ID])}>'
                )
Esempio n. 2
0
 def add_to_collection(src, dest):
     """
     Adds the source collection to the destination collection. Needed in cases, where the iterable object is
     neither hashable nor of NonType
     :param src: source collection, which gets added to destination
     :param dest: destination collection
     :return: None
     """
     if src is None or dest is None:
         logging.error(
             f'[{Recommender.add_to_collection.__name__}] - invalid parameters'
         )
         return
     for elem in src:
         dest.add(elem)
Esempio n. 3
0
def get_image_url(movie_title: str) -> str:
    """
    @param movie_title: the movie title for which an image url will be retrieved from OMDb
    @return: the image url, where the poster of the movie can be found. Note: Can be of type None.
    """

    if type(movie_title) is not str:
        logging.error(f'[{get_image_url.__name__}] - movie_title in parameter is not of type str')
        return None

    logging.debug(f'[{get_image_url.__name__}] - start function with movie title: {movie_title}')

    image_url: str = _title_to_image_url(movie_title=movie_title, comma_check=True)
    json_response = _get_json_response(image_url)

    # comma corrected movie_title did receive a response
    if json_response is not None:
        logging.debug(f'[{get_image_url.__name__}] - json response received with comma correction')
        return json_response
    # try without comma correction
    else:
        logging.debug(f'[{get_image_url.__name__}] - json response received without comma correction')
        image_url = _title_to_image_url(movie_title=movie_title, comma_check=False)
        return _get_json_response(image_url)
Esempio n. 4
0
def recommendation(request):
    if request.method == 'POST':
        logging.debug(
            f'[{recommendation.__name__}] - start function with request: {request}'
        )
        # The movie title is the value of the selected submit button in the form
        selection_query = request.POST['submit']
        # Again needs to be mapped to the actual movie object as only the string is provided
        selection = map_string_to_movie(selection_query)
        # ID for different algorithms to work
        selection_id = selection.iloc[0][MOVIE_ID]
        # Title to show the user as the selected movie
        selection_title = selection.iloc[0][TITLE]

        print('selection id {}, selection title {}'.format(
            selection_id, selection_title))
        # Results of different algorithms
        rec = recommender.Recommender()

        movies_metadata: list = rec.metadata_recommender(selection_id)
        movies_keywords: list = rec.metadata_recommender_with_keywords(
            selection_id)

        rec_obj = MovieRecommendationItemRating()
        movies_item_rating = rec_obj.get_similar_movies_based_on_itemRating(
            rec_obj, selection_title)

        obj_rec = MovieRecommendationByGenre()
        movies_genres = obj_rec.get_similar_movies_based_on_genre(
            selection_title)

        obj = MovieRecommendationByTags()
        movies_tags = obj.get_similar_movies_based_on_tags(selection_title)

        selection_tuple: tuple = (selection_title,
                                  mp.get_image_url(selection_title))

        try:
            alg1: dict = dict()
            alg2: dict = dict()
            alg3: dict = dict()
            alg4: dict = dict()
            alg5: dict = dict()

            movieList = [
                movies_metadata, movies_keywords, movies_item_rating,
                movies_genres, movies_tags
            ]
            alg_list = [alg1, alg2, alg3, alg4, alg5]

            with concurrent.futures.ThreadPoolExecutor(
                    max_workers=5) as executor:
                executor.map(_get_views_dict, movieList, alg_list)

            return render(
                request, "recommendations.html", {
                    "selection_title": selection_tuple,
                    "alg1": alg1,
                    "alg2": alg2,
                    "alg3": alg3,
                    "alg4": alg4,
                    "alg5": alg5
                })
        except Exception as excError:
            logging.error(
                f'An error occurred during the recommendation process with error: {excError}'
            )
            return render(request, "error.html", {"error": excError})
Esempio n. 5
0
def error(request):
    logging.error(
        f'An error occurred during the process with request: {request}')
    return render(request, "index.html", {})