Compare commits
No commits in common. "05a09a2205641b0ab674c0bd0580399b0b87e90d" and "200cb5e6d15d24d3fef2331e00e0e9f829d71997" have entirely different histories.
05a09a2205
...
200cb5e6d1
@ -1,19 +1,3 @@
|
|||||||
# Mirai Nikki Bot a video frame posting bot for Pleroma
|
|
||||||
# Copyright (C) 2022 Anon <yanderefedi@proton.me>
|
|
||||||
#
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
@ -54,11 +38,13 @@ settings_encrypt = {
|
|||||||
|
|
||||||
# Basic settings to configure Yandere Bot's behavior
|
# Basic settings to configure Yandere Bot's behavior
|
||||||
settings_behavior = {
|
settings_behavior = {
|
||||||
"visibility": "unlisted",
|
"visibility": "private",
|
||||||
"feature_set": "pleroma",
|
"feature_set": "pleroma",
|
||||||
"sleep_seconds": 20*60,
|
"sleep_seconds": 20*60,
|
||||||
"uploads_per_post": 1,
|
"uploads_per_post": 1,
|
||||||
"retry_seconds": 15,
|
"retry_seconds": 15,
|
||||||
|
"delete_after_upload": True,
|
||||||
|
"target": None,
|
||||||
"content_type": "text/plain",
|
"content_type": "text/plain",
|
||||||
"content_newline": "\n",
|
"content_newline": "\n",
|
||||||
"post_image_link": False,
|
"post_image_link": False,
|
||||||
|
@ -1,19 +1,3 @@
|
|||||||
# Mirai Nikki Bot a video frame posting bot for Pleroma
|
|
||||||
# Copyright (C) 2022 Anon <yanderefedi@proton.me>
|
|
||||||
#
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
@ -54,11 +38,13 @@ settings_encrypt = {
|
|||||||
|
|
||||||
# Basic settings to configure Yandere Bot's behavior
|
# Basic settings to configure Yandere Bot's behavior
|
||||||
settings_behavior = {
|
settings_behavior = {
|
||||||
"visibility": "unlisted",
|
"visibility": "private",
|
||||||
"feature_set": "pleroma",
|
"feature_set": "pleroma",
|
||||||
"sleep_seconds": 20*60,
|
"sleep_seconds": 20*60,
|
||||||
"uploads_per_post": 1,
|
"uploads_per_post": 1,
|
||||||
"retry_seconds": 15,
|
"retry_seconds": 15,
|
||||||
|
"delete_after_upload": True,
|
||||||
|
"target": None,
|
||||||
"content_type": "text/plain",
|
"content_type": "text/plain",
|
||||||
"content_newline": "\n",
|
"content_newline": "\n",
|
||||||
"post_image_link": False,
|
"post_image_link": False,
|
||||||
|
156
src/create_app.py
Executable file
156
src/create_app.py
Executable file
@ -0,0 +1,156 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
|
# Yandere Lewd Bot, an image posting bot for Pleroma
|
||||||
|
# Copyright (C) 2022 Anon
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import getpass
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import datetime
|
||||||
|
import importlib
|
||||||
|
from mastodon import Mastodon, MastodonIllegalArgumentError, MastodonAPIError, MastodonIOError
|
||||||
|
from collections import OrderedDict
|
||||||
|
import yanlib
|
||||||
|
|
||||||
|
|
||||||
|
class CreateAppError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def get_client_id_and_secret(app, permissions, domain):
|
||||||
|
try:
|
||||||
|
return Mastodon.create_app(app, scopes=permissions, api_base_url=domain)
|
||||||
|
except MastodonIOError:
|
||||||
|
raise CreateAppError("An error occurred. Make sure the domain name is correct.")
|
||||||
|
|
||||||
|
|
||||||
|
def get_api(client_id, client_secret, domain):
|
||||||
|
api = Mastodon(client_id, client_secret, api_base_url=domain)
|
||||||
|
return api
|
||||||
|
|
||||||
|
|
||||||
|
def get_token(api, email, password, permissions):
|
||||||
|
try:
|
||||||
|
token = api.log_in(email, password, scopes=permissions)
|
||||||
|
return token
|
||||||
|
except MastodonIllegalArgumentError:
|
||||||
|
raise CreateAppError("Username or Password is incorrect.")
|
||||||
|
except MastodonAPIError:
|
||||||
|
raise CreateAppError("Could not grant scopes:", ", ".join(permissions))
|
||||||
|
|
||||||
|
|
||||||
|
def get_cfg(cfg_name):
|
||||||
|
try:
|
||||||
|
cfg = importlib.import_module(cfg_name)
|
||||||
|
return cfg
|
||||||
|
except ImportError:
|
||||||
|
raise CreateAppError("Cannot import module:", cfg_name, "Make sure you omitted the .py extension and try again")
|
||||||
|
|
||||||
|
|
||||||
|
def package_settings(app, domain, client_id, client_secret, token):
|
||||||
|
settings_server = OrderedDict([
|
||||||
|
("app_name", app),
|
||||||
|
("api_base_url", domain),
|
||||||
|
("client_id", client_id),
|
||||||
|
("client_secret", client_secret),
|
||||||
|
("access_token", token)
|
||||||
|
])
|
||||||
|
return settings_server
|
||||||
|
|
||||||
|
|
||||||
|
def get_setting_reminder(fmt):
|
||||||
|
dt_now = datetime.datetime.now()
|
||||||
|
dt_now = dt_now.replace(year=dt_now.year + 1)
|
||||||
|
settings_reminder = dt_now.strftime(fmt)
|
||||||
|
return settings_reminder
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Create App
|
||||||
|
print("Generate and register Mastodon App")
|
||||||
|
print("You can just hit enter to accept the default for prompts that have them")
|
||||||
|
print("Ctrl+C to Quit\n")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Get instance information
|
||||||
|
app = input("Enter your app name (Default: app): ") or "app"
|
||||||
|
domain = input("URL of Instance (Default: https://yandere.cc): ") or "https://yandere.cc"
|
||||||
|
email = input("Enter your email: ")
|
||||||
|
password = getpass.getpass("Enter password: ")
|
||||||
|
print("Scopes: read, write, follow, push")
|
||||||
|
print("Separate each scope with a comma (example above).")
|
||||||
|
print("!!! Accept the default unless you intend to modify Yandere Lewd Bot !!!")
|
||||||
|
ans = input("Scopes (Default: write): ") or "write"
|
||||||
|
ans = re.sub(r"\s+", "", ans, flags=re.UNICODE)
|
||||||
|
permissions = ans.split(",")
|
||||||
|
print("Granting:", permissions)
|
||||||
|
|
||||||
|
# Begin logging in
|
||||||
|
client_id, client_secret = get_client_id_and_secret(app, permissions, domain)
|
||||||
|
api = get_api(client_id, client_secret, domain)
|
||||||
|
token = get_token(api, email, password, permissions)
|
||||||
|
|
||||||
|
# Get user setting for settings_time["long_date_format"]
|
||||||
|
# Needed for setting_reminder
|
||||||
|
print("What is the name of your main configuration file? Exclude the '.py' extension.")
|
||||||
|
ans = input("(Default: cfg): ") or "cfg"
|
||||||
|
cfg = get_cfg(ans)
|
||||||
|
|
||||||
|
# Credentials (unencrypted)
|
||||||
|
encrypt, salt = False, ""
|
||||||
|
settings_server = package_settings(app, domain, client_id, client_secret, token)
|
||||||
|
reminder = get_setting_reminder(cfg.settings_time["long_date_format"])
|
||||||
|
|
||||||
|
# Encrypt
|
||||||
|
ans = input("Do you want to encrypt your credentials (y/N)? ")
|
||||||
|
if ans.upper() in ("Y", "YES"):
|
||||||
|
import encryption
|
||||||
|
encrypt = True
|
||||||
|
salt, settings_server = encryption.settings_server_encrypt_cfg(settings_server)
|
||||||
|
|
||||||
|
settings_encrypt = OrderedDict([
|
||||||
|
("encrypt", encrypt),
|
||||||
|
("salt", salt),
|
||||||
|
])
|
||||||
|
|
||||||
|
# Output the user's new credentials
|
||||||
|
print("Copy to your config file!!!")
|
||||||
|
yanlib.pp_ordered_dict("settings_server", settings_server)
|
||||||
|
print('\nsettings_reminder = "{}"\n'.format(reminder))
|
||||||
|
yanlib.pp_dict("settings_encrypt", settings_encrypt)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
except (KeyboardInterrupt, EOFError):
|
||||||
|
print("Quitting...")
|
||||||
|
return 1
|
||||||
|
except CreateAppError as e:
|
||||||
|
print(e)
|
||||||
|
return 2
|
||||||
|
except Exception as e:
|
||||||
|
print("Unhandled Exception!!", e)
|
||||||
|
return 3
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
status_code = main()
|
||||||
|
status_msg = {
|
||||||
|
0: "Success :)",
|
||||||
|
1: "User Quit :|",
|
||||||
|
2: "Error :(",
|
||||||
|
3: "Unhandled Exception ¯\\_(ツ)_/¯"
|
||||||
|
}
|
||||||
|
print(status_msg[status_code])
|
||||||
|
sys.exit(status_code)
|
@ -1,7 +1,7 @@
|
|||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
# Yandere Lewd Bot, an image posting bot for Pleroma
|
# Yandere Lewd Bot, an image posting bot for Pleroma
|
||||||
# Copyright (C) 2022 Anon <yanderefedi@proton.me>
|
# Copyright (C) 2022 Anon
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@ -191,7 +191,7 @@ def settings_server_decrypt_cfg(settings_server, settings_encrypt):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
import argparse
|
import argparse
|
||||||
from pprint import pformat
|
import yanlib
|
||||||
|
|
||||||
default_cfg = "cfg"
|
default_cfg = "cfg"
|
||||||
|
|
||||||
@ -235,8 +235,8 @@ def main():
|
|||||||
("salt", salt)
|
("salt", salt)
|
||||||
])
|
])
|
||||||
|
|
||||||
print("settings_server = {}".format(pformat(settings_server)))
|
yanlib.pp_ordered_dict("settings_server", settings_server)
|
||||||
print("settings_encrypt = {}".format(pformat(settings_encrypt)))
|
yanlib.pp_dict("settings_encrypt", settings_encrypt)
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
# Mirai Nikki Bot a video frame posting bot for Pleroma
|
# Yandere Lewd Bot, an image posting bot for Pleroma
|
||||||
# Copyright (C) 2022 Anon
|
# Copyright (C) 2022 Anon
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
@ -19,6 +19,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import signal
|
import signal
|
||||||
|
import yanlib
|
||||||
import yandereBot
|
import yandereBot
|
||||||
import datetime
|
import datetime
|
||||||
import contextlib
|
import contextlib
|
||||||
@ -84,7 +85,7 @@ class YandereBot(yandereBot.YandereBot):
|
|||||||
hour=t.hour, minute=t.minute, second=t.second, microsecond=t.microsecond
|
hour=t.hour, minute=t.minute, second=t.second, microsecond=t.microsecond
|
||||||
)
|
)
|
||||||
if self.dateNextSelection < self.dateSelection and add_24:
|
if self.dateNextSelection < self.dateSelection and add_24:
|
||||||
self.dateNextSelection = yandereBot.time_add_seconds(self.dateNextSelection, 60 * 60 * 24)
|
self.dateNextSelection = yanlib.time_add_seconds(self.dateNextSelection, 60 * 60 * 24)
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Invalid time format: {}\n\nCorrect date/time format examples:".format(h))
|
print("Invalid time format: {}\n\nCorrect date/time format examples:".format(h))
|
||||||
self.print_date_time_example()
|
self.print_date_time_example()
|
||||||
@ -101,7 +102,7 @@ class YandereBot(yandereBot.YandereBot):
|
|||||||
# Check for potential misconfigurations by the user
|
# Check for potential misconfigurations by the user
|
||||||
def pass_sanity_test(self):
|
def pass_sanity_test(self):
|
||||||
# Calculate pre-timer value
|
# Calculate pre-timer value
|
||||||
seconds_until_next_pos = yandereBot.time_diff_seconds(self.dateNextSelection, self.dateSelection)
|
seconds_until_next_pos = yanlib.time_diff_seconds(self.dateNextSelection, self.dateSelection)
|
||||||
|
|
||||||
# Possible misconfigurations that will prompt the user to continue
|
# Possible misconfigurations that will prompt the user to continue
|
||||||
pretimer_less_than_zero = seconds_until_next_pos < 0
|
pretimer_less_than_zero = seconds_until_next_pos < 0
|
||||||
|
@ -1,20 +1,6 @@
|
|||||||
#! /usr/bin/env sh
|
#! /usr/bin/env sh
|
||||||
|
|
||||||
# Mirai Nikki Bot a video frame posting bot for Pleroma
|
# src/render_script.sh
|
||||||
# Copyright (C) 2022 Anon
|
|
||||||
#
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
# ISSUES:
|
# ISSUES:
|
||||||
# - https://github.com/mpv-player/mpv/issues/5498
|
# - https://github.com/mpv-player/mpv/issues/5498
|
||||||
|
@ -1,20 +1,6 @@
|
|||||||
#! /usr/bin/env sh
|
#! /usr/bin/env sh
|
||||||
|
|
||||||
# Mirai Nikki Bot a video frame posting bot for Pleroma
|
# src/render_script.sh
|
||||||
# Copyright (C) 2022 Anon
|
|
||||||
#
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
# ISSUES:
|
# ISSUES:
|
||||||
# - https://github.com/mpv-player/mpv/issues/5498
|
# - https://github.com/mpv-player/mpv/issues/5498
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import yanlib
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
import contextlib
|
import contextlib
|
||||||
@ -135,7 +137,7 @@ class YandereBot:
|
|||||||
dateNextSelection = None
|
dateNextSelection = None
|
||||||
|
|
||||||
# YandereBot.__init__()
|
# YandereBot.__init__()
|
||||||
# @param cfg A dynamically loaded python module
|
# @param cfg A dynamically loaded python module. See yanlib.module_load() for an example
|
||||||
# @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, debug_mode=False, prime_bot=True):
|
def __init__(self, cfg, debug_mode=False, prime_bot=True):
|
||||||
@ -207,7 +209,7 @@ class YandereBot:
|
|||||||
profile, frame, nsfw
|
profile, frame, nsfw
|
||||||
))
|
))
|
||||||
print("Path:", path)
|
print("Path:", path)
|
||||||
next_selection_seconds = max(0, int(time_diff_seconds(date_next_selection, date_selection)))
|
next_selection_seconds = max(0, int(yanlib.time_diff_seconds(date_next_selection, date_selection)))
|
||||||
print("Selection time: {}".format(
|
print("Selection time: {}".format(
|
||||||
date_selection.strftime(self.settings_time["long_date_format"])) )
|
date_selection.strftime(self.settings_time["long_date_format"])) )
|
||||||
print("Next selection time: {} ({} seconds)".format(
|
print("Next selection time: {} ({} seconds)".format(
|
||||||
@ -342,11 +344,11 @@ class YandereBot:
|
|||||||
|
|
||||||
def schedule_next_post(self):
|
def schedule_next_post(self):
|
||||||
self.dateSelection = self.dateNextSelection
|
self.dateSelection = self.dateNextSelection
|
||||||
self.dateNextSelection = time_add_seconds(self.dateSelection, self.settings_behavior["sleep_seconds"])
|
self.dateNextSelection = yanlib.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 = time_diff_seconds(self.dateNextSelection, datetime.datetime.now())
|
seconds = yanlib.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]
|
||||||
@ -368,8 +370,8 @@ class YandereBot:
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
start_time = self.dateSelection
|
start_time = self.dateSelection
|
||||||
delay_seconds = max(time_diff_seconds(self.dateNextSelection, start_time) + delay, delay)
|
delay_seconds = max(yanlib.time_diff_seconds(self.dateNextSelection, start_time) + delay, delay)
|
||||||
delay_time = time_add_seconds(start_time, delay_seconds)
|
delay_time = yanlib.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:
|
||||||
@ -414,14 +416,6 @@ class YandereBot:
|
|||||||
if self.can_post():
|
if self.can_post():
|
||||||
self.wait_future_time()
|
self.wait_future_time()
|
||||||
|
|
||||||
# ------------------------------- 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()
|
|
||||||
|
|
||||||
|
|
||||||
# Custom Exceptions for YandereBot
|
# Custom Exceptions for YandereBot
|
||||||
class Debug(Exception):
|
class Debug(Exception):
|
||||||
|
90
src/yanlib.py
Normal file
90
src/yanlib.py
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
|
# Yandere Lewd Bot, an image posting bot for Pleroma
|
||||||
|
# Copyright (C) 2022 Anon
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# yanlib.py is required by yandereBot.py
|
||||||
|
# This file contains functions and utilities that may be useful to external tools and programs that interface with
|
||||||
|
# yandereBot, or manipulate hash files in some way. Typically instantiating a yandereBot object is unnecessary for this.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import datetime
|
||||||
|
import shutil
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------- 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 input_time_format(s):
|
||||||
|
return s.replace("-", "")
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------------- Pretty Print Functions ------------------------------------
|
||||||
|
# If value is a subclass of string, return it in parentheses (used for generating a python configuration file)
|
||||||
|
def _pp_str(v):
|
||||||
|
if issubclass(str, type(v)):
|
||||||
|
return '"{}"'.format(v)
|
||||||
|
else:
|
||||||
|
return "{}".format(v)
|
||||||
|
|
||||||
|
|
||||||
|
# Prints out a dictionary in pretty print form as valid python code.
|
||||||
|
# setting_dict should consist of string formats with key value pairs defined in setting_dict ex. '{key} {value}'.
|
||||||
|
def _pp_dict(head, setting_dict, fmt, tail):
|
||||||
|
if head:
|
||||||
|
print(head)
|
||||||
|
last_index = len(setting_dict) - 1
|
||||||
|
# i, k -> index and key value of setting_dict
|
||||||
|
for i, k in enumerate(setting_dict):
|
||||||
|
_fmt = fmt
|
||||||
|
_k = _pp_str(k)
|
||||||
|
_v = _pp_str(setting_dict[k])
|
||||||
|
# Add comma separator on all but the last value setting
|
||||||
|
if i < last_index:
|
||||||
|
_fmt += ","
|
||||||
|
print(_fmt.format(_k, _v))
|
||||||
|
if tail:
|
||||||
|
print(tail)
|
||||||
|
|
||||||
|
|
||||||
|
def pp_ordered_dict(setting, setting_dict):
|
||||||
|
_pp_dict(
|
||||||
|
"{} = OrderedDict([".format(setting),
|
||||||
|
setting_dict,
|
||||||
|
'\t({},\t{})',
|
||||||
|
"])")
|
||||||
|
|
||||||
|
|
||||||
|
def pp_dict(setting, setting_dict):
|
||||||
|
_pp_dict(
|
||||||
|
"{} = {{".format(setting),
|
||||||
|
setting_dict,
|
||||||
|
'\t{}:\t{}',
|
||||||
|
"}")
|
Reference in New Issue
Block a user