Added time and append functions from yanlib.py

This commit is contained in:
Anon 2022-08-27 00:13:30 -07:00
parent 521087cbe8
commit 740221df94

View File

@ -23,6 +23,7 @@ import datetime
import contextlib import contextlib
import fnmatch import fnmatch
import math import math
import shutil
from threading import Event from threading import Event
from mastodon import Mastodon, MastodonIllegalArgumentError, MastodonAPIError, MastodonVersionError from mastodon import Mastodon, MastodonIllegalArgumentError, MastodonAPIError, MastodonVersionError
@ -156,7 +157,7 @@ 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 f in self.settings_behavior["master_blacklist_w"]:
yanlib.append_file( append_file(
f, picked.get_full_string(), f, picked.get_full_string(),
self.settings_behavior["atomic_saving"], self.settings_behavior["atomic_saving"],
self.settings_behavior["sync_save"], self.settings_behavior["sync_save"],
@ -197,18 +198,18 @@ class YandereBot:
picked_profile = picked.get_post_setting()["name"] if picked else None 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 = 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 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"]) n_posts_remain = math.ceil(len(self.listPictures) / self.settings_behavior["uploads_per_post"])
if date_selection != date_next_selection: if date_selection != date_next_selection:
n_posts_remain -= 1 n_posts_remain -= 1
remaining_seconds = n_posts_remain * self.settings_behavior["sleep_seconds"] 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 = 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_seconds = max(0, time_diff_seconds(date_end_selection, date_selection))
if date_selection != date_next_selection and picked is None: if date_selection != date_next_selection and picked is None:
date_end_selection_seconds += next_selection_seconds 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]", picked_profile)
print("[Profile Next]", picked_next_profile) print("[Profile Next]", picked_next_profile)
@ -360,11 +361,11 @@ class YandereBot:
def schedule_next_post(self): def schedule_next_post(self):
self.dateSelection = self.dateNextSelection 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 # Will wait between the current time and the time of next selection
def wait_future_time(self): 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)) self.eventSleep.wait(max(0, seconds))
# [BEGIN THE PROGRAM] # [BEGIN THE PROGRAM]
@ -389,8 +390,8 @@ class YandereBot:
return 1 return 1
start_time = self.dateSelection start_time = self.dateSelection
delay_seconds = max(yanlib.time_diff_seconds(self.dateNextSelection, start_time) + delay, delay) delay_seconds = max(time_diff_seconds(self.dateNextSelection, start_time) + delay, delay)
delay_time = yanlib.time_add_seconds(start_time, delay_seconds) delay_time = time_add_seconds(start_time, delay_seconds)
# Print the first image in the list if a delay or pretimer is set # Print the first image in the list if a delay or pretimer is set
if delay_seconds: if delay_seconds:
@ -464,6 +465,55 @@ def get_list_of_hashes_with_profiles(f_name, profiles, profiles_default, callbac
return r 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 # Custom Exceptions for YandereBot
class Debug(Exception): class Debug(Exception):
pass pass