"""IMDB backend using Cinemagoer library."""
from imdb import Cinemagoer
class IMDBBackend:
"""Wrapper class for Cinemagoer to query IMDB data."""
def __init__(self):
self.ia = Cinemagoer()
def search_movies(self, query: str, limit: int = 10) -> list[dict]:
"""Search for movies by title.
Args:
query: Movie title to search for
limit: Maximum number of results to return
Returns:
List of movie dicts with id, title, and year
"""
movies = self.ia.search_movie(query)[:limit]
results = []
for movie in movies:
results.append({
"id": movie.movieID,
"title": movie.get("title", ""),
"year": movie.get("year", None),
})
return results
def get_movie_details(self, movie_id: str) -> dict:
"""Get full details for a movie.
Args:
movie_id: IMDB movie ID
Returns:
Dict with movie details including title, year, rating, plot, etc.
"""
movie = self.ia.get_movie(movie_id)
return {
"id": movie.movieID,
"title": movie.get("title", ""),
"year": movie.get("year", None),
"rating": movie.get("rating", None),
"votes": movie.get("votes", None),
"plot": movie.get("plot", []),
"genres": movie.get("genres", []),
"directors": [
{"id": d.personID, "name": d.get("name", "")}
for d in movie.get("directors", [])
],
"writers": [
{"id": w.personID, "name": w.get("name", "")}
for w in movie.get("writers", [])
],
"runtime": movie.get("runtimes", []),
"countries": movie.get("countries", []),
"languages": movie.get("languages", []),
}
def search_people(self, query: str, limit: int = 10) -> list[dict]:
"""Search for people (actors, directors, etc.) by name.
Args:
query: Person name to search for
limit: Maximum number of results to return
Returns:
List of person dicts with id and name
"""
people = self.ia.search_person(query)[:limit]
results = []
for person in people:
results.append({
"id": person.personID,
"name": person.get("name", ""),
})
return results
def get_person_details(self, person_id: str) -> dict:
"""Get full details for a person including filmography.
Args:
person_id: IMDB person ID
Returns:
Dict with person details and filmography
"""
person = self.ia.get_person(person_id)
# Build filmography from various roles
filmography = {}
for role in ["actor", "actress", "director", "writer", "producer"]:
if role in person.get("filmography", {}):
filmography[role] = [
{
"id": m.movieID,
"title": m.get("title", ""),
"year": m.get("year", None),
}
for m in person["filmography"][role][:10] # Limit to 10 per role
]
return {
"id": person.personID,
"name": person.get("name", ""),
"birth_date": str(person.get("birth date", "")) if person.get("birth date") else None,
"birth_place": person.get("birth notes", None),
"bio": person.get("mini biography", [])[:1], # First bio entry
"filmography": filmography,
}
def get_top_250_movies(self, limit: int = 25) -> list[dict]:
"""Get IMDB Top 250 movies.
Args:
limit: Maximum number of movies to return
Returns:
List of top-rated movie dicts with rank, id, title, year, and rating
"""
top_movies = self.ia.get_top250_movies()[:limit]
results = []
for rank, movie in enumerate(top_movies, start=1):
results.append({
"rank": rank,
"id": movie.movieID,
"title": movie.get("title", ""),
"year": movie.get("year", None),
"rating": movie.get("rating", None),
})
return results
def get_movie_cast(self, movie_id: str, limit: int = 20) -> list[dict]:
"""Get the cast of a movie.
Args:
movie_id: IMDB movie ID
limit: Maximum number of cast members to return
Returns:
List of cast member dicts with id, name, and role
"""
movie = self.ia.get_movie(movie_id)
cast = movie.get("cast", [])[:limit]
results = []
for actor in cast:
results.append({
"id": actor.personID,
"name": actor.get("name", ""),
"role": str(actor.currentRole) if actor.currentRole else None,
})
return results