Ratings: Reorganized code

pull/105/head
desimaniac 5 years ago
parent 8093f312c1
commit e37e4362bc

@ -5,44 +5,76 @@ import requests
log = logger.get_logger(__name__)
def get_rating(apikey, movie):
def get_rating(omdb_apikey, movie):
"""
Lookup movie ratings via OMDb
:param omdb_apikey: OMDb api key
:param movie: sorted_movie item
:return: rating or false if no rating found
"""
ratings_exist = False
imdb_id = movie['movie']['ids']['imdb']
if imdb_id:
log.debug("Requesting info from OMDb for: \'%s (%s)\' | IMDb ID: %s",
movie_imdbid = movie['movie']['ids']['imdb']
movie_year = str(movie['movie']['year']) if movie['movie']['year'] else '????'
if movie_imdbid:
log.debug("Requesting info from OMDb for: \'%s (%s)\' [IMDb ID: %s]",
movie['movie']['title'],
str(movie['movie']['year']) if movie['movie']['year'] else '????',
imdb_id)
r = requests.get('http://www.omdbapi.com/?i=' + imdb_id + '&apikey=' + apikey)
movie_year,
movie_imdbid)
r = requests.get('http://www.omdbapi.com/?i=' + movie_imdbid + '&apikey=' + omdb_apikey)
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'],
str(movie['movie']['year']) if movie['movie']['year'] else '????',
imdb_id)
movie_year,
movie_imdbid)
for source in json.loads(r.text)["Ratings"]:
if source['Source'] == 'Rotten Tomatoes':
# noinspection PyUnusedLocal
ratings_exist = True
log.debug("Rotten Tomatoes score of %s for: \'%s (%s)\' | IMDb ID: %s ",
log.debug("Rotten Tomatoes score of %s for: \'%s (%s)\' [IMDb ID: %s]",
source['Value'],
movie['movie']['title'],
str(movie['movie']['year']) if movie['movie']['year'] else '????',
imdb_id)
movie_year,
movie_imdbid)
return int(source['Value'].split('%')[0])
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'],
str(movie['movie']['year']) if movie['movie']['year'] else '????',
imdb_id)
movie_year,
movie_imdbid)
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'],
str(movie['movie']['year']) if movie['movie']['year'] else '????',
imdb_id)
movie_year,
movie_imdbid)
else:
log.debug("Skipping OMDb ratings lookup because no IMDb ID was found for: \'%s (%s)\'",
movie['movie']['title'],
str(movie['movie']['year']) if movie['movie']['year'] else '????')
movie_year)
return False
def does_movie_have_min_req_rating(api_key, sorted_movie, rating):
# pull RT score
movie_rating = get_rating(api_key, sorted_movie)
# convert movie year to string
movie_year = str(sorted_movie['movie']['year']) \
if sorted_movie['movie']['year'] else '????'
return -1
if not movie_rating:
log.info("SKIPPING: \'%s (%s)\' because a Rotten Tomatoes score could not be found.", sorted_movie['movie']['title'],
movie_year)
return False
elif movie_rating < rating:
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)
return False
elif movie_rating >= rating:
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)
return True

@ -697,9 +697,9 @@ def movies(list_type, add_limit=0, add_delay=2.5, sort='votes', rating=None, gen
sorted_movies_list = misc_helper.sorted_list(processed_movies_list, 'movie', 'votes')
log.info("Sorted movies list to process by highest 'votes'")
# display specified RT score
# display specified min RT score
if rating is not None and 'omdb' in cfg and 'api_key' in cfg['omdb'] and cfg['omdb']['api_key']:
log.debug("Minimum Rotten Tomatoes score of %d%% requested.", rating)
log.info("Minimum Rotten Tomatoes score of %d%% requested.", rating)
# loop movies
log.info("Processing list now...")
@ -725,43 +725,41 @@ def movies(list_type, add_limit=0, add_delay=2.5, sort='votes', rating=None, gen
if not trakt_helper.is_movie_blacklisted(sorted_movie, cfg.filters.movies, ignore_blacklist,
callback_remove_recommended if remove_rejected_from_recommended
else None):
# Assuming the movie is not blacklisted, proceed to pull RT score if the user wishes to restrict
movie_rating = None
# Skip movie if below user specified min RT rating
if rating is not None and 'omdb' in cfg and 'api_key' in cfg['omdb'] and cfg['omdb']['api_key']:
movie_rating = rating_helper.get_rating(cfg['omdb']['api_key'], sorted_movie)
if movie_rating == -1:
log.info("SKIPPED: \'%s (%s)\'", sorted_movie['movie']['title'],
movie_year)
if not rating_helper.does_movie_have_min_req_rating(cfg['omdb']['api_key'], sorted_movie, rating):
continue
if (rating is None or movie_rating is None) or movie_rating >= rating:
log.info("Adding: \'%s (%s)\' | Country: %s | Language: %s | Genre: %s ",
sorted_movie['movie']['title'],
movie_year,
(sorted_movie['movie']['country'] or 'N/A').upper(),
(sorted_movie['movie']['language'] or 'N/A').upper(),
movie_genres)
# add movie to radarr
if radarr.add_movie(sorted_movie['movie']['ids']['tmdb'], sorted_movie['movie']['title'],
movie_year,
sorted_movie['movie']['ids']['slug'], profile_id,
cfg.radarr.root_folder, cfg.radarr.minimum_availability, not no_search):
log.info("ADDED: \'%s (%s)\'", sorted_movie['movie']['title'],
movie_year)
if notifications:
callback_notify({'event': 'add_movie', 'list_type': list_type,
'movie': sorted_movie['movie']})
added_movies += 1
else:
log.error("FAILED ADDING: \'%s (%s)\'", sorted_movie['movie']['title'], movie_year)
log.info("Adding: \'%s (%s)\' | Country: %s | Language: %s | Genre: %s ",
sorted_movie['movie']['title'], movie_year,
(sorted_movie['movie']['country'] or 'N/A').upper(),
(sorted_movie['movie']['language'] or 'N/A').upper(), movie_genres)
# add movie to radarr
if radarr.add_movie(sorted_movie['movie']['ids']['tmdb'], sorted_movie['movie']['title'],
movie_year, sorted_movie['movie']['ids']['slug'], profile_id,
cfg.radarr.root_folder, cfg.radarr.minimum_availability, not no_search):
log.info("ADDED: \'%s (%s)\'", sorted_movie['movie']['title'], movie_year)
if notifications:
callback_notify({'event': 'add_movie', 'list_type': list_type, 'movie': sorted_movie['movie']})
added_movies += 1
else:
log.debug("Minimum Rotten Tomatoes score of %d%% was not met.", rating)
log.info("SKIPPED: \'%s (%s)\'", sorted_movie['movie']['title'], movie_year)
# stop adding movies, if added_movies >= add_limit
if add_limit and added_movies >= add_limit:
break
log.error("FAILED ADDING: \'%s (%s)\'", sorted_movie['movie']['title'], movie_year)
# sleep before adding any more
time.sleep(add_delay)
else:
log.info("SKIPPED: \'%s (%s)\'", sorted_movie['movie']['title'], movie_year)
# stop adding movies, if added_movies >= add_limit
if add_limit and added_movies >= add_limit:
break
# sleep before adding any more
time.sleep(add_delay)
except Exception:
log.exception("Exception while processing movie \'%s\': ", sorted_movie['movie']['title'])

Loading…
Cancel
Save