0

I'm making a Web Application about movies and using IMDb's API. Here's the JSON result:

{"items":[{
"id":"tt0111161",
"rank":"1",
"title":"The Shawshank Redemption",
"fullTitle":"The Shawshank Redemption (1994)",
"year":"1994",
"image":"https://m.media-amazon.com/images/M/MV5BMDFkYTc0MGEtZmNhMC00ZDIzLWFmNTEtODM1ZmRlYWMwMWFmXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_UX128_CR0,3,128,176_AL_.jpg",
"crew":"Frank Darabont (dir.), Tim Robbins, Morgan Freeman",
"imDbRating":"9.2",
"imDbRatingCount":"2463571"},
{"id":"tt0068646",
"rank":"2",
"title":"The Godfather",
"fullTitle":"The Godfather (1972)",
"year":"1972",
"image":"https://m.media-amazon.com/images/M/MV5BM2MyNjYxNmUtYTAwNi00MTYxLWJmNWYtYzZlODY3ZTk3OTFlXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_UX128_CR0,1,128,176_AL_.jpg",
"crew":"Francis Ford Coppola (dir.), Marlon Brando, Al Pacino",
"imDbRating":"9.1",
"imDbRatingCount":"1703634"}
...
{"id":"tt0018455",
"rank":"250",
"title":"Sunrise",
"fullTitle":"Sunrise (1927)",
"year":"1927",
"image":"https://m.media-amazon.com/images/M/MV5BNDVkYmYwM2ItNzRiMy00NWQ4LTlhMjMtNDI1ZDYyOGVmMzJjXkEyXkFqcGdeQXVyNTgzMzU5MDI@._V1_UX128_CR0,3,128,176_AL_.jpg",
"crew":"F.W. Murnau (dir.), George O'Brien, Janet Gaynor",
"imDbRating":"8.0",
"imDbRatingCount":"49521"}],
"errorMessage":""}

As you can see, this is a list of dicts inside an "items" dict. Since this is the top250 movies, I'm getting all of this data and I only want the top10. Is there a way to do this?

1 Answer 1

0

I'm getting otherwise related errors, so I can't know for sure, but I think I figured it out. All I need from the data is the name of the top10 movies to use others API requests and get additional info, so I did this:

def top_ten_movies():
    api_key = os.environ.get("API_KEY")
    try:
        url = f"https://imdb-api.com/en/API/Top250Movies/{api_key}"
        response = requests.get(url)
        response.raise_for_status()

    except requests.RequestException:
        return None

    try:
        movies = response.json()
        y = movies["items"]

        for i in range(len(y)):
            while i < 10:
                return {
                    "name": y[i]["title"],
                }

    except requests.RequestException:
        return None

If you see some mistake or room for improvement, feel free to tell me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .