Imports were a bit optimized

This commit is contained in:
Profitroll 2022-08-31 14:31:24 +02:00
parent 6e1de38da0
commit 58c5429e5c
3 changed files with 50 additions and 46 deletions

View File

@ -1,12 +1,16 @@
import os try:
import gzip from ujson import loads
import json except ModuleNotFoundError:
import shutil from json import loads
from os import stat, makedirs, path, getcwd
from gzip import open as gzipopen
from shutil import copyfileobj
from datetime import datetime from datetime import datetime
with open(os.getcwd()+os.path.sep+"config.json", "r", encoding='utf8') as file: with open(getcwd()+path.sep+"config.json", "r", encoding='utf8') as file:
json_contents = json.loads(file.read()) json_contents = loads(file.read())
log_size = json_contents["logging"]["size"] log_size = json_contents["logging"]["size"]
log_folder = json_contents["logging"]["location"] log_folder = json_contents["logging"]["location"]
file.close() file.close()
@ -22,16 +26,16 @@ def checkSize(debug=False):
log_file = "latest.log" log_file = "latest.log"
try: try:
os.makedirs(log_folder, exist_ok=True) makedirs(log_folder, exist_ok=True)
log = os.stat(os.path.join(log_folder, log_file)) log = stat(path.join(log_folder, log_file))
if (log.st_size / 1024) > log_size: if (log.st_size / 1024) > log_size:
with open(os.path.join(log_folder, log_file), 'rb') as f_in: with open(path.join(log_folder, log_file), 'rb') as f_in:
with gzip.open(os.path.join(log_folder, f'{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz'), 'wb') as f_out: with gzipopen(path.join(log_folder, f'{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz'), 'wb') as f_out:
shutil.copyfileobj(f_in, f_out) copyfileobj(f_in, f_out)
print(f'Copied {os.path.join(log_folder, datetime.now().strftime("%d.%m.%Y_%H:%M:%S"))}.log.gz') print(f'Copied {path.join(log_folder, datetime.now().strftime("%d.%m.%Y_%H:%M:%S"))}.log.gz')
open(os.path.join(log_folder, log_file), 'w').close() open(path.join(log_folder, log_file), 'w').close()
except FileNotFoundError: except FileNotFoundError:
print(f'Log file {os.path.join(log_folder, log_file)} does not exist') print(f'Log file {path.join(log_folder, log_file)} does not exist')
pass pass
# Append string to log # Append string to log
@ -47,7 +51,7 @@ def logAppend(message, debug=False):
else: else:
log_file = "latest.log" log_file = "latest.log"
log = open(os.path.join(log_folder, log_file), 'a') log = open(path.join(log_folder, log_file), 'a')
log.write(f'{message_formatted}\n') log.write(f'{message_formatted}\n')
log.close() log.close()

View File

@ -1,9 +1,9 @@
try: try:
import ujson as json
from ujson import JSONDecodeError as JSONDecodeError from ujson import JSONDecodeError as JSONDecodeError
from ujson import loads, dumps
except ModuleNotFoundError: except ModuleNotFoundError:
import json
from json import JSONDecodeError as JSONDecodeError from json import JSONDecodeError as JSONDecodeError
from json import loads, dumps
import os import os
import sys import sys
@ -16,7 +16,7 @@ def jsonLoad(filename):
"""Loads arg1 as json and returns its contents""" """Loads arg1 as json and returns its contents"""
with open(filename, "r", encoding='utf8') as file: with open(filename, "r", encoding='utf8') as file:
try: try:
output = json.loads(file.read()) output = loads(file.read())
except JSONDecodeError: except JSONDecodeError:
logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{traceback.print_exc()}") logWrite(f"Could not load json file {filename}: file seems to be incorrect!\n{traceback.print_exc()}")
raise raise
@ -30,7 +30,7 @@ def jsonSave(contents, filename):
"""Dumps dict/list arg1 to file arg2""" """Dumps dict/list arg1 to file arg2"""
try: try:
with open(filename, "w", encoding='utf8') as file: with open(filename, "w", encoding='utf8') as file:
file.write(json.dumps(contents, ensure_ascii=False, indent=4)) file.write(dumps(contents, ensure_ascii=False, indent=4))
file.close() file.close()
except Exception as exp: except Exception as exp:
logWrite(f"Could not save json file {filename}: {exp}\n{traceback.print_exc()}") logWrite(f"Could not save json file {filename}: {exp}\n{traceback.print_exc()}")

