Added max file size feature

This commit is contained in:
Anon 2022-09-01 01:17:02 -07:00
parent 3431f33e80
commit f831e0e5d9

View File

@ -147,7 +147,10 @@ class YandereBot:
username = self.settings_credentials[backend_s]["username"]
password = self.settings_credentials[backend_s]["password"]
img = None
downloader = backend.downloader(username, password, tmp=self.settings_behavior["tmp_dir"])
downloader = backend.downloader(
username, password,
self.settings_behavior["tmp_dir"]
)
img = downloader.fetch_post(picked_profile)
@ -205,6 +208,13 @@ class YandereBot:
return mime_category in ("image", "video")
def valid_file_size(self, picked):
full_path = picked["full_path"]
max_size = self.settings_behavior["max_size"]
file_size = os.stat(full_path).st_size
return file_size <= max_size
def _post(self, picked):
# Validate picked
@ -217,6 +227,9 @@ class YandereBot:
elif not self.valid_mimetype(picked):
raise InvalidMimeType("Invalid mime type")
elif not self.valid_file_size(picked):
raise FileTooLarge("File is too large to upload")
media_list = self.upload_media_list([picked["full_path"]])
content_type, message = self.get_post_text(picked, media_list)
if self.debug_mode:
@ -238,7 +251,7 @@ class YandereBot:
if tag_select == "random":
pick_index = random.randint(0, len(profiles) - 1)
elif tag_select == "sequential":
pick_index = self.currentIndexCount % len(profiles)
pick_index = max(0, self.currentIndexCount % len(profiles))
return profiles[pick_index]
@ -273,11 +286,14 @@ class YandereBot:
except InvalidPost as e:
print("Invalid post:", e)
# Failed post
except (InvalidMimeType, FileNotFoundError) as e:
# Decrement currentIndexCount to repost from the same profile
except (FileTooLarge, InvalidMimeType) as e:
os.remove(picked["full_path"])
self.currentIndexCount -= 1
print("Posting error:", e)
print("Unable to post:", e)
except FileNotFoundError as e:
self.currentIndexCount -= 1
print("File not found:", e)
# Check if the file limit has been reached
except MastodonAPIError as e:
@ -405,6 +421,10 @@ class InvalidPost(Exception):
pass
class FileTooLarge(Exception):
pass
class InvalidMimeType(Exception):
pass