1

I'm trying to execute this code that shows me some IMDB movie ratings:

import json
import sys

import imdb
import sendgrid

NOTIFY_ABOVE_RATING = 7.5

SENDGRID_API_KEY = "API KEY GOES HERE"


def run_checker(scraped_movies):
    imdb_conn = imdb.IMDb()
    good_movies = []
    for scraped_movie in scraped_movies:
        imdb_movie = get_imdb_movie(imdb_conn, scraped_movie['name'])
        if imdb_movie['rating'] > NOTIFY_ABOVE_RATING:
            good_movies.append(imdb_movie)
    if good_movies:
        send_email(good_movies)


def get_imdb_movie(imdb_conn, movie_name):
    results = imdb_conn.search_movie(movie_name)
    movie = results[0]
    imdb_conn.update(movie)
    print("{title} => {rating}".format(**movie))
    return movie


def send_email(movies):
    sendgrid_client = sendgrid.SendGridClient(SENDGRID_API_KEY)
    message = sendgrid.Mail()
    message.add_to("trevor@example.com")
    message.set_from("no-reply@example.com")
    message.set_subject("Highly rated movies of the day")
    body = "High rated today:<br><br>"
    for movie in movies:
        body += "{title} => {rating}".format(**movie)
    message.set_html(body)
    sendgrid_client.send(message)
    print("Sent email with {} movie(s).".format(len(movies)))


if __name__ == '__main__':
    movies_json_file = sys.argv[1]
    with open(movies_json_file) as scraped_movies_file:
        movies = json.loads(scraped_movies_file.read())
    run_checker(movies)

But it returns me this error:

C:\Python27\Scripts>python check_imdb.py movies.json
T2 Trainspotting => 8.1
Sing => 7.3
Traceback (most recent call last):
  File "check_imdb.py", line 49, in <module>
    run_checker(movies)
  File "check_imdb.py", line 16, in run_checker
    imdb_movie = get_imdb_movie(imdb_conn, scraped_movie['name'])
  File "check_imdb.py", line 27, in get_imdb_movie
    print("{title} => {rating}".format(**movie))
KeyError: 'rating'

This happens because it's trying to print a movie that have no rating yet. I tryed if/else many times to fix it, but no success.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
Lestat
  • 71
  • 2
  • 9

1 Answers1

0

The issue is because your movie dict is not having rating key . In order to fix this, you may set the default value of rating using dict.setdefault as 'unknown' or '0' (based on what suits you) as:

# set value of 'rating' as 'unknown' if 'rating' key is not present
movie.setdefault('rating', 'unknown')  

# then do format as:
"{title} => {rating}".format(**movie)

For more information, check: Use cases for the setdefault dict method

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • I did it. But now, when there is no Title, it shows `movie = results[0]` `IndexError: list index out of range` Tryed movie.setdefault('title', 'unknown') but nothing chaged. – Lestat Jan 31 '17 at 22:14
  • @Lestat I believe you know that SO is not tutorial service. You will have to put some effort to debug the issue by your own. Based on this new error, maximum I (or anyone else) could tell is `results` is empty list here and you are trying to access the element at 0th index. Hence, the message which you are seeing in the error – Moinuddin Quadri Jan 31 '17 at 22:16