From ad8787812ae39bd2c6961c589a3eeb0ba357855c Mon Sep 17 00:00:00 2001 From: Vee9ahd1 Date: Mon, 10 May 2021 20:15:04 -0400 Subject: added some error handling for the case where artbreeder's api doesn't return any info for a given key --- gantools/ganbreeder.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'gantools') diff --git a/gantools/ganbreeder.py b/gantools/ganbreeder.py index a2546a2..40e58d3 100644 --- a/gantools/ganbreeder.py +++ b/gantools/ganbreeder.py @@ -52,13 +52,29 @@ def get_info(sid, key): cookies = { 'connect.sid': sid } - r = requests.get('http://artbreeder.com/info?k='+str(key), cookies=cookies) + url = 'http://artbreeder.com/info?k='+str(key) + r = requests.get(url, cookies=cookies) r.raise_for_status() - return parse_info_dict(r.json()) + + r = r.json() + if r is not None: + try: + return parse_info_dict(r) + except Exception as err: + print("Could not retrieve info for key \'{0}\': {1}".format(key, err)) + raise + else: + raise RuntimeError("No info could be found at URL \'{0}\'".format(url)) def get_info_batch(username, password, keys): l = list() sid = login(username, password) for key in keys: - l.append(get_info(sid, key)) + try: + l.append(get_info(sid, key)) + except Exception as err: + print("Encountered an error while attempting" + "to download info for key {0}".format(key)) + print("Error says: {}".format(err)) + print("Skipping...") return l -- cgit v1.2.1