soxogvv commited on
Commit
56c5f44
Β·
verified Β·
1 Parent(s): 92bbef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -75,20 +75,15 @@ def apply_today_plan():
75
  continue
76
 
77
  for run_time in plan["times"]:
78
- if run_time.tzinfo is None:
79
- run_time = tz.localize(run_time)
80
- else:
81
- run_time = run_time.astimezone(tz)
82
-
83
- if run_time.date() == now.date() and run_time > now:
84
  scheduler.add_job(
85
  auto_create_groups,
86
  trigger='date',
87
  run_date=run_time,
88
  args=[userbots[plan["user_id"]], acc, plan]
89
  )
90
- elif run_time <= now:
91
- log.warning(f"⏰ Missed scheduled time for {acc['name']}: {run_time}")
92
  def reset_and_plan():
93
  tz = pytz.timezone("Asia/Kolkata")
94
  now = datetime.now(tz)
@@ -100,26 +95,27 @@ def reset_and_plan():
100
  limit = random.randint(3, 5)
101
  times = set()
102
 
103
- minutes_left = int((end_of_day - now).total_seconds() // 60)
104
- if minutes_left <= 0:
105
- log.warning(f"⏳ Not enough time left to schedule for {acc['name']}. Skipping.")
106
- continue
 
 
107
 
108
- while len(times) < limit and len(times) < minutes_left:
109
  random_minutes = random.randint(1, minutes_left)
110
  t = now + timedelta(minutes=random_minutes)
111
- t = t.astimezone(tz) # Ensure timezone-aware
112
  times.add(t)
113
 
114
  schedule_times = sorted(list(times))
115
- plans.insert_one({
116
- "user_id": acc["user_id"],
117
- "times": schedule_times,
118
- "created": 0
119
- })
120
-
121
- log.info(f"πŸ“… Today's plan created between {now.strftime('%Y-%m-%d %H:%M')} and {end_of_day.strftime('%Y-%m-%d %H:%M')}.")
122
-
123
  apply_today_plan()
124
 
125
  try:
@@ -128,7 +124,7 @@ def reset_and_plan():
128
  asyncio.create_task,
129
  bot.send_message(
130
  ADMIN_ID,
131
- f"πŸ—“οΈ Daily schedule reset and updated.\nπŸ•’ Next Reset: {end_of_day.strftime('%Y-%m-%d 00:00:00')}"
132
  )
133
  )
134
  except Exception as e:
@@ -137,27 +133,36 @@ def reset_and_plan():
137
  @bot.on(events.NewMessage(pattern=r'^/stats$'))
138
  async def handle_stats(event):
139
  tz = pytz.timezone("Asia/Kolkata")
 
 
140
  msg = "πŸ“Š **Today's Schedule:**\n\n"
 
 
 
141
  plans_today = list(plans.find())
142
  if not plans_today:
143
  return await event.reply("πŸ“­ No plans found today.")
 
144
  for plan in plans_today:
145
  acc = accounts.find_one({"user_id": plan["user_id"]})
146
  if not acc:
147
  continue
148
  name = acc["name"]
149
- times_str = ", ".join([t.astimezone(tz).strftime("%H:%M") for t in plan["times"]])
150
  created = plan.get("created", 0)
151
  msg += f"πŸ‘€ **{name}** β†’ `{times_str}`\nβœ… Created: `{created}`\n\n"
 
152
  next_run = None
153
  for job in scheduler.get_jobs():
154
  if job.id == "daily_reset_job":
155
  next_run = job.next_run_time
156
  break
157
  if next_run:
158
- msg += f"πŸ•’ Next Reset: `{next_run.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S')}`"
 
159
  await event.reply(msg)
160
 
 
161
  @bot.on(events.NewMessage(pattern=r'^/reset$'))
162
  async def handle_reset(event):
163
  sender = await event.get_sender()
 
75
  continue
76
 
77
  for run_time in plan["times"]:
78
+ run_time = run_time.astimezone(tz)
79
+ if run_time > now:
 
 
 
 
80
  scheduler.add_job(
81
  auto_create_groups,
82
  trigger='date',
83
  run_date=run_time,
84
  args=[userbots[plan["user_id"]], acc, plan]
85
  )
86
+
 
87
  def reset_and_plan():
88
  tz = pytz.timezone("Asia/Kolkata")
89
  now = datetime.now(tz)
 
95
  limit = random.randint(3, 5)
96
  times = set()
97
 
98
+ while len(times) < limit:
99
+ # Always calculate from *latest current time* in loop
100
+ now = datetime.now(tz)
101
+ minutes_left = int((end_of_day - now).total_seconds() // 60)
102
+ if minutes_left <= 1:
103
+ break # Not enough time left
104
 
 
105
  random_minutes = random.randint(1, minutes_left)
106
  t = now + timedelta(minutes=random_minutes)
107
+ t = t.replace(second=0, microsecond=0) # clean seconds
108
  times.add(t)
109
 
110
  schedule_times = sorted(list(times))
111
+ if schedule_times:
112
+ plans.insert_one({
113
+ "user_id": acc["user_id"],
114
+ "times": schedule_times,
115
+ "created": 0
116
+ })
117
+
118
+ log.info(f"πŸ“… Plans created for today between {now.strftime('%Y-%m-%d %I:%M %p')} and {end_of_day.strftime('%I:%M %p')} IST.")
119
  apply_today_plan()
120
 
121
  try:
 
124
  asyncio.create_task,
125
  bot.send_message(
126
  ADMIN_ID,
127
+ f"βœ… Plans reset for today ({now.strftime('%d/%m/%Y')}), all future only.\nNext reset: 00:00 IST"
128
  )
129
  )
130
  except Exception as e:
 
133
  @bot.on(events.NewMessage(pattern=r'^/stats$'))
134
  async def handle_stats(event):
135
  tz = pytz.timezone("Asia/Kolkata")
136
+ now = datetime.now(tz)
137
+
138
  msg = "πŸ“Š **Today's Schedule:**\n\n"
139
+ msg += f"πŸ—“οΈ Date: {now.strftime('%d/%m/%Y')}\n"
140
+ msg += f"πŸ•’ Time: {now.strftime('%I:%M %p')} IST\n\n"
141
+
142
  plans_today = list(plans.find())
143
  if not plans_today:
144
  return await event.reply("πŸ“­ No plans found today.")
145
+
146
  for plan in plans_today:
147
  acc = accounts.find_one({"user_id": plan["user_id"]})
148
  if not acc:
149
  continue
150
  name = acc["name"]
151
+ times_str = ", ".join([t.astimezone(tz).strftime("%I:%M %p") for t in plan["times"]])
152
  created = plan.get("created", 0)
153
  msg += f"πŸ‘€ **{name}** β†’ `{times_str}`\nβœ… Created: `{created}`\n\n"
154
+
155
  next_run = None
156
  for job in scheduler.get_jobs():
157
  if job.id == "daily_reset_job":
158
  next_run = job.next_run_time
159
  break
160
  if next_run:
161
+ msg += f"πŸ•› Next Reset: `{next_run.astimezone(tz).strftime('%d/%m/%Y %I:%M %p')} IST`"
162
+
163
  await event.reply(msg)
164
 
165
+
166
  @bot.on(events.NewMessage(pattern=r'^/reset$'))
167
  async def handle_reset(event):
168
  sender = await event.get_sender()