mardi 31 janvier 2017

IMDBpy KeyError: 'rating'

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.

Aucun commentaire:

Enregistrer un commentaire