Scheduler refactor

This commit is contained in:
Profitroll 2022-12-10 15:52:34 +01:00
parent 4e8fb00475
commit 09cd36d5ba
2 changed files with 35 additions and 18 deletions

View File

@ -1,18 +0,0 @@
from datetime import datetime
from os import fsdecode, listdir, sep
from modules.utils import configGet, jsonLoad, locale
from dateutil.relativedelta import relativedelta
def check_birthdays(app):
for user_file in listdir(f"{configGet('data', 'locations')}{sep}users{sep}"):
filename = fsdecode(f"{configGet('data', 'locations')}{sep}users{sep}{user_file}")
if filename.endswith(".json"):
user = jsonLoad(filename)
if isinstance(user["application"]["2"], str):
try:
if ".".join([((user["application"]["2"]).split("."))[0], ((user["application"]["2"]).split("."))[1]]) == datetime.now().strftime("%d.%m"):
tg_user = app.get_users(int(user_file.replace(".json", "")))
app.send_message( configGet("admin_group"), locale("birthday", "message").format(str(tg_user.first_name), str(tg_user.username), str(relativedelta(datetime.now(), datetime.strptime(user["application"]["2"], '%d.%m.%Y')).years)) )
except AttributeError:
continue

35
modules/scheduled.py Normal file
View File

@ -0,0 +1,35 @@
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from os import fsdecode, listdir, sep
from app import app
from modules.utils import configGet, jsonLoad, locale, logWrite
from dateutil.relativedelta import relativedelta
from modules.database import col_applications
scheduler = BackgroundScheduler()
# for user_file in listdir(f"{configGet('data', 'locations')}{sep}users{sep}"):
# filename = fsdecode(f"{configGet('data', 'locations')}{sep}users{sep}{user_file}")
# if filename.endswith(".json"):
# user = jsonLoad(filename)
# if isinstance(user["application"]["2"], str):
# try:
# if ".".join([((user["application"]["2"]).split("."))[0], ((user["application"]["2"]).split("."))[1]]) == datetime.now().strftime("%d.%m"):
# tg_user = await app.get_users(int(user_file.replace(".json", "")))
# await app.send_message( configGet("admin_group"), locale("birthday", "message").format(str(tg_user.first_name), str(tg_user.username), str(relativedelta(datetime.now(), datetime.strptime(user["application"]["2"], '%d.%m.%Y')).years)) )
# except AttributeError:
# continue
if configGet("enabled", "scheduler", "birthdays"):
@scheduler.scheduled_job(trigger="cron", hour=configGet("time", "scheduler", "birthdays"))
async def check_birthdays():
for entry in col_applications.find({"2": datetime.now().strftime("%d.%m.%Y")}):
tg_user = await app.get_users(entry["user"])
await app.send_message( configGet("admin_group"), locale("birthday", "message").format(str(tg_user.first_name), str(tg_user.username), str(relativedelta(datetime.now(), datetime.strptime(entry["2"], '%d.%m.%Y')).years)) ) # type: ignore
logWrite(f"Notified admins about {entry['user']}'s birthday")
logWrite("Birthdays check performed")
if configGet("enabled", "scheduler", "sponsorships"):
@scheduler.scheduled_job(trigger="cron", hour=configGet("time", "scheduler", "sponsorships"))
async def check_sponsors():
logWrite("Sponsorships check performed")