kishan0725 / AJAX-Movie-Recommendation-System-with-Sentiment-Analysis

A content-based recommender system that recommends movies similar to the movie the user likes and analyses the sentiments of the reviews given by the user
https://tmc.kishanlal.dev
533 stars 456 forks source link

Attribute name must be string, not 'int' || Typecaste to str as well #33

Open rsurati opened 1 year ago

rsurati commented 1 year ago

TypeError Traceback (most recent call last)

Cell In[188], line 1, in (x) ----> 1 df['genres'] = df['Title'].map(lambda x: get_genre(str(x)))

Cell In[186], line 7, in get_genre(x) 5 result = tmdb_movie.search(x) 6 if ( len(result) != 0 ): ----> 7 movie_id = str(result[0]['id']) 8 response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.) 9 data_json = response.json()

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\tmdbv3api\as_obj.py:47, in AsObj.getitem(self, key) 45 return self._obj_list[key] 46 else: ---> 47 return getattr(self, key)

TypeError: attribute name must be string, not 'int'

Here is the genre method:

from tmdbv3api import Movie tmdb_movie = Movie() def get_genre(x): genres = [] result = tmdb_movie.search(x) if ( len(result) != 0 ): print(type(result[0])) movie_id = str(result[0].id) response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.api_key)) data_json = response.json() if data_json['genres']: genre_str = " " for i in range(0,len(data_json['genres'])): genres.append(data_json['genres'][i]['name']) return genre_str.join(genres) else: np.NaN else: np.NaN

Siddharth-Latthe-07 commented 3 months ago

The error you're encountering is because tmdbv3api returns a custom object, and you're trying to access its attributes using getitem syntax corrected get_genre function:-

from tmdbv3api import Movie
import requests
import numpy as np

tmdb_movie = Movie()

def get_genre(x):
    genres = []
    result = tmdb_movie.search(x)

    if len(result) != 0:
        movie_id = str(result[0].id)
        response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id, tmdb.api_key))
        data_json = response.json()

        if 'genres' in data_json and data_json['genres']:
            genre_str = " "
            for genre in data_json['genres']:
                genres.append(genre['name'])
            return genre_str.join(genres)
        else:
            return np.NaN
    else:
        return np.NaN

and update the data frame:- df['genres'] = df['Title'].map(lambda x: get_genre(str(x)))

Hope this helps Thanks