Refactored, image validation is on the backend
This commit is contained in:
parent
8b31de29f6
commit
5ff564326e
@ -44,7 +44,6 @@ class YandereBot:
|
||||
settings_post_default = None
|
||||
settings_encrypt = None
|
||||
settings_credentials = None
|
||||
settings_banned = None
|
||||
|
||||
# Class variables
|
||||
mastodon_api = None
|
||||
@ -89,7 +88,6 @@ class YandereBot:
|
||||
"settings_post_default",
|
||||
"settings_encrypt",
|
||||
"settings_credentials",
|
||||
"settings_banned"
|
||||
)
|
||||
_settings = settings or default_settings
|
||||
for ele in _settings:
|
||||
@ -198,43 +196,32 @@ class YandereBot:
|
||||
string_post = content_newline.join(filter(None, (static_message, string_imglinks_joined)))
|
||||
|
||||
return content_type, string_post
|
||||
|
||||
|
||||
def is_banned(self, picked):
|
||||
tag_response = picked["tag_response"]
|
||||
for tag in self.settings_banned:
|
||||
if tag in tag_response:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def valid_mimetype(self, picked):
|
||||
full_path = picked["full_path"]
|
||||
mime = magic.from_file(full_path, mime=True)
|
||||
|
||||
|
||||
if mime is None:
|
||||
return False
|
||||
|
||||
|
||||
mime_category = mime.split("/", 1)[0]
|
||||
|
||||
|
||||
return mime_category in ("image", "video")
|
||||
|
||||
|
||||
|
||||
def _post(self, picked):
|
||||
# Validate picked
|
||||
if picked is None:
|
||||
raise InvalidPost("Picked post is None")
|
||||
elif self.is_banned(picked):
|
||||
raise BannedTag("Tag is banned")
|
||||
raise InvalidPost("Picked post is None")
|
||||
|
||||
if not os.path.isfile(picked["full_path"]):
|
||||
raise FileNotFoundError("File not found: {}".format(picked))
|
||||
|
||||
elif not self.valid_mimetype(picked):
|
||||
raise InvalidMimeType("Invalid mime type")
|
||||
|
||||
full_path = picked["full_path"]
|
||||
if not os.path.isfile(full_path):
|
||||
raise FileNotFoundError("File not found: {}".format(media_list))
|
||||
|
||||
media_list = self.upload_media_list([full_path])
|
||||
|
||||
media_list = self.upload_media_list([picked["full_path"]])
|
||||
content_type, message = self.get_post_text(picked, media_list)
|
||||
if self.debug_mode:
|
||||
return picked
|
||||
@ -274,8 +261,6 @@ class YandereBot:
|
||||
try:
|
||||
# Post
|
||||
picked_profile = self.pick_profile()
|
||||
print("Posting...", picked_profile["name"])
|
||||
|
||||
picked = self.download_media(picked_profile)
|
||||
self._post(picked)
|
||||
|
||||
@ -293,7 +278,7 @@ class YandereBot:
|
||||
print("Invalid post:", e)
|
||||
|
||||
# Failed post
|
||||
except (BannedTag, UnknownMimeType, InvalidMimeType, FileNotFoundError) as e:
|
||||
except (InvalidMimeType, FileNotFoundError) as e:
|
||||
# Decrement currentIndexCount to repost from the same profile
|
||||
self.currentIndexCount -= 1
|
||||
print("Posting error:", e)
|
||||
@ -304,6 +289,8 @@ class YandereBot:
|
||||
file_limit_reached = False
|
||||
with contextlib.suppress(IndexError):
|
||||
file_limit_reached = (e.args[1] == 413)
|
||||
if file_limit_reached:
|
||||
self.currentIndexCount -= 1
|
||||
|
||||
print("API Error:", e)
|
||||
|
||||
@ -423,14 +410,6 @@ class InvalidPost(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class BannedTag(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownMimeType(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidMimeType(Exception):
|
||||
pass
|
||||
|
||||
|
Reference in New Issue
Block a user