0

I'm a beginner at Python.

I was trying to get some information about a movie using Imdbpy. The code looks like this:

def imdb(movie_name):
    ia = IMDb()
    results = ia.search_movie(movie_name)
    movie_id = results[0].getID()
    movie = ia.get_movie(movie_id)
    return movie

This is very slow, it took about 1 minute to get the information. So I asked Chatgpt to make it faster and it told me to do this:

def imdb(movie_name):
    ia = IMDb()
    movie_fields = ['title', 'year', 'genres', 'Rating']
    results = ia.search_movie(movie_name)
    movie_id = results[0].getID()
    movie = ia.get_movie(movie_id, info=movie_fields)
    return movie

But now I get error like this:

2023-04-28 14:12:00,808 ERROR [imdbpy] C:\Users\mahdi\AppData\Roaming\Python\Python310-32\site-packages\imdb\__init__.py:844: unknown information set "title"  
2023-04-28 14:12:00,813 ERROR [imdbpy] C:\Users\mahdi\AppData\Roaming\Python\Python310-32\site-packages\imdb\__init__.py:844: unknown information set "year"   
2023-04-28 14:12:00,813 ERROR [imdbpy] C:\Users\mahdi\AppData\Roaming\Python\Python310-32\site-packages\imdb\__init__.py:844: unknown information set "genres" 
2023-04-28 14:12:00,813 ERROR [imdbpy] C:\Users\mahdi\AppData\Roaming\Python\Python310-32\site-packages\imdb\__init__.py:844: unknown information set "Rating" 
Traceback (most recent call last):
  File "e:\python\file_name\film_names.py", line 185, in <module>
    final_name = f"{film_name}.{movie['year']}"      
  File "C:\Users\mahdi\AppData\Roaming\Python\Python310-32\site-packages\imdb\utils.py", line 1503, in __getitem__
    rawData = self.data[key]
KeyError: 'year'

Could you please help me to make this error disappear and also make it faster? Or if Imdb is not a good module to use, what should I use then?

I did try some APIs before this error popped up, I think some of them were Omdb API and Imdb API but it was working with requests and I just didn't want it to be complex.

And to note, I do want more information than ['title', 'year', 'genres', 'Rating']. I want the plot that I'm taking this somewhere else and if it's a series I wanna get every episode's name and rate and maybe something else, not sure.

paleonix
  • 2,293
  • 1
  • 13
  • 29
zedmahdi
  • 9
  • 2

1 Answers1

1

the error is because the keys (columns) chatGPT told you isn't right (['title', 'year', 'genres', 'Rating']), as chatGPT is very good in text comprehension and search; but not in exact values (maths, programming..) .

Then, IMDb() database calls its web API every time you query to it; so I guess that's the reason for slowness.

Then I recomnend call once the search_movie() and search by your own the data. Example:

import imdb
    
ia = imdb.IMDb()
items = ia.search_movie('Avengers')
# check if the list is empty
if len(items) > 0:
    for i in items:
        if i['title'] == 'Next Avengers: Heroes of Tomorrow':
            print(i)

You can check the keys (columns) of the rows like a dictionary:

print(items[0].keys())

Happy coding :)

Gabo
  • 11
  • 2
  • thanks for your code but that's not what i am looking for. because the movie_name is the user varible and its not the orginal one from imdb and i want to get the id i dont think i can get infos without the id. thanks for the code agian – zedmahdi Apr 28 '23 at 13:38