Imports were a bit optimized

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

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"):