Fixed link text bug

This commit is contained in:
Anon 2022-10-13 14:58:02 -07:00
parent fb72004f42
commit c9a65fd993

View File

@ -72,8 +72,9 @@ class YandereBot:
# YandereBot.__init__() # YandereBot.__init__()
# @param cfg A dynamically loaded python module. See yanlib.module_load() for an example # @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) # @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): def __init__(self, cfg, keyfile=None, debug_mode=False, prime_bot=True):
self.cfg = cfg self.cfg = cfg
self.load_settings(self.cfg) self.load_settings(self.cfg)
@ -156,13 +157,9 @@ class YandereBot:
def blacklist(self, picked): def blacklist(self, picked):
self.lenBlacklist += 1 self.lenBlacklist += 1
for f in self.settings_behavior["master_blacklist_w"]: for path in self.settings_behavior["master_blacklist_w"]:
append_file( with open(path, "w") as f:
f, picked.get_full_string(), print(picked.get_full_string(), file=f)
self.settings_behavior["atomic_saving"],
self.settings_behavior["sync_save"],
self.settings_behavior["tmp_dir"]
)
# load_pictures will return a list of YanHashObj() with a blacklist(s) applied # load_pictures will return a list of YanHashObj() with a blacklist(s) applied
# @param list_blacklist A list of HashObjects() that are blacklist hashes # @param list_blacklist A list of HashObjects() that are blacklist hashes
@ -219,6 +216,8 @@ class YandereBot:
for path in path_list: for path in path_list:
if not os.path.isfile(path): if not os.path.isfile(path):
raise FileNotFoundError("Could not upload: {}".format(path)) raise FileNotFoundError("Could not upload: {}".format(path))
for path in path_list:
if not self.debug_mode: if not self.debug_mode:
media = self.mastodon_api.media_post(path, description=os.path.basename(path)) media = self.mastodon_api.media_post(path, description=os.path.basename(path))
media_dict = { media_dict = {
@ -236,8 +235,10 @@ class YandereBot:
string_imglinks = [] string_imglinks = []
if media_list and self.settings_behavior["post_image_link"]: if media_list and self.settings_behavior["post_image_link"]:
for ele in media_list: for media_dict in media_list:
path, media = ele path = media_dict["path"]
media = media_dict["media"]
if path is None or media is None: if path is None or media is None:
continue continue
elif content_type == "text/markdown" and not self.debug_mode: elif content_type == "text/markdown" and not self.debug_mode:
@ -271,8 +272,7 @@ class YandereBot:
return picked return picked
# The main post function # The main post function
# This function is responsible for incrementing self.currentSessionCount, as well as posting and blacklisting the # This funciton is responsible for picking, posting, and blacklisting an image in listPictures
# picked item.
# #
# It is also responsible for removing the picked item from self.listPictures, which can be accomplished by simply # 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. # 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): with contextlib.suppress(IndexError):
reinsert_image = e.args[1] != 413 reinsert_image = e.args[1] != 413
# Server Errors and other general exceptions
# Server Errors # Assume all exceptions are on the server side (besides FileNotFoundError of course)
# Assume all exceptions are on the server side
# If the connection is timing out it could be for two reasons: # 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: # 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"] # 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 # 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). # 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: except Exception as e:
print("Unhandled Exception:", e) print("Unhandled Exception:", e)
# Exception flags # Exception flags
@ -358,7 +358,8 @@ class YandereBot:
# End Conditions: # End Conditions:
# 1. User presses Ctrl+C # 1. User presses Ctrl+C
# 2. There is nothing left in the picture list to select from # 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): def can_post(self):
return ( return (
not self.eventSleep.is_set() and 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 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 # Custom Exceptions for YandereBot
class Debug(Exception): class Debug(Exception):
pass pass