-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
1384 lines (1196 loc) · 48 KB
/
Copy pathmain.py
File metadata and controls
1384 lines (1196 loc) · 48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import logging
import os
import queue
import shutil
import signal
import subprocess
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import TimeoutError as FutureTimeoutError
from pathlib import Path
from typing import Any
from urllib.parse import urlparse, urlsplit
import telebot
import yt_dlp
from requests_toolbelt.multipart.encoder import MultipartEncoder, MultipartEncoderMonitor
from telebot import types
from telebot.util import quick_markup
from app.download_utils import (
calc_download_progress as _calc_download_progress,
)
from app.download_utils import (
find_downloaded_file as _find_downloaded_file_impl,
)
from app.download_utils import (
find_file_by_prefix as _find_file_by_prefix_impl,
)
from app.download_utils import (
render_status as _render_status,
)
from app.healthcheck import HEALTH_MARKER
from app.http_utils import telegram_upload_session
from app.logging_setup import configure_logging
from app.media_validation import validate_media_file
from app.models import ActiveJob, DownloadJob, PendingRequest, VideoFormatCandidate
from app.planner import (
apply_instagram_stability_opts as _apply_instagram_stability_opts,
)
from app.planner import (
apply_youtube_runtime_opts as _apply_youtube_runtime_opts,
)
from app.planner import (
build_audio_plan_mp3 as _build_audio_plan_mp3,
)
from app.planner import build_video_candidates as _build_video_candidates
from app.planner import (
get_video_meta as _get_video_meta,
)
from app.planner import (
is_instagram_url as _is_instagram_url,
)
from app.planner import metadata_without_format_selection as _metadata_without_format_selection
from app.settings import Settings, load_settings
from app.temp_files import cleanup_directory_contents, cleanup_job_directory, cleanup_stale_directories
from app.text_utils import (
extract_first_url as _extract_first_url,
)
from app.text_utils import (
fmt_bytes as _fmt_bytes,
)
from app.text_utils import (
sanitize_filename_base as _sanitize_filename_base,
)
from app.text_utils import (
strip_hashtags as _strip_hashtags,
)
from app.text_utils import (
youtube_url_validation,
)
from app.url_security import UnsafeUrlError, safe_url_for_log, validate_public_url
# =========================
# Telegram bot init
# =========================
bot: telebot.TeleBot | None = None
SETTINGS: Settings | None = None
LOGGER = logging.getLogger("video_downloader_bot")
# Edit throttling (avoid Telegram flood limits)
EDIT_INTERVAL_SEC = 1.8
# Parallel jobs: how many downloads/uploads can run simultaneously
WORKERS = 2
# TTL for pending "choice" requests to avoid memory leaks
PENDING_TTL_SEC = 10 * 60
# Telegram Bot API upload limit (you keep it in config)
MAX_SEND_BYTES = 50 * 1024 * 1024
# yt-dlp optimization for segmented streams (HLS/DASH)
YTDLP_CONCURRENT_FRAGMENTS = 4
# =========================
# Global state
# =========================
bot_lock = threading.RLock()
state_lock = threading.RLock()
stop_event = threading.Event()
last_edited: dict[str, datetime.datetime] = {}
last_text: dict[str, str] = {}
pending_requests: dict[str, PendingRequest] = {}
jobs_q: "queue.Queue[DownloadJob | None]" = queue.Queue(maxsize=200)
# Cancel support
cancel_events: dict[str, threading.Event] = {}
active_jobs: dict[str, ActiveJob] = {}
worker_threads: list[threading.Thread] = []
maintenance_thread: threading.Thread | None = None
upload_slots = threading.BoundedSemaphore(2)
metadata_slots = threading.BoundedSemaphore(2)
metadata_executor: ThreadPoolExecutor | None = None
maintenance_finished = threading.Event()
fatal_lifecycle_error = threading.Event()
worker_failure_alerted = threading.Event()
class JobCancelled(RuntimeError):
pass
class JobTimedOut(RuntimeError):
pass
def _settings() -> Settings:
if SETTINGS is None:
raise RuntimeError("application is not initialized")
return SETTINGS
def _output_folder() -> Path:
return _settings().output_dir
def _check_job(job_id: str, deadline: float) -> None:
if _is_cancelled(job_id):
raise JobCancelled("cancelled")
if time.monotonic() >= deadline:
raise JobTimedOut("job deadline exceeded")
# =========================
# Helpers (safe bot calls)
# =========================
def _bot_call(fn, *args, **kwargs):
with bot_lock:
return fn(*args, **kwargs)
def _safe_delete(chat_id: int, message_id: int) -> None:
try:
assert bot is not None
_bot_call(bot.delete_message, chat_id, message_id)
except Exception:
LOGGER.debug("telegram delete failed chat_id=%s message_id=%s", chat_id, message_id, exc_info=True)
def _safe_edit(chat_id: int, message_id: int, text: str, reply_markup=None, force: bool = False) -> None:
key = f"{chat_id}-{message_id}"
now = datetime.datetime.now()
if not force:
with state_lock:
last = last_edited.get(key)
if last is not None and (now - last).total_seconds() < EDIT_INTERVAL_SEC:
return
if last_text.get(key) == text:
return
try:
assert bot is not None
_bot_call(
bot.edit_message_text,
chat_id=chat_id,
message_id=message_id,
text=text,
reply_markup=reply_markup,
disable_web_page_preview=True,
)
with state_lock:
last_edited[key] = now
last_text[key] = text
except Exception:
LOGGER.debug("telegram edit failed chat_id=%s message_id=%s", chat_id, message_id, exc_info=True)
def _safe_send_message(chat_id: int, text: str, reply_to_message_id: int | None = None, reply_markup=None):
try:
assert bot is not None
return _bot_call(
bot.send_message,
chat_id,
text,
reply_to_message_id=reply_to_message_id,
reply_markup=reply_markup,
disable_web_page_preview=True,
)
except Exception:
LOGGER.warning("telegram send message failed chat_id=%s", chat_id, exc_info=True)
return None
def _safe_answer_callback(call_id: str, text: str = "") -> None:
try:
assert bot is not None
_bot_call(bot.answer_callback_query, call_id, text=text)
except Exception:
LOGGER.debug("telegram callback answer failed", exc_info=True)
def _notify_operator_critical(text: str) -> None:
if SETTINGS is None or SETTINGS.logs_chat_id is None or bot is None:
return
try:
_bot_call(bot.send_message, SETTINGS.logs_chat_id, text, disable_web_page_preview=True)
except Exception:
LOGGER.exception("operator critical notification failed")
# =========================
# Cancel UI
# =========================
def _cancel_markup(job_id: str) -> types.InlineKeyboardMarkup:
kb = types.InlineKeyboardMarkup(row_width=1)
kb.add(types.InlineKeyboardButton("Cancel", callback_data=f"cnl|{job_id}"))
return kb
def _is_cancelled(job_id: str) -> bool:
with state_lock:
ev = cancel_events.get(job_id)
return bool(ev and ev.is_set())
# =========================
# Upload via Bot API with progress + cancel
# =========================
def _send_via_bot_api_with_progress(
job_id: str,
chat_id: int,
reply_to_message_id: int,
status_message_id: int,
title: str,
method_name: str,
file_field_name: str,
file_path: str,
send_filename: str,
stage_label: str,
extra_params: dict[str, Any],
deadline: float,
) -> None:
api_url = f"https://api.telegram.org/bot{_settings().token}/{method_name}"
file_size = os.path.getsize(file_path) if os.path.exists(file_path) else 0
def render_upload(pct: int | None, sent: int | None, total_len: int | None) -> str:
line = f"Status: ⬆️ {stage_label}"
if pct is not None:
pct = max(0, min(100, int(pct)))
line += f" {pct}%"
if isinstance(sent, int) and isinstance(total_len, int) and total_len > 0:
line += f"\n{_fmt_bytes(sent)} / {_fmt_bytes(total_len)}"
return f"{title}\n\n{line}"
_safe_edit(
chat_id, status_message_id, render_upload(0, 0, file_size), reply_markup=_cancel_markup(job_id), force=True
)
_check_job(job_id, deadline)
remaining = max(0.0, deadline - time.monotonic())
if not upload_slots.acquire(timeout=remaining):
raise JobTimedOut("upload slot deadline exceeded")
try:
with open(file_path, "rb") as f:
fields = {
"chat_id": str(chat_id),
"reply_to_message_id": str(reply_to_message_id),
**{k: str(v) for k, v in extra_params.items() if v is not None},
file_field_name: (send_filename, f),
}
encoder = MultipartEncoder(fields=fields)
def _cb(monitor: MultipartEncoderMonitor):
_check_job(job_id, deadline)
total_len = monitor.len
sent = monitor.bytes_read
pct = int((sent * 100) / total_len) if total_len else None
_safe_edit(
chat_id,
status_message_id,
render_upload(pct, sent, total_len),
reply_markup=_cancel_markup(job_id),
)
monitor = MultipartEncoderMonitor(encoder, _cb)
session = telegram_upload_session()
try:
remaining = max(1.0, deadline - time.monotonic())
resp = session.post(
api_url,
data=monitor,
headers={"Content-Type": monitor.content_type},
timeout=(20, min(60 * 30, remaining)),
)
finally:
session.close()
finally:
upload_slots.release()
_parse_upload_response(resp)
_safe_edit(
chat_id,
status_message_id,
render_upload(100, file_size, file_size),
reply_markup=_cancel_markup(job_id),
force=True,
)
def _parse_upload_response(response: Any) -> dict[str, Any]:
try:
data = response.json()
except Exception as exc:
raise RuntimeError(f"Telegram API returned HTTP {response.status_code}") from exc
if not isinstance(data, dict) or not data.get("ok"):
description = data.get("description", "request failed") if isinstance(data, dict) else "request failed"
raise RuntimeError(f"Telegram API error: {description}")
return data
# =========================
# Downloaded file discovery
# =========================
def _find_file_by_prefix(output_folder: str, prefix: str, prefer_ext: str | None = None) -> str | None:
return _find_file_by_prefix_impl(output_folder, prefix, prefer_ext=prefer_ext)
def _find_downloaded_file(
info: dict[str, Any], output_folder: str, fallback_prefix: str, prefer_ext: str | None = None
) -> str | None:
return _find_downloaded_file_impl(info, output_folder, fallback_prefix, prefer_ext=prefer_ext)
def _get_video_meta_with_hidden_retries(url: str) -> dict[str, Any]:
settings = _settings()
try:
meta = _get_video_meta(
url,
js_runtimes=settings.ytdlp_js_runtimes,
remote_components=settings.ytdlp_remote_components,
instagram_impersonate=settings.ytdlp_instagram_impersonate,
instagram_retries=settings.ytdlp_instagram_retries,
instagram_fragment_retries=settings.ytdlp_instagram_fragment_retries,
instagram_socket_timeout=settings.ytdlp_instagram_socket_timeout,
cookies_file=str(settings.cookies_file) if settings.cookies_file else None,
)
_validate_metadata_urls(meta)
return meta
except Exception:
if not _is_instagram_url(url):
raise
LOGGER.debug("Instagram metadata primary attempt failed; using fallback", exc_info=True)
# Fallback: retry without forced impersonation.
meta = _get_video_meta(
url,
js_runtimes=settings.ytdlp_js_runtimes,
remote_components=settings.ytdlp_remote_components,
instagram_impersonate=None,
instagram_retries=5,
instagram_fragment_retries=5,
instagram_socket_timeout=20,
cookies_file=str(settings.cookies_file) if settings.cookies_file else None,
)
_validate_metadata_urls(meta)
return meta
class MetadataBusy(RuntimeError):
pass
class MetadataTimedOut(RuntimeError):
pass
def _run_metadata_operation(url: str) -> dict[str, Any]:
executor = metadata_executor
if executor is None:
raise RuntimeError("metadata executor is not initialized")
if not metadata_slots.acquire(blocking=False):
raise MetadataBusy("metadata capacity is full")
def run() -> dict[str, Any]:
try:
return _get_video_meta_with_hidden_retries(url)
finally:
metadata_slots.release()
future = executor.submit(run)
try:
return future.result(timeout=_settings().metadata_timeout_seconds)
except FutureTimeoutError as exc:
future.cancel()
raise MetadataTimedOut("metadata extraction timed out") from exc
def _validate_metadata_urls(meta: dict[str, Any]) -> None:
candidates = [meta.get(key) for key in ("webpage_url", "original_url", "url")]
candidates.extend(item.get("url") for item in meta.get("formats", []) if isinstance(item, dict))
checked_hosts: set[tuple[str, int | None]] = set()
for value in candidates:
if not isinstance(value, str) or not value.startswith(("http://", "https://")):
continue
parts = urlsplit(value)
host_key = ((parts.hostname or "").lower(), parts.port)
if host_key in checked_hosts:
continue
validate_public_url(value)
checked_hosts.add(host_key)
# =========================
# Worker: download + send
# =========================
def _download_and_send(job: DownloadJob) -> None:
job_id = job.job_id
chat_id = job.chat_id
reply_to_message_id = job.reply_to_message_id
status_message_id = job.status_message_id
url = job.url
mode = job.mode
title = job.title
deadline = job.deadline
job_dir = _output_folder() / job_id
try:
job_dir.mkdir(parents=True, exist_ok=False)
except Exception:
LOGGER.exception("job workspace creation failed job_id=%s", job_id)
_safe_edit(
chat_id,
status_message_id,
f"{title}\n\nStatus: ❌ Download failed. Please try again.",
reply_markup=None,
force=True,
)
with state_lock:
cancel_events.pop(job_id, None)
active_jobs.pop(job_id, None)
return
file_path: str | None = None
settings = _settings()
def build_options(plan: VideoFormatCandidate | dict[str, Any], tmp_id: str) -> dict[str, Any]:
progress_state: dict[str, Any] = {"pct": 0}
def progress_hook(data: dict[str, Any]) -> None:
_check_job(job_id, deadline)
filename = data.get("filename") or data.get("tmpfilename") or ""
if filename and tmp_id not in os.path.basename(filename):
return
if data.get("status") == "downloading":
pct, done_bytes, total_bytes = _calc_download_progress(data, progress_state)
hard_total = data.get("total_bytes")
if isinstance(hard_total, int) and hard_total > MAX_SEND_BYTES and mode != "audio":
raise RuntimeError("download exceeded the preflight size limit")
_safe_edit(
chat_id,
status_message_id,
_render_status(title, "downloading", pct, done_bytes, total_bytes),
reply_markup=_cancel_markup(job_id),
)
elif data.get("status") == "finished":
progress_state["pct"] = 100
_safe_edit(
chat_id,
status_message_id,
_render_status(title, "downloading", 100, None, None),
reply_markup=_cancel_markup(job_id),
force=True,
)
options: dict[str, Any] = {
"format": str(plan.get("format_spec", "best")),
"outtmpl": os.fspath(job_dir / f"{tmp_id}.%(ext)s"),
"progress_hooks": [progress_hook],
"max_filesize": MAX_SEND_BYTES,
"noplaylist": True,
"quiet": True,
"no_warnings": True,
"concurrent_fragment_downloads": settings.concurrent_fragments,
"retries": 5,
"fragment_retries": 5,
"socket_timeout": 20,
"postprocessor_hooks": [lambda _status: _check_job(job_id, deadline)],
}
if settings.cookies_file:
options["cookiefile"] = os.fspath(settings.cookies_file)
_apply_youtube_runtime_opts(
options,
url,
settings.ytdlp_js_runtimes,
settings.ytdlp_remote_components,
)
_apply_instagram_stability_opts(
options,
url,
impersonate=settings.ytdlp_instagram_impersonate,
retries=settings.ytdlp_instagram_retries,
fragment_retries=settings.ytdlp_instagram_fragment_retries,
socket_timeout=settings.ytdlp_instagram_socket_timeout,
)
if plan.get("merge_output_format"):
options["merge_output_format"] = str(plan["merge_output_format"])
if mode == "audio":
options["postprocessors"] = [
{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": str(int(plan.get("mp3_kbps", 128))),
}
]
return options
plans: list[VideoFormatCandidate | dict[str, Any]] = (
([job.audio_plan] if job.audio_plan else []) if mode == "audio" else list(job.video_candidates)
)
try:
_check_job(job_id, deadline)
_safe_edit(
chat_id,
status_message_id,
_render_status(title, "downloading", 0, None, None),
reply_markup=_cancel_markup(job_id),
force=True,
)
for index, plan in enumerate(plans, start=1):
cleanup_directory_contents(job_dir)
tmp_id = uuid.uuid4().hex
options = build_options(plan, tmp_id)
info: dict[str, Any]
try:
_check_job(job_id, deadline)
with yt_dlp.YoutubeDL(options) as ydl:
info = ydl.process_ie_result(
_metadata_without_format_selection(job.metadata),
download=True,
)
except (JobCancelled, JobTimedOut):
raise
except Exception:
_check_job(job_id, deadline)
if not _is_instagram_url(url):
LOGGER.warning(
"download candidate failed job_id=%s stage=download candidate=%s attempt=%s/%s",
job_id,
plan.get("format_spec"),
index,
len(plans),
exc_info=True,
)
cleanup_directory_contents(job_dir)
continue
LOGGER.info(
"Instagram fresh extraction retry job_id=%s stage=download candidate=%s",
job_id,
plan.get("format_spec"),
exc_info=True,
)
cleanup_directory_contents(job_dir)
tmp_id = uuid.uuid4().hex
options = build_options(plan, tmp_id)
options.pop("impersonate", None)
options["concurrent_fragment_downloads"] = 1
try:
with yt_dlp.YoutubeDL(options) as ydl:
info = ydl.extract_info(url, download=True)
except (JobCancelled, JobTimedOut):
raise
except Exception:
LOGGER.warning(
"Instagram candidate failed after fresh extraction job_id=%s candidate=%s attempt=%s/%s",
job_id,
plan.get("format_spec"),
index,
len(plans),
exc_info=True,
)
cleanup_directory_contents(job_dir)
continue
try:
_check_job(job_id, deadline)
prefer_ext = ".mp3" if mode == "audio" else None
file_path = _find_downloaded_file(info, os.fspath(job_dir), tmp_id, prefer_ext=prefer_ext)
if not file_path:
file_path = _find_file_by_prefix(os.fspath(job_dir), tmp_id, prefer_ext=prefer_ext)
if not file_path:
raise RuntimeError("downloaded file was not found")
remaining = max(1.0, deadline - time.monotonic())
validate_media_file(
file_path,
mode,
MAX_SEND_BYTES,
timeout_seconds=min(15.0, remaining),
)
break
except (JobCancelled, JobTimedOut):
raise
except Exception:
LOGGER.warning(
"download candidate rejected job_id=%s stage=validate candidate=%s attempt=%s/%s",
job_id,
plan.get("format_spec"),
index,
len(plans),
exc_info=True,
)
file_path = None
cleanup_directory_contents(job_dir)
else:
raise RuntimeError("all prechecked download candidates failed")
_check_job(job_id, deadline)
if not file_path:
raise RuntimeError("downloaded file was not found")
base = _sanitize_filename_base(title)
if mode == "audio":
send_filename = f"{base}.mp3"
_send_via_bot_api_with_progress(
job_id=job_id,
chat_id=chat_id,
reply_to_message_id=reply_to_message_id,
status_message_id=status_message_id,
title=title,
method_name="sendAudio",
file_field_name="audio",
file_path=file_path,
send_filename=send_filename,
stage_label="Sending audio...",
extra_params={},
deadline=deadline,
)
elif mode == "doc":
ext = os.path.splitext(file_path)[1] or ".mp4"
send_filename = f"{base}{ext}"
_send_via_bot_api_with_progress(
job_id=job_id,
chat_id=chat_id,
reply_to_message_id=reply_to_message_id,
status_message_id=status_message_id,
title=title,
method_name="sendDocument",
file_field_name="document",
file_path=file_path,
send_filename=send_filename,
stage_label="Sending document...",
extra_params={},
deadline=deadline,
)
else:
ext = os.path.splitext(file_path)[1] or ".mp4"
send_filename = f"{base}{ext}"
_send_via_bot_api_with_progress(
job_id=job_id,
chat_id=chat_id,
reply_to_message_id=reply_to_message_id,
status_message_id=status_message_id,
title=title,
method_name="sendVideo",
file_field_name="video",
file_path=file_path,
send_filename=send_filename,
stage_label="Sending video...",
extra_params={"supports_streaming": "true"},
deadline=deadline,
)
# Success: delete status message (only media remains)
_safe_delete(chat_id, status_message_id)
except JobCancelled:
LOGGER.info("job cancelled job_id=%s stage=processing", job_id)
_safe_delete(chat_id, status_message_id)
except JobTimedOut:
LOGGER.warning("job timed out job_id=%s stage=processing", job_id)
_safe_edit(
chat_id,
status_message_id,
f"{title}\n\nStatus: ❌ Job timed out. Please try again.",
reply_markup=None,
force=True,
)
except Exception:
LOGGER.exception("job failed job_id=%s stage=processing url=%s", job_id, safe_url_for_log(url))
if _is_cancelled(job_id):
_safe_delete(chat_id, status_message_id)
else:
_safe_edit(
chat_id,
status_message_id,
f"{title}\n\nStatus: ❌ Download failed. Please try again.",
reply_markup=None,
force=True,
)
finally:
cleanup_job_directory(job_dir)
with state_lock:
cancel_events.pop(job_id, None)
active_jobs.pop(job_id, None)
def _worker_loop():
while not stop_event.is_set():
job = jobs_q.get()
try:
if job is None:
return
_download_and_send(job)
except Exception:
LOGGER.exception("worker crashed while processing a job")
finally:
jobs_q.task_done()
# =========================
# Logging (kept as-is)
# =========================
def log(message, text: str, media: str):
LOGGER.info(
"download request media=%s user_id=%s chat_id=%s url=%s",
media,
message.from_user.id,
message.chat.id,
safe_url_for_log(text),
)
# =========================
# Commands
# =========================
def start_help(message):
assert bot is not None
bot.reply_to(
message,
"*Send me a video link* and I'll download it for you.\n\n"
"You can choose:\n"
"• *Video*\n"
"• *Document* (original file)\n"
"• *Audio (MP3)*\n\n"
f"Upload limit: *{_fmt_bytes(MAX_SEND_BYTES)}*\n\n"
"_Powered by_ [Avazbek Olimov](https://github.com/Avazbek22/VideoDownloaderBot)",
parse_mode="MARKDOWN",
disable_web_page_preview=True,
)
# =========================
# Pending cleanup
# =========================
def _cleanup_pending() -> None:
now = time.time()
with state_lock:
expired = [rid for rid, data in pending_requests.items() if now - data.created_at > PENDING_TTL_SEC]
for rid in expired:
pending_requests.pop(rid, None)
stale_edit_keys = [
key
for key, edited_at in last_edited.items()
if (datetime.datetime.now() - edited_at).total_seconds() > PENDING_TTL_SEC
]
for key in stale_edit_keys:
last_edited.pop(key, None)
last_text.pop(key, None)
# =========================
# Main flow: message -> Getting info -> buttons
# (NO downloading unless size <= limit is proven)
# =========================
def _send_choice_ui(message, url: str) -> None:
assert bot is not None
_cleanup_pending()
processing_msg = bot.reply_to(message, "Getting info...", disable_web_page_preview=True)
try:
meta = _run_metadata_operation(url)
except (MetadataBusy, MetadataTimedOut):
LOGGER.warning("metadata unavailable url=%s", safe_url_for_log(url), exc_info=True)
_safe_delete(message.chat.id, processing_msg.message_id)
bot.reply_to(message, "Invalid URL or unsupported website.", disable_web_page_preview=True)
return
except Exception:
LOGGER.exception("metadata failed url=%s", safe_url_for_log(url))
_safe_delete(message.chat.id, processing_msg.message_id)
bot.reply_to(message, "Invalid URL or unsupported website.", disable_web_page_preview=True)
return
title = (meta.get("title") or "Video").strip()
title = _strip_hashtags(title) or "Video"
# Build a best-first list. Every retained candidate has a proven size.
proven_candidates = _build_video_candidates(meta, 2**63 - 1)
video_candidates = [candidate for candidate in proven_candidates if candidate.estimated_size <= MAX_SEND_BYTES]
video_plan = video_candidates[0] if video_candidates else None
display_plan = video_plan or (proven_candidates[0] if proven_candidates else None)
audio_plan, audio_reason = _build_audio_plan_mp3(meta, MAX_SEND_BYTES)
_safe_delete(message.chat.id, processing_msg.message_id)
# Decide availability for VIDEO/DOC:
# We allow only if size is confident and <= limit.
video_size = display_plan.get("estimated_size") if display_plan else None
video_conf = bool(display_plan and display_plan.estimated_confident and display_plan.estimated_size > 0)
video_ok = bool(video_conf and isinstance(video_size, int) and video_size <= MAX_SEND_BYTES)
# If video is confidently too big -> tell and do not offer Video/Document.
if video_conf and isinstance(video_size, int) and video_size > MAX_SEND_BYTES:
msg = (
f"{title}\n\n"
f"This video is too large for Telegram bots.\n"
f"Estimated size: {_fmt_bytes(video_size)}\n"
f"Limit: {_fmt_bytes(MAX_SEND_BYTES)}\n"
)
# If audio fits, offer only audio button
if audio_plan:
request_id = uuid.uuid4().hex[:18]
with state_lock:
pending_requests[request_id] = PendingRequest(
created_at=time.time(),
user_id=message.from_user.id,
chat_id=message.chat.id,
reply_to_message_id=message.message_id,
url=url,
title=title,
video_candidates=(),
audio_plan=audio_plan,
metadata=meta,
)
kb = types.InlineKeyboardMarkup(row_width=1)
kb.add(types.InlineKeyboardButton("Download as Audio (MP3)", callback_data=f"dl|audio|{request_id}"))
_safe_send_message(
chat_id=message.chat.id,
text=msg + f"\nAudio option available: {audio_plan.get('quality_label', 'mp3')}",
reply_to_message_id=message.message_id,
reply_markup=kb,
)
return
# No audio either
if audio_reason:
msg += f"\nAudio is not available: {audio_reason}"
_safe_send_message(message.chat.id, msg, reply_to_message_id=message.message_id)
return
# If we cannot confidently determine size -> do NOT download video/doc (policy to avoid wasting time/data)
if not video_ok:
msg = (
f"{title}\n\n"
f"I can't reliably determine the final video size before downloading.\n"
f"Telegram bot upload limit is {_fmt_bytes(MAX_SEND_BYTES)}.\n"
f"Please try a shorter video.\n"
)
# If audio fits, offer audio
if audio_plan:
request_id = uuid.uuid4().hex[:18]
with state_lock:
pending_requests[request_id] = PendingRequest(
created_at=time.time(),
user_id=message.from_user.id,
chat_id=message.chat.id,
reply_to_message_id=message.message_id,
url=url,
title=title,
video_candidates=(),
audio_plan=audio_plan,
metadata=meta,
)
kb = types.InlineKeyboardMarkup(row_width=1)
kb.add(types.InlineKeyboardButton("Download as Audio (MP3)", callback_data=f"dl|audio|{request_id}"))
_safe_send_message(
chat_id=message.chat.id,
text=msg + f"\nAudio option available: {audio_plan.get('quality_label', 'mp3')}",
reply_to_message_id=message.message_id,
reply_markup=kb,
)
return
if audio_reason:
msg += f"\nAudio is not available: {audio_reason}"
_safe_send_message(message.chat.id, msg, reply_to_message_id=message.message_id)
return
# If video_ok == True -> show normal 3 buttons (Video/Document/Audio if available)
request_id = uuid.uuid4().hex[:18]
with state_lock:
pending_requests[request_id] = PendingRequest(
created_at=time.time(),
user_id=message.from_user.id,
chat_id=message.chat.id,
reply_to_message_id=message.message_id,
url=url,
title=title,
video_candidates=tuple(video_candidates),
audio_plan=audio_plan,
metadata=meta,
)
kb = types.InlineKeyboardMarkup(row_width=2)
kb.add(
types.InlineKeyboardButton("Download as Video", callback_data=f"dl|video|{request_id}"),
types.InlineKeyboardButton("Download as Document", callback_data=f"dl|doc|{request_id}"),
)
if audio_plan:
kb.add(types.InlineKeyboardButton("Download as Audio (MP3)", callback_data=f"dl|audio|{request_id}"))
info_lines = [
f"Estimated size: {_fmt_bytes(int(video_size))} (limit {_fmt_bytes(MAX_SEND_BYTES)})",
f"Selected: {video_plan.get('quality_label', 'mp4')}",
]
if audio_plan:
info_lines.append(f"Audio: {audio_plan.get('quality_label', 'mp3')}")
_safe_send_message(
chat_id=message.chat.id,
text=f"{title}\n\nChoose download method:\n" + "\n".join(info_lines),
reply_to_message_id=message.message_id,
reply_markup=kb,
)
def handle_private_messages(message):
if message.chat.type != "private":
return
text = message.text if message.text else message.caption if message.caption else None
if not text:
return
if isinstance(text, str) and text.strip().startswith("/"):
return
url = _extract_first_url(text)
if not url:
return
try:
validate_public_url(url)
except UnsafeUrlError:
bot.reply_to(message, "Invalid URL", disable_web_page_preview=True)
return
url_info = urlparse(url)
if url_info.netloc in ["www.youtube.com", "youtu.be", "youtube.com", "youtu.be"] and not youtube_url_validation(
url
):
bot.reply_to(message, "Invalid URL", disable_web_page_preview=True)
return
log(message, url, "video")
_send_choice_ui(message, url)
# =========================
# Callback: cancel
# =========================
def on_cancel(call):
try:
parts = call.data.split("|")
if len(parts) != 2:
_safe_answer_callback(call.id, "Invalid action")
return