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