understanding commited on
Commit
5f5f7a2
·
verified ·
1 Parent(s): 7c31ea8

Update bot/handlers.py

Browse files
Files changed (1) hide show
  1. bot/handlers.py +57 -11
bot/handlers.py CHANGED
@@ -174,9 +174,9 @@ def setup_handlers(app: Client) -> None:
174
  if not j.get("ok"):
175
  return await q.answer("Failed", show_alert=True)
176
 
177
- await q.answer("Default set ✅", show_alert=False)
178
  try:
179
- await q.message.edit_text(f"✅ Default set: `{value}`")
180
  except Exception:
181
  pass
182
  return
@@ -211,7 +211,6 @@ def setup_handlers(app: Client) -> None:
211
  if not isinstance(j, dict):
212
  return await safe_reply(m, f"❌ profile_add bad response: `{j}`")
213
 
214
- # ✅ handle both response shapes
215
  if not j.get("ok") and not (isinstance(j.get("data"), dict) and j["data"].get("ok")):
216
  return await safe_reply(m, texts.PROFILE_ADD_FAIL.format(j))
217
 
@@ -237,9 +236,17 @@ def setup_handlers(app: Client) -> None:
237
  return await safe_reply(m, texts.PROFILE_LIST_FAIL.format(j))
238
 
239
  profiles = j.get("profiles") or []
240
- txt = f"Default: `{j.get('default_profile_id')}`\n\n"
241
- for p in profiles:
242
- txt += f"- `{p['profile_id']}` | {p.get('label')} | refresh={p.get('has_refresh')} | ch={p.get('channel_title')}\n"
 
 
 
 
 
 
 
 
243
 
244
  kb = profiles_keyboard(profiles)
245
  await safe_reply(m, txt, reply_markup=kb)
@@ -284,13 +291,44 @@ def setup_handlers(app: Client) -> None:
284
 
285
  task_id = f"{uid}:{m.id}"
286
  create_task(task_id, uid)
287
- set_task(task_id, "downloading", "")
288
 
289
  file_path = ""
 
 
290
  try:
291
- file_path, _file_size, file_name = await download_to_temp(app_, m)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  title, desc = extract_title_description(m, file_name)
293
 
 
 
 
 
294
  se = SpeedETA()
295
  last_edit = 0.0
296
 
@@ -307,8 +345,6 @@ def setup_handlers(app: Client) -> None:
307
  )
308
  await safe_edit(status, txt)
309
 
310
- set_task(task_id, "uploading", "")
311
-
312
  yt_url = await upload_video(
313
  access_tok,
314
  file_path,
@@ -318,10 +354,20 @@ def setup_handlers(app: Client) -> None:
318
  progress_cb=prog,
319
  )
320
 
 
 
 
321
  await record_upload(uid, use_profile_id)
322
 
323
  set_task(task_id, "done", "", yt_url=yt_url)
324
- await safe_reply(m, f"{texts.DONE}\n\n🎬 {title}\n🔗 {yt_url}")
 
 
 
 
 
 
 
325
 
326
  except Exception as e:
327
  set_task(task_id, "error", str(e))
 
174
  if not j.get("ok"):
175
  return await q.answer("Failed", show_alert=True)
176
 
177
+ await q.answer("Current set ✅", show_alert=False)
178
  try:
179
+ await q.message.edit_text(f"✅ Current profile set: `{value}`")
180
  except Exception:
181
  pass
182
  return
 
211
  if not isinstance(j, dict):
212
  return await safe_reply(m, f"❌ profile_add bad response: `{j}`")
213
 
 
214
  if not j.get("ok") and not (isinstance(j.get("data"), dict) and j["data"].get("ok")):
215
  return await safe_reply(m, texts.PROFILE_ADD_FAIL.format(j))
216
 
 
236
  return await safe_reply(m, texts.PROFILE_LIST_FAIL.format(j))
237
 
238
  profiles = j.get("profiles") or []
239
+ default_id = j.get("default_profile_id")
240
+
241
+ txt = f"Current: `{default_id}`\n\n"
242
+ for i, p in enumerate(profiles, start=1):
243
+ pid = p.get("profile_id")
244
+ ch = (p.get("channel_title") or "—").strip()
245
+ cid = p.get("client_id_hint") or "—"
246
+ sec = p.get("client_secret_hint") or "****…****"
247
+ connected = "✅ Connected" if (p.get("has_refresh") and p.get("channel_id")) else "⏳ Pending"
248
+ cur = "🟢 Current" if pid == default_id else ""
249
+ txt += f"[{i}] {ch} | {cur} | id: {cid} | secret: {sec} | {connected}\n"
250
 
251
  kb = profiles_keyboard(profiles)
252
  await safe_reply(m, txt, reply_markup=kb)
 
291
 
292
  task_id = f"{uid}:{m.id}"
293
  create_task(task_id, uid)
 
294
 
295
  file_path = ""
296
+ file_size = 0
297
+
298
  try:
299
+ # ---------------- Download with ETA ----------------
300
+ set_task(task_id, "downloading", "")
301
+
302
+ dl_se = SpeedETA()
303
+ dl_last_edit = 0.0
304
+
305
+ async def dl_prog(done: int, total: int):
306
+ nonlocal dl_last_edit
307
+ snap = dl_se.update(done, total)
308
+ if time.time() - dl_last_edit < 2.0 and done < total:
309
+ return
310
+ dl_last_edit = time.time()
311
+ txt = (
312
+ f"⬇️ Downloading…\n"
313
+ f"{human_bytes(done)}/{human_bytes(total)}\n"
314
+ f"speed: {human_bytes(snap['speed_bps'])}/s | eta: {human_eta(snap['eta_sec'])}"
315
+ )
316
+ await safe_edit(status, txt)
317
+
318
+ file_path, file_size, file_name, dl_sec = await download_to_temp(
319
+ app_, m,
320
+ progress=dl_prog,
321
+ progress_args=(),
322
+ )
323
+ dl_avg_bps = int((file_size or 0) / max(dl_sec, 0.001))
324
+
325
+ # parse title/desc after download
326
  title, desc = extract_title_description(m, file_name)
327
 
328
+ # ---------------- Upload with ETA ----------------
329
+ set_task(task_id, "uploading", "")
330
+ up_t0 = time.time()
331
+
332
  se = SpeedETA()
333
  last_edit = 0.0
334
 
 
345
  )
346
  await safe_edit(status, txt)
347
 
 
 
348
  yt_url = await upload_video(
349
  access_tok,
350
  file_path,
 
354
  progress_cb=prog,
355
  )
356
 
357
+ up_sec = max(0.001, time.time() - up_t0)
358
+ up_avg_bps = int((file_size or 0) / up_sec)
359
+
360
  await record_upload(uid, use_profile_id)
361
 
362
  set_task(task_id, "done", "", yt_url=yt_url)
363
+ await safe_reply(
364
+ m,
365
+ f"{texts.DONE}\n\n"
366
+ f"🎬 {title}\n"
367
+ f"🔗 {yt_url}\n\n"
368
+ f"⬇️ Download: {human_eta(int(dl_sec))} | avg {human_bytes(dl_avg_bps)}/s\n"
369
+ f"⬆️ Upload: {human_eta(int(up_sec))} | avg {human_bytes(up_avg_bps)}/s"
370
+ )
371
 
372
  except Exception as e:
373
  set_task(task_id, "error", str(e))