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

View File

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

View File

@ -1,37 +1,37 @@
import os
import random
import shutil
import sys
from os import sep, remove, getpid, listdir
from random import choice
from shutil import move
from sys import argv, exit
from threading import Thread
import time
import traceback
from time import time
from traceback import format_exc
from pathlib import Path
from modules.logging import logWrite
from modules.utils import configGet, jsonLoad, jsonSave, killProc, locale
# Args =====================================================================================================================================
if "--move-sent" in sys.argv:
if "--move-sent" in argv:
for entry in jsonLoad(configGet("index", "locations"))["sent"]:
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:
logWrite(locale("move_sent_doesnt_exist", "console", locale=configGet("locale")).format(entry))
except Exception as exp:
logWrite(locale("move_sent_doesnt_exception", "console", locale=configGet("locale")).format(entry, exp))
logWrite(locale("move_sent_completed", "console", locale=configGet("locale")))
if "--cleanup" in sys.argv:
if "--confirm" in sys.argv:
if "--cleanup" in argv:
if "--confirm" in argv:
index = jsonLoad(configGet("index", "locations"))
for entry in index["sent"]:
try:
try:
os.remove(configGet("queue", "locations")+os.sep+entry)
remove(configGet("queue", "locations")+sep+entry)
except FileNotFoundError:
pass
try:
os.remove(configGet("sent", "locations")+os.sep+entry)
remove(configGet("sent", "locations")+sep+entry)
except FileNotFoundError:
pass
except Exception as exp:
@ -41,8 +41,8 @@ if "--cleanup" in sys.argv:
else:
logWrite(locale("cleanup_unathorized", "console", locale=configGet("locale")))
if "--cleanup-index" in sys.argv:
if "--confirm" in sys.argv:
if "--cleanup-index" in argv:
if "--confirm" in argv:
index = jsonLoad(configGet("index", "locations"))
index["sent"] = []
jsonSave(index, jsonLoad(configGet("index", "locations")))
@ -50,9 +50,9 @@ if "--cleanup-index" in sys.argv:
else:
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")))
sys.exit()
exit()
#===========================================================================================================================================
@ -65,11 +65,11 @@ try:
from pyrogram.raw.functions.stats import GetMessagePublicForwards
except ModuleNotFoundError:
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"))
@ -109,7 +109,7 @@ def send_content():
try:
index = jsonLoad(configGet("index", "locations"))
list_queue = os.listdir(configGet("queue", "locations"))
list_queue = listdir(configGet("queue", "locations"))
for file in list_queue:
@ -136,8 +136,8 @@ def send_content():
list_queue.remove(file)
if len(list_queue) > 0:
candidate_file = random.choice(list_queue)
candidate = configGet("queue", "locations")+os.sep+candidate_file
candidate_file = choice(list_queue)
candidate = configGet("queue", "locations")+sep+candidate_file
else:
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
@ -186,7 +186,7 @@ def send_content():
jsonSave(index, configGet("index", "locations"))
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
@ -196,7 +196,7 @@ def send_content():
])) # type: ignore
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"):
app.send_message(configGet("admin"), locale("post_exception", "message", locale=configGet("locale")).format(exp, traceback.format_exc())) # type: ignore
pass
@ -235,7 +235,7 @@ def cmd_kill(app, msg):
# Submission =====================================================================================================================================
def subLimit(user):
submit = jsonLoad(configGet("submit", "locations"))
submit[str(user.id)] = time.time()
submit[str(user.id)] = time()
jsonSave(submit, configGet("submit", "locations"))
def subLimited(user):
@ -244,7 +244,7 @@ def subLimited(user):
else:
submit = jsonLoad(configGet("submit", "locations"))
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
else:
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)
return
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"):
index = jsonLoad(configGet("index", "locations"))
index["captions"][Path(media).name] = submission.caption
@ -431,7 +431,7 @@ if __name__ == "__main__":
if configGet("submit", "mode"):
# Registering user commands
for entry in os.listdir(configGet("locale", "locations")):
for entry in listdir(configGet("locale", "locations")):
if entry.endswith(".json"):
commands_list = []
for command in configGet("commands"):