diff --git a/src/yandere_bot.py b/src/yandere_bot.py index e955bbf..b62640d 100644 --- a/src/yandere_bot.py +++ b/src/yandere_bot.py @@ -72,8 +72,9 @@ class YandereBot: # YandereBot.__init__() # @param cfg A dynamically loaded python module. See yanlib.module_load() for an example + # @param keyfile Keyfile to decrypt settings_post # @param debug_mode Should the bot run in debug mode (do not sign in or post to Pleroma) - # prime_bot Should the bot immediately prime itself (configure picture list and login, but don't post) + # @prime_bot Should the bot immediately prime itself (configure picture list and login, but don't post) def __init__(self, cfg, keyfile=None, debug_mode=False, prime_bot=True): self.cfg = cfg self.load_settings(self.cfg) @@ -156,13 +157,9 @@ class YandereBot: def blacklist(self, picked): self.lenBlacklist += 1 - for f in self.settings_behavior["master_blacklist_w"]: - append_file( - f, picked.get_full_string(), - self.settings_behavior["atomic_saving"], - self.settings_behavior["sync_save"], - self.settings_behavior["tmp_dir"] - ) + for path in self.settings_behavior["master_blacklist_w"]: + with open(path, "w") as f: + print(picked.get_full_string(), file=f) # load_pictures will return a list of YanHashObj() with a blacklist(s) applied # @param list_blacklist A list of HashObjects() that are blacklist hashes @@ -219,6 +216,8 @@ class YandereBot: for path in path_list: if not os.path.isfile(path): raise FileNotFoundError("Could not upload: {}".format(path)) + + for path in path_list: if not self.debug_mode: media = self.mastodon_api.media_post(path, description=os.path.basename(path)) media_dict = { @@ -236,8 +235,10 @@ class YandereBot: string_imglinks = [] if media_list and self.settings_behavior["post_image_link"]: - for ele in media_list: - path, media = ele + for media_dict in media_list: + path = media_dict["path"] + media = media_dict["media"] + if path is None or media is None: continue elif content_type == "text/markdown" and not self.debug_mode: @@ -271,8 +272,7 @@ class YandereBot: return picked # The main post function - # This function is responsible for incrementing self.currentSessionCount, as well as posting and blacklisting the - # picked item. + # This funciton is responsible for picking, posting, and blacklisting an image in listPictures # # It is also responsible for removing the picked item from self.listPictures, which can be accomplished by simply # popping it at index 0. This should handle any error that might occur while posting. @@ -310,15 +310,15 @@ class YandereBot: with contextlib.suppress(IndexError): reinsert_image = e.args[1] != 413 - - # Server Errors - # Assume all exceptions are on the server side + # Server Errors and other general exceptions + # Assume all exceptions are on the server side (besides FileNotFoundError of course) # If the connection is timing out it could be for two reasons: # 1. The error was caused by the user attempting to upload a large file over a slow connection: # a. FIX: Reduce settings_behavior["max_size"] # 2. The server is down. Check to verify in a web browser (this is the default assumption since the # mastodon.py API will not specify why the connection timed out). - # The default assumption is #2 + # 3. Failed to generate screenshot + # 4. Other general exceptions except Exception as e: print("Unhandled Exception:", e) # Exception flags @@ -358,7 +358,8 @@ class YandereBot: # End Conditions: # 1. User presses Ctrl+C # 2. There is nothing left in the picture list to select from - # 3. settings_behavior["uploads_per_post"] is less than one for some reason + # 3. settings_behavior["uploads_per_post"] is less than uploads_per_post + # 4. Consecutive failed uploads is less than max_errors def can_post(self): return ( not self.eventSleep.is_set() and @@ -404,40 +405,6 @@ def get_list_of_hashes_with_profiles(f_name, profiles, profiles_default, callbac return r -# -------------------------------------------------------------------------------------------- -def sync_to_disk(file_handle): - file_handle.flush() - os.fsync(file_handle.fileno()) - - -def append_file(f_name, s, atomic_saving=False, sync_save=False, tmp_dir="/var/tmp"): - file_handle = None - if atomic_saving: - import tempfile - file_handle = tempfile.NamedTemporaryFile(dir=tmp_dir) - with contextlib.suppress(IOError): - with open(f_name, "rb") as src: - shutil.copyfileobj(src, file_handle) - if sync_save: - sync_to_disk(file_handle) - else: - file_handle = open(f_name, "ab") - - file_handle.write(str.encode(s + os.linesep)) - - if sync_save: - sync_to_disk(file_handle) - - if atomic_saving: - file_handle.seek(0) - with open(f_name, "wb") as dst_file: - shutil.copyfileobj(file_handle, dst_file) - if sync_save: - sync_to_disk(dst_file) - - file_handle.close() - - # Custom Exceptions for YandereBot class Debug(Exception): pass