Added check to skip request if a movie does not have an imdbID

pull/49/merge^2
davidaghaian@student.cccd.edu 6 years ago
parent a906b361ec
commit 8fc96d8b0b

@ -5,22 +5,26 @@ import requests
log = logger.get_logger(__name__) log = logger.get_logger(__name__)
def get_rating(apikey,movie): def get_rating(apikey,movie):
imdbID = movie['movie']['ids']['imdb'] 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'], 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) ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(),imdbID)
r = requests.get('http://www.omdbapi.com/?i=' + imdbID + '&apikey=' + apikey) r = requests.get('http://www.omdbapi.com/?i=' + imdbID + '&apikey=' + apikey)
if(r.status_code == 200): if(r.status_code == 200):
log.debug("Successfully requested ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s", log.debug("Successfully requested ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s",
movie['movie']['title'], movie['movie']['year'], movie['movie']['title'], movie['movie']['year'],
', '.join(movie['movie']['genres']), movie['movie']['country'].upper(), imdbID) ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(), imdbID)
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'):
log.debug("Rotten Tomatoes shows rating: %s for %s (%d) | Genres: %s | Country: %s | imdbID: %s ",source['Value'],movie['movie']['title'], movie['movie']['year'], 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) ', '.join(movie['movie']['genres']), movie['movie']['country'].upper(),imdbID)
return int(source['Value'].split('%')[0]) 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)
else: else:
log.debug("Error encountered when requesting ratings from OMDB for %s (%d) | Genres: %s | Country: %s | imdbID: %s", log.debug("Skipping %s (%d) | Genres: %s | Country: %s as it does not have an imdbID",
movie['movie']['title'], movie['movie']['year'], movie['movie']['title'], movie['movie']['year'],
', '.join(movie['movie']['genres']), movie['movie']['country'].upper(), imdbID) ', '.join(movie['movie']['genres']), movie['movie']['country'].upper())
return -1 return -1

Loading…
Cancel
Save