View File

@ -1,37 +1,37 @@
import os from os import sep, remove, getpid, listdir
import random from random import choice
import shutil from shutil import move
import sys from sys import argv, exit
from threading import Thread from threading import Thread
import time from time import time
import traceback from traceback import format_exc
from pathlib import Path from pathlib import Path
from modules.logging import logWrite from modules.logging import logWrite
from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale
# Args ===================================================================================================================================== # Args =====================================================================================================================================
if "--move-sent" in sys.argv: if "--move-sent" in argv:
for entry in jsonLoad(configGet("index", "locations"))["sent"]: for entry in jsonLoad(configGet("index", "locations"))["sent"]:
try: try:
shutil.move(configGet("queue", "locations")+os.sep+entry, configGet("sent", "locations")+os.sep+entry) move(configGet("queue", "locations")+sep+entry, configGet("sent", "locations")+sep+entry)
except FileNotFoundError: except FileNotFoundError:
logWrite(locale("move_sent_doesnt_exist", "console", locale=configGet("locale")).format(entry)) logWrite(locale("move_sent_doesnt_exist", "console", locale=configGet("locale")).format(entry))
except Exception as exp: except Exception as exp:
logWrite(locale("move_sent_doesnt_exception", "console", locale=configGet("locale")).format(entry, exp)) logWrite(locale("move_sent_doesnt_exception", "console", locale=configGet("locale")).format(entry, exp))
logWrite(locale("move_sent_completed", "console", locale=configGet("locale"))) logWrite(locale("move_sent_completed", "console", locale=configGet("locale")))
if "--cleanup" in sys.argv: if "--cleanup" in argv:
if "--confirm" in sys.argv: if "--confirm" in argv:
index = jsonLoad(configGet("index", "locations")) index = jsonLoad(configGet("index", "locations"))
for entry in index["sent"]: for entry in index["sent"]:
try: try:
try: try:
os.remove(configGet("queue", "locations")+os.sep+entry) remove(configGet("queue", "locations")+sep+entry)
except FileNotFoundError: except FileNotFoundError:
pass pass
try: try:
os.remove(configGet("sent", "locations")+os.sep+entry) remove(configGet("sent", "locations")+sep+entry)
except FileNotFoundError: except FileNotFoundError:
pass pass
except Exception as exp: except Exception as exp:
@ -41,8 +41,8 @@ if "--cleanup" in sys.argv:
else: else:
logWrite(locale("cleanup_unathorized", "console", locale=configGet("locale"))) logWrite(locale("cleanup_unathorized", "console", locale=configGet("locale")))
if "--cleanup-index" in sys.argv: if "--cleanup-index" in argv:
if "--confirm" in sys.argv: if "--confirm" in argv:
index = jsonLoad(configGet("index", "locations")) index = jsonLoad(configGet("index", "locations"))
index["sent"] = [] index["sent"] = []
jsonSave(index, jsonLoad(configGet("index", "locations"))) jsonSave(index, jsonLoad(configGet("index", "locations")))
@ -50,9 +50,9 @@ if "--cleanup-index" in sys.argv:
else: else:
logWrite(locale("cleanup_index_unathorized", "console", locale=configGet("locale"))) logWrite(locale("cleanup_index_unathorized", "console", locale=configGet("locale")))
if "--norun" in sys.argv: if "--norun" in argv:
logWrite(locale("passed_norun", "console", locale=configGet("locale"))) logWrite(locale("passed_norun", "console", locale=configGet("locale")))
sys.exit() exit()
#=========================================================================================================================================== #===========================================================================================================================================
@ -65,11 +65,11 @@ try:
from pyrogram.raw.functions.stats import GetMessagePublicForwards from pyrogram.raw.functions.stats import GetMessagePublicForwards
except ModuleNotFoundError: except ModuleNotFoundError:
print(locale("deps_missing", "console", locale=configGet("locale")), flush=True) print(locale("deps_missing", "console", locale=configGet("locale")), flush=True)
sys.exit() exit()
#=========================================================================================================================================== #===========================================================================================================================================
pid = os.getpid() pid = getpid()
app = Client("duptsiaposter", bot_token=configGet("bot_token", "bot"), api_id=configGet("api_id", "bot"), api_hash=configGet("api_hash", "bot")) app = Client("duptsiaposter", bot_token=configGet("bot_token", "bot"), api_id=configGet("api_id", "bot"), api_hash=configGet("api_hash", "bot"))
@ -109,7 +109,7 @@ def send_content():
try: try:
index = jsonLoad(configGet("index", "locations")) index = jsonLoad(configGet("index", "locations"))
list_queue = os.listdir(configGet("queue", "locations")) list_queue = listdir(configGet("queue", "locations"))
for file in list_queue: for file in list_queue:
@ -136,8 +136,8 @@ def send_content():
list_queue.remove(file) list_queue.remove(file)
if len(list_queue) > 0: if len(list_queue) > 0:
candidate_file = random.choice(list_queue) candidate_file = choice(list_queue)
candidate = configGet("queue", "locations")+os.sep+candidate_file candidate = configGet("queue", "locations")+sep+candidate_file
else: else:
logWrite(locale("post_empty", "console", locale=configGet("locale"))) logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"): if configGet("error", "reports"):
@ -186,7 +186,7 @@ def send_content():
jsonSave(index, configGet("index", "locations")) jsonSave(index, configGet("index", "locations"))
if configGet("move_sent", "posting"): if configGet("move_sent", "posting"):
shutil.move(candidate, configGet("sent", "locations")+os.sep+candidate_file) move(candidate, configGet("sent", "locations")+sep+candidate_file)
logWrite(locale("post_sent", "console", locale=configGet("locale")).format(candidate, ext_type, str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting")))) # type: ignore logWrite(locale("post_sent", "console", locale=configGet("locale")).format(candidate, ext_type, str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting")))) # type: ignore
@ -196,7 +196,7 @@ def send_content():
])) # type: ignore ])) # type: ignore
except Exception as exp: except Exception as exp:
logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), traceback.format_exc())) logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc()))
if configGet("error", "reports"): if configGet("error", "reports"):
app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, traceback.format_exc())) # type: ignore app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, traceback.format_exc())) # type: ignore
pass pass
@ -235,7 +235,7 @@ def cmd_kill(app, msg):
# Submission ===================================================================================================================================== # Submission =====================================================================================================================================
def subLimit(user): def subLimit(user):
submit = jsonLoad(configGet("submit", "locations")) submit = jsonLoad(configGet("submit", "locations"))
submit[str(user.id)] = time.time() submit[str(user.id)] = time()
jsonSave(submit, configGet("submit", "locations")) jsonSave(submit, configGet("submit", "locations"))
def subLimited(user): def subLimited(user):
@ -244,7 +244,7 @@ def subLimited(user):
else: else:
submit = jsonLoad(configGet("submit", "locations")) submit = jsonLoad(configGet("submit", "locations"))
if str(user.id) in submit: if str(user.id) in submit:
if (time.time() - submit[str(user.id)]) < configGet("timeout", "submission"): if (time() - submit[str(user.id)]) < configGet("timeout", "submission"):
return True return True
else: else:
return False return False
@ -345,7 +345,7 @@ def callback_query_yes(app, clb): # type: ignore
clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True)
return return
try: try:
media = app.download_media(submission, file_name=configGet("queue", "locations")+os.sep) media = app.download_media(submission, file_name=configGet("queue", "locations")+sep)
if clb.data.endswith("_caption"): if clb.data.endswith("_caption"):
index = jsonLoad(configGet("index", "locations")) index = jsonLoad(configGet("index", "locations"))
index["captions"][Path(media).name] = submission.caption index["captions"][Path(media).name] = submission.caption
@ -431,7 +431,7 @@ if __name__ == "__main__":
if configGet("submit", "mode"): if configGet("submit", "mode"):
# Registering user commands # Registering user commands
for entry in os.listdir(configGet("locale", "locations")): for entry in listdir(configGet("locale", "locations")):
if entry.endswith(".json"): if entry.endswith(".json"):
commands_list = [] commands_list = []
for command in configGet("commands"): for command in configGet("commands"):