0

I want to be able to be able to give a script a person's IMDB ID and then the script will print out the person's filmography.

Here is the code I have so far:

import imdb
moviesDB = imdb.IMDb()
#PeterDinklage IMDB ID
ActorId = "0227759"
person = moviesDB.get_person(ActorId)
#Actor's name
print (person)
actor_work = moviesDB.get_person_filmography(ActorId)

print (actor_work['data']['filmography']['actor'])

This would give me an output that is messy and looks like this :

[<Movie id:6674864[http] title:_The Dwarf () (None)_>, <Movie id:2028582[http] title:_The Wild Bunch (2022)_>, <Movie id:4058618[http] title:_The Thicket () (None)_>, ...]

I could parse through this output to get the format I desire but I was wondering if there was a cleaner way to grab the information so that the output looked more like this:

The Dwarf ()
The Wild Bunch (2022)
The Thicket ()
...

I realized I could do something with a for loop like this:

for movie in actor_work['data']['filmography']['actor'] :
    print (movie)

but this would just give me an output with the movie names and not include the year of the movie or tv show.

Makuza
  • 139
  • 6
  • read the docs https://imdbpy.readthedocs.io/en/latest/usage/quickstart.html – drum Aug 12 '21 at 01:07
  • I realized I can use a for loop but it will only give me the title of the movie, I want the year as well. – Makuza Aug 12 '21 at 01:09

1 Answers1

0

I was able to do this by using the movie object year. It will error out if there is no listed year for the movie. For example if the actor has an upcoming movie that does not have a specified release date. So to get around this you can use try and except

Here is the code I used:

import imdb
moviesDB = imdb.IMDb()

#PeterDinklage IMDB ID
ActorId = "0227759"
person = moviesDB.get_person(ActorId)
#Actor's name
print (person)
actor_work = moviesDB.get_person_filmography(ActorId)
for movie in actor_work['data']['filmography']['actor'] :
    try:
        print (movie.movieID, movie['title'], movie.currentRole, movie['year'])
    except KeyError:
        #print("No Year for the movie")
        print (movie.movieID, movie['title'], movie.currentRole)
Makuza
  • 139
  • 6