From 740221df9445532873be51b3ba6fc9642ec24a86 Mon Sep 17 00:00:00 2001 From: Anon Date: Sat, 27 Aug 2022 00:13:30 -0700 Subject: [PATCH] Added time and append functions from yanlib.py --- src/yandereBot.py | 68 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/yandereBot.py b/src/yandereBot.py index 40fa962..c2319bc 100644 --- a/src/yandereBot.py +++ b/src/yandereBot.py @@ -23,6 +23,7 @@ import datetime import contextlib import fnmatch import math +import shutil from threading import Event from mastodon import Mastodon, MastodonIllegalArgumentError, MastodonAPIError, MastodonVersionError @@ -156,7 +157,7 @@ class YandereBot: def blacklist(self, picked): self.lenBlacklist += 1 for f in self.settings_behavior["master_blacklist_w"]: - yanlib.append_file( + append_file( f, picked.get_full_string(), self.settings_behavior["atomic_saving"], self.settings_behavior["sync_save"], @@ -197,18 +198,18 @@ class YandereBot: picked_profile = picked.get_post_setting()["name"] if picked else None picked_next = self.listPictures[0].get_full_string() if self.listPictures else None picked_next_profile = self.listPictures[0].get_post_setting()["name"] if self.listPictures else None - next_selection_seconds = max(0, int(yanlib.time_diff_seconds(date_next_selection, date_selection))) + next_selection_seconds = max(0, int(time_diff_seconds(date_next_selection, date_selection))) n_posts_remain = math.ceil(len(self.listPictures) / self.settings_behavior["uploads_per_post"]) if date_selection != date_next_selection: n_posts_remain -= 1 remaining_seconds = n_posts_remain * self.settings_behavior["sleep_seconds"] - date_end_selection = yanlib.time_add_seconds(date_selection, remaining_seconds + next_selection_seconds) - date_end_selection_seconds = max(0, yanlib.time_diff_seconds(date_end_selection, date_selection)) + date_end_selection = time_add_seconds(date_selection, remaining_seconds + next_selection_seconds) + date_end_selection_seconds = max(0, time_diff_seconds(date_end_selection, date_selection)) if date_selection != date_next_selection and picked is None: date_end_selection_seconds += next_selection_seconds - d, h, m, s = yanlib.humanize_time_delta(date_end_selection_seconds) + d, h, m, s = humanize_time_delta(date_end_selection_seconds) print("[Profile]", picked_profile) print("[Profile Next]", picked_next_profile) @@ -360,11 +361,11 @@ class YandereBot: def schedule_next_post(self): self.dateSelection = self.dateNextSelection - self.dateNextSelection = yanlib.time_add_seconds(self.dateSelection, self.settings_behavior["sleep_seconds"]) + self.dateNextSelection = time_add_seconds(self.dateSelection, self.settings_behavior["sleep_seconds"]) # Will wait between the current time and the time of next selection def wait_future_time(self): - seconds = yanlib.time_diff_seconds(self.dateNextSelection, datetime.datetime.now()) + seconds = time_diff_seconds(self.dateNextSelection, datetime.datetime.now()) self.eventSleep.wait(max(0, seconds)) # [BEGIN THE PROGRAM] @@ -389,8 +390,8 @@ class YandereBot: return 1 start_time = self.dateSelection - delay_seconds = max(yanlib.time_diff_seconds(self.dateNextSelection, start_time) + delay, delay) - delay_time = yanlib.time_add_seconds(start_time, delay_seconds) + delay_seconds = max(time_diff_seconds(self.dateNextSelection, start_time) + delay, delay) + delay_time = time_add_seconds(start_time, delay_seconds) # Print the first image in the list if a delay or pretimer is set if delay_seconds: @@ -464,6 +465,55 @@ def get_list_of_hashes_with_profiles(f_name, profiles, profiles_default, callbac return r +# ------------------------------- TIME FUNCTIONS --------------------------------------------- +def time_add_seconds(dt, seconds): + return dt + datetime.timedelta(0, seconds) + + +def time_diff_seconds(d1, d2): + return (d1-d2).total_seconds() + + +def humanize_time_delta(total_seconds): + m, s = divmod(int(total_seconds), 60) + h, m = divmod(m, 60) + d, h = divmod(h, 24) + return d, h, m, s + +# -------------------------------------------------------------------------------------------- +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