Update app.py
Browse files
app.py
CHANGED
|
@@ -74,11 +74,16 @@ def create_plan(account, limit):
|
|
| 74 |
def apply_plan():
|
| 75 |
now = datetime.now(tz)
|
| 76 |
count = 0
|
|
|
|
| 77 |
for plan in plans.find():
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
account = accounts.find_one({"_id": plan["account_id"]})
|
| 81 |
if not account:
|
|
|
|
| 82 |
continue
|
| 83 |
|
| 84 |
userbot = userbots.get(account["user_id"])
|
|
@@ -86,19 +91,27 @@ def apply_plan():
|
|
| 86 |
print(f"β οΈ No userbot for account {account['_id']}, skipping...")
|
| 87 |
continue
|
| 88 |
|
|
|
|
|
|
|
|
|
|
| 89 |
job_id = f"job_{plan['_id']}"
|
| 90 |
scheduler.add_job(
|
| 91 |
-
|
| 92 |
-
auto_create_groups(ub, acc, pl)
|
| 93 |
-
),
|
| 94 |
trigger='date',
|
| 95 |
run_date=plan_time,
|
| 96 |
-
id=job_id
|
|
|
|
| 97 |
)
|
|
|
|
| 98 |
print(f"π Scheduled job for account {account['_id']} at {plan_time.strftime('%H:%M:%S')} (ID: {job_id})")
|
| 99 |
count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
print(f"β
Applied and scheduled {count} job(s).")
|
| 101 |
|
|
|
|
| 102 |
def reset_and_plan():
|
| 103 |
print("π Resetting plans and scheduling new ones...")
|
| 104 |
plans.delete_many({})
|
|
|
|
| 74 |
def apply_plan():
|
| 75 |
now = datetime.now(tz)
|
| 76 |
count = 0
|
| 77 |
+
|
| 78 |
for plan in plans.find():
|
| 79 |
+
try:
|
| 80 |
+
plan_time = datetime.fromisoformat(plan["scheduled_time"]).astimezone(tz)
|
| 81 |
+
if plan_time <= now:
|
| 82 |
+
continue # Skip past plans
|
| 83 |
+
|
| 84 |
account = accounts.find_one({"_id": plan["account_id"]})
|
| 85 |
if not account:
|
| 86 |
+
print(f"β οΈ No account found for plan {plan['_id']}")
|
| 87 |
continue
|
| 88 |
|
| 89 |
userbot = userbots.get(account["user_id"])
|
|
|
|
| 91 |
print(f"β οΈ No userbot for account {account['_id']}, skipping...")
|
| 92 |
continue
|
| 93 |
|
| 94 |
+
async def job_wrapper(ub=userbot, acc=account, pl=plan):
|
| 95 |
+
await auto_create_groups(ub, acc, pl)
|
| 96 |
+
|
| 97 |
job_id = f"job_{plan['_id']}"
|
| 98 |
scheduler.add_job(
|
| 99 |
+
job_wrapper,
|
|
|
|
|
|
|
| 100 |
trigger='date',
|
| 101 |
run_date=plan_time,
|
| 102 |
+
id=job_id,
|
| 103 |
+
name=f"AutoGroupPlan_{account['_id']}"
|
| 104 |
)
|
| 105 |
+
|
| 106 |
print(f"π Scheduled job for account {account['_id']} at {plan_time.strftime('%H:%M:%S')} (ID: {job_id})")
|
| 107 |
count += 1
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"β Error scheduling plan {plan.get('_id')}: {e}")
|
| 111 |
+
|
| 112 |
print(f"β
Applied and scheduled {count} job(s).")
|
| 113 |
|
| 114 |
+
|
| 115 |
def reset_and_plan():
|
| 116 |
print("π Resetting plans and scheduling new ones...")
|
| 117 |
plans.delete_many({})
|