Compare commits

..

2 Commits

Author SHA1 Message Date
f831e0e5d9 Added max file size feature 2022-09-01 01:17:02 -07:00
3431f33e80 Removed unused variables 2022-09-01 01:16:30 -07:00
2 changed files with 28 additions and 8 deletions

View File

@ -186,7 +186,7 @@ def main():
arguments = parser.parse_args()
# Redirect stdout when the bot first initializes if the bot is not going to run normally
redirect_stdout = None if arguments.output_hashes or arguments.help else sys.stdout
redirect_stdout = None if arguments.help else sys.stdout
# Yandere Lewd Bot
yandere = None
@ -201,7 +201,7 @@ def main():
raise FailedToLoadCfg
# Flag if the bot is running in debug mode
debug_mode = (arguments.dry_run or arguments.debug or arguments.output_hashes or arguments.help)
debug_mode = (arguments.dry_run or arguments.debug or arguments.help)
with contextlib.redirect_stdout(redirect_stdout):
prime_bot = not arguments.help

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