diff --git a/booru_utils.py b/booru_utils.py index 8e904c2..4c45cc9 100644 --- a/booru_utils.py +++ b/booru_utils.py @@ -136,15 +136,20 @@ def fetch_images_with_tag( response = requests.get(url, params=params) - if response.status_code == 200: - return response.json() - else: + if response.status_code != 200: print( f"Failed to fetch images with tag '{tag}'. Status code: {response.status_code}" ) print(f"Response: {response.text}") return [] + try: + return response.json() + except ValueError as e: + print(f"Failed to parse JSON response for tag '{tag}': {e}") + print(f"Response text: {response.text}") + return [] + def tag_exists(tag, danbooru_url, api_key, username): url = f"{danbooru_url}/tags.json" @@ -245,20 +250,27 @@ def fetch_new_comments(danbooru_url, api_key, username, last_comment_id): } response = requests.get(url, params=params) - filtered_response = [] - - # No easy way (i think) to filter these so... here we go :3 - for comment in response.json(): - if int(comment["id"]) > int(last_comment_id): - filtered_response.append(comment) - - if response.status_code == 200: - return filtered_response - else: + if response.status_code != 200: print(f"Failed to fetch new comments. Status code: {response.status_code}") print(f"Response: {response.text}") return [] + try: + comments_data = response.json() + except ValueError as e: + print(f"Failed to parse JSON response: {e}") + print(f"Response text: {response.text}") + return [] + + filtered_response = [] + + # No easy way (i think) to filter these so... here we go :3 + for comment in comments_data: + if int(comment["id"]) > int(last_comment_id): + filtered_response.append(comment) + + return filtered_response + def get_username(danbooru_url, api_key, username, user_id): url = f"{danbooru_url}/users/{user_id}.json" @@ -356,12 +368,17 @@ def fetch_usernames(danbooru_url, api_key, username, limit=10): } response = requests.get(url, params=params) - if response.status_code == 200: - return [user["name"] for user in response.json()] - else: + if response.status_code != 200: print(f"Failed to fetch usernames. Status code: {response.status_code}") print(f"Response: {response.text}") return [] + + try: + return [user["name"] for user in response.json()] + except ValueError as e: + print(f"Failed to parse JSON response for usernames: {e}") + print(f"Response text: {response.text}") + return [] def fetch_usernames_with_favs(danbooru_url, api_key, username, limit=10): @@ -392,8 +409,13 @@ def fetch_usernames_with_favs(danbooru_url, api_key, username, limit=10): } favs_response = requests.get(favs_url, params=favs_params) - if favs_response.status_code == 200 and favs_response.json(): - users_with_favs.append(user_name) + if favs_response.status_code == 200: + try: + if favs_response.json(): + users_with_favs.append(user_name) + except ValueError: + # Skip if JSON parsing fails + pass return users_with_favs @@ -414,15 +436,20 @@ def fetch_user_favorites( # Make the request response = requests.get(url, params=params) - if response.status_code == 200: - # Return the list of post IDs from the response - return [post["id"] for post in response.json()] - else: + if response.status_code != 200: print( f"Failed to fetch favorites for '{user}'. Status code: {response.status_code}" ) print(f"Response: {response.text}") return [] + + try: + # Return the list of post IDs from the response + return [post["id"] for post in response.json()] + except ValueError as e: + print(f"Failed to parse JSON response for favorites '{user}': {e}") + print(f"Response text: {response.text}") + return [] def get_tag_from_tag_id(tag_id, api_url, api_key, username):