soxogvv commited on
Commit
92bbef7
·
verified ·
1 Parent(s): 8fd0fdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -10
app.py CHANGED
@@ -77,6 +77,8 @@ def apply_today_plan():
77
  for run_time in plan["times"]:
78
  if run_time.tzinfo is None:
79
  run_time = tz.localize(run_time)
 
 
80
 
81
  if run_time.date() == now.date() and run_time > now:
82
  scheduler.add_job(
@@ -87,12 +89,10 @@ def apply_today_plan():
87
  )
88
  elif run_time <= now:
89
  log.warning(f"⏰ Missed scheduled time for {acc['name']}: {run_time}")
90
-
91
-
92
  def reset_and_plan():
93
  tz = pytz.timezone("Asia/Kolkata")
94
- today = datetime.now(tz).replace(hour=0, minute=0, second=0, microsecond=0)
95
- end_of_day = today.replace(hour=23, minute=59, second=59, microsecond=0)
96
 
97
  plans.delete_many({})
98
 
@@ -100,15 +100,25 @@ def reset_and_plan():
100
  limit = random.randint(3, 5)
101
  times = set()
102
 
103
- while len(times) < limit:
104
- random_minutes = random.randint(0, 23 * 60 + 59)
105
- t = today + timedelta(minutes=random_minutes)
 
 
 
 
 
 
106
  times.add(t)
107
 
108
  schedule_times = sorted(list(times))
109
- plans.insert_one({"user_id": acc["user_id"], "times": schedule_times, "created": 0})
 
 
 
 
110
 
111
- log.info(f"📅 Today's plan created between {today.strftime('%Y-%m-%d %H:%M')} and {end_of_day.strftime('%Y-%m-%d %H:%M')}.")
112
 
113
  apply_today_plan()
114
 
@@ -116,7 +126,10 @@ def reset_and_plan():
116
  loop = asyncio.get_event_loop()
117
  loop.call_soon_threadsafe(
118
  asyncio.create_task,
119
- bot.send_message(ADMIN_ID, "🗓️ Daily schedule reset and updated.")
 
 
 
120
  )
121
  except Exception as e:
122
  log.warning(f"⚠️ Failed to notify admin: {e}")
 
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(
 
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)
95
+ end_of_day = now.replace(hour=23, minute=59, second=59, microsecond=0)
96
 
97
  plans.delete_many({})
98
 
 
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
 
 
126
  loop = asyncio.get_event_loop()
127
  loop.call_soon_threadsafe(
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:
135
  log.warning(f"⚠️ Failed to notify admin: {e}")