Add extra error handling
This commit is contained in:
parent
91eb0fb7f5
commit
99f4370711
|
|
@ -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,13 +368,18 @@ 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):
|
||||
url = f"{danbooru_url}/users.json"
|
||||
|
|
@ -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():
|
||||
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,16 +436,21 @@ 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):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue