Refactored and improved exception handling

This commit is contained in:
Anon 2022-08-30 00:16:50 -07:00
parent 71f321c6f9
commit 2475241f21

View File

@ -9,15 +9,11 @@ import os
def random_tag(*tags): def random_tag(*tags):
if len(tags) == 1 and tags[0].lower() == "random": return len(tags) == 1 and tags[0].lower() == "random"
return True
return False
def get_most_sever_rating(*ratings): def get_most_sever_rating(rating):
if "q" in ratings or "e" in ratings: return rating in ("q", "e")
return True
return False
class downloader: class downloader:
@ -42,9 +38,8 @@ class downloader:
print("Remote image request returned:", remote_image.status_code) print("Remote image request returned:", remote_image.status_code)
return None return None
for d in full_path: with open(full_path, "wb") as f:
with open(d, "wb") as f: f.write(remote_image.content)
f.write(remote_image.content)
return post return post
@ -80,11 +75,11 @@ class downloader:
if tag_type in response: if tag_type in response:
tag_response.append(response[tag_type].strip()) tag_response.append(response[tag_type].strip())
nsfw = search_request.json()[0]["rating"] nsfw = response["rating"]
nsfw = get_most_sever_rating(nsfw) nsfw = get_most_sever_rating(nsfw)
file_url = response["file_url"] file_url = response["file_url"]
basename = file_url.rsplit("/", 1)[1] basename = file_url.rsplit("/", 1)[1]
full_path = os.path.join(self.tmp, basename) full_path = os.path.join(self.tmp, basename)
@ -99,7 +94,7 @@ class downloader:
# Query results # Query results
"search_url": search_url, "search_url": search_url,
"file_url": file_url, "file_url": file_url,
"full_path": [full_path], "full_path": full_path,
"tag_response": " ".join(tag_response), "tag_response": " ".join(tag_response),
"nsfw": nsfw "nsfw": nsfw
} }