From c1a42c636c7388882a4b8c030d5764e90d34e2ff Mon Sep 17 00:00:00 2001 From: desimaniac <5501908+desimaniac@users.noreply.github.com> Date: Fri, 14 Jun 2019 03:53:58 -0500 Subject: [PATCH] Python: Improved get_rating function - handle missing responses from OMDB - aesthetics - PEP --- helpers/rating.py | 42 ++++++++++++++++++++++++------------------ traktarr.py | 3 ++- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/helpers/rating.py b/helpers/rating.py index 353903b..0bf601a 100644 --- a/helpers/rating.py +++ b/helpers/rating.py @@ -3,28 +3,34 @@ import json import requests log = logger.get_logger(__name__) -def get_rating(apikey,movie): - imdbID = movie['movie']['ids']['imdb'] - if(imdbID): - log.debug("Requesting ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s",movie['movie']['title'], movie['movie']['year'], - ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(),imdbID) - r = requests.get('http://www.omdbapi.com/?i=' + imdbID + '&apikey=' + apikey) - if(r.status_code == 200): - log.debug("Successfully requested ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s", + + +def get_rating(apikey, movie): + + imdb_id = movie['movie']['ids']['imdb'] + if imdb_id: + log.debug("Requesting info from OMDB for %s (%d) | Genres: %s | Country: %s | IMDB ID: %s", + movie['movie']['title'], movie['movie']['year'], ', '.join(movie['movie']['genres']), + (movie['movie']['country'] or 'N/A').upper(), imdb_id) + r = requests.get('http://www.omdbapi.com/?i=' + imdb_id + '&apikey=' + apikey) + if r.status_code == 200 and json.loads(r.text)["Response"] == 'True': + log.debug("Successfully requested ratings from OMDB for %s (%d) | Genres: %s | Country: %s | IMDB ID: %s", movie['movie']['title'], movie['movie']['year'], - ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(), imdbID) + ', '.join(movie['movie']['genres']), (movie['movie']['country'] or 'N/A').upper(), imdb_id) for source in json.loads(r.text)["Ratings"]: - if(source['Source'] == 'Rotten Tomatoes'): - log.debug("Rotten Tomatoes shows rating: %s for %s (%d) | Genres: %s | Country: %s | imdbID: %s ",source['Value'],movie['movie']['title'], movie['movie']['year'], - ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(),imdbID) + if source['Source'] == 'Rotten Tomatoes': + log.debug("Rotten Tomatoes score of %s for %s (%d) | Genres: %s | Country: %s | IMDB ID: %s ", + source['Value'], movie['movie']['title'], movie['movie']['year'], + ', '.join(movie['movie']['genres']), (movie['movie']['country'] or 'N/A').upper(), + imdb_id) return int(source['Value'].split('%')[0]) else: - log.debug("Error encountered when requesting ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s", - movie['movie']['title'], movie['movie']['year'], - ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(), imdbID) + log.debug("Error encountered when requesting ratings from OMDB for %s (%d) | Genres: %s | Country: %s" + + " | IMDB ID: %s", movie['movie']['title'], movie['movie']['year'], + ', '.join(movie['movie']['genres']), (movie['movie']['country'] or 'N/A').upper(), imdb_id) else: - log.debug("Skipping %s (%d) | Genres: %s | Country: %s as it does not have an imdbID", - movie['movie']['title'], movie['movie']['year'], - ', '.join(movie['movie']['genres']), movie['movie']['country'].upper()) + log.debug("Skipping %s (%d) | Genres: %s | Country: %s as it does not have an IMDB ID", + movie['movie']['title'], movie['movie']['year'], ', '.join(movie['movie']['genres']), + (movie['movie']['country'] or 'N/A').upper()) return -1 diff --git a/traktarr.py b/traktarr.py index 9a986bb..3d44cd4 100755 --- a/traktarr.py +++ b/traktarr.py @@ -574,7 +574,8 @@ def movies(list_type, add_limit=0, add_delay=2.5, sort='votes', rating=None, gen log.error("FAILED adding %s (%d)", sorted_movie['movie']['title'], sorted_movie['movie']['year']) else: - log.info("SKIPPING: %s (%d) | Genres: %s | Country: %s", sorted_movie['movie']['title'], + log.info("Minimum Rotten Tomatoes score was not met. " + + "Skipping: %s (%d) | Genres: %s | Country: %s", sorted_movie['movie']['title'], sorted_movie['movie']['year'], ', '.join(sorted_movie['movie']['genres']), (sorted_movie['movie']['country'] or 'N/A').upper()) # stop adding movies, if added_movies >= add_limit