Ratings: Cleaner function

Update rating.py
pull/105/head
desimaniac 5 years ago
parent f1adf63d88
commit f486ae21a3

@ -5,76 +5,77 @@ import requests
log = logger.get_logger(__name__) log = logger.get_logger(__name__)
def get_rating(omdb_apikey, movie): def get_rating(omdb_api_key, movie_title, movie_year, movie_imdb_id):
""" """
Lookup movie ratings via OMDb Lookup movie ratings via OMDb
:param omdb_apikey: OMDb api key :param omdb_api_key: OMDb API Key
:param movie: sorted_movie item :param movie_title: Movie Title
:return: rating or false if no rating found :param movie_year: Movie Year
:param movie_imdb_id: Movie IMDb ID
:return: Rating score (int) or False if no rating found
""" """
ratings_exist = False ratings_exist = False
movie_imdbid = movie['movie']['ids']['imdb']
movie_year = str(movie['movie']['year']) if movie['movie']['year'] else '????'
if movie_imdbid: if movie_imdb_id:
log.debug("Requesting info from OMDb for: \'%s (%s)\' [IMDb ID: %s]", log.debug("Requesting info from OMDb for: \'%s (%s)\' [IMDb ID: %s]",
movie['movie']['title'], movie_title,
movie_year, movie_year,
movie_imdbid) movie_imdb_id)
r = requests.get('http://www.omdbapi.com/?i=' + movie_imdbid + '&apikey=' + omdb_apikey) r = requests.get('http://www.omdbapi.com/?i=' + movie_imdb_id + '&apikey=' + omdb_api_key)
if r.status_code == 200 and json.loads(r.text)["Response"] == 'True': if r.status_code == 200 and json.loads(r.text)["Response"] == 'True':
log.debug("Successfully requested ratings from OMDB for \'%s (%s)\' [IMDb ID: %s]", log.debug("Successfully requested ratings from OMDB for \'%s (%s)\' [IMDb ID: %s]",
movie['movie']['title'], movie_title,
movie_year, movie_year,
movie_imdbid) movie_imdb_id)
for source in json.loads(r.text)["Ratings"]: for source in json.loads(r.text)["Ratings"]:
if source['Source'] == 'Rotten Tomatoes': if source['Source'] == 'Rotten Tomatoes':
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
ratings_exist = True ratings_exist = True
log.debug("Rotten Tomatoes score of %s for: \'%s (%s)\' [IMDb ID: %s]", log.debug("Rotten Tomatoes score for \'%s (%s)\' [IMDb ID: %s]: %s",
source['Value'], movie_title,
movie['movie']['title'],
movie_year, movie_year,
movie_imdbid) movie_imdb_id,
source['Value'])
return int(source['Value'].split('%')[0]) return int(source['Value'].split('%')[0])
if not ratings_exist: if not ratings_exist:
log.debug("No Rotten Tomatoes score found for: \'%s (%s)\' [IMDb ID: %s]", log.debug("No Rotten Tomatoes score found for: \'%s (%s)\' [IMDb ID: %s]",
movie['movie']['title'], movie_title,
movie_year, movie_year,
movie_imdbid) movie_imdb_id)
else: else:
log.debug("Error encountered when requesting ratings from OMDb for: \'%s (%s)\' [IMDb ID: %s]", log.debug("Error encountered when requesting ratings from OMDb for: \'%s (%s)\' [IMDb ID: %s]",
movie['movie']['title'], movie_title,
movie_year, movie_year,
movie_imdbid) movie_imdb_id)
else: else:
log.debug("Skipping OMDb ratings lookup because no IMDb ID was found for: \'%s (%s)\'", log.debug("Skipping OMDb ratings lookup because no IMDb ID was found for: \'%s (%s)\'",
movie['movie']['title'], movie_title,
movie_year) movie_year)
return False return False
def does_movie_have_min_req_rating(api_key, sorted_movie, rating): def does_movie_have_min_req_rating(omdb_api_key, sorted_movie, req_rating):
# pull RT score movie_title = sorted_movie['movie']['title']
movie_rating = get_rating(api_key, sorted_movie)
# convert movie year to string
movie_year = str(sorted_movie['movie']['year']) \ movie_year = str(sorted_movie['movie']['year']) \
if sorted_movie['movie']['year'] else '????' if sorted_movie['movie']['year'] else '????'
movie_imdb_id = sorted_movie['movie']['ids']['imdb']
# pull RT score
movie_rating = get_rating(omdb_api_key, movie_title, movie_year, movie_imdb_id)
if not movie_rating: if not movie_rating:
log.info("SKIPPING: \'%s (%s)\' because a Rotten Tomatoes score could not be found.", sorted_movie['movie']['title'], log.info("SKIPPING: \'%s (%s)\' because a Rotten Tomatoes score could not be found.", movie_title,
movie_year) movie_year)
return False return False
elif movie_rating < rating: elif movie_rating < req_rating:
log.info("SKIPPING: \'%s (%s)\' because its Rotten Tomatoes score of %d%% is below the required score of %d%%.", log.info("SKIPPING: \'%s (%s)\' because its Rotten Tomatoes score of %d%% is below the required score of %d%%.",
sorted_movie['movie']['title'], movie_year, movie_rating, rating) movie_title, movie_year, movie_rating, req_rating)
return False return False
elif movie_rating >= rating: elif movie_rating >= req_rating:
log.info("ADDING: \'%s (%s)\' because its Rotten Tomatoes score of %d%% is above the required score of %d%%.", log.info("ADDING: \'%s (%s)\' because its Rotten Tomatoes score of %d%% is above the required score of %d%%.",
sorted_movie['movie']['title'], movie_year, movie_rating, rating) movie_title, movie_year, movie_rating, req_rating)
return True return True

Loading…
Cancel
Save