Files
Downloader-Bot/handlers/utils.py
Mak5erandClaude Fable 5 caffc343af refactor: apply full-project audit — bug fixes, performance, deduplication
Full-codebase audit (149 raw findings, 57 verified and applied).

Fixes:
- download resume: recompute Range header per retry attempt; never resume
  preallocated multipart .part files (silent media corruption)
- inline sends: canonical error tail (rate-limit/queue-busy/timeout/failure)
  for all platforms; YouTube inline no longer strands tokens in
  'processing' for 6h
- admin mailing: exclude banned users; fix Telegram error-text matching
- middlewares: instantiate once and share across message/callback/inline
  so antiflood/ban state no longer fragments
- event loop: move ZIP compression, JSON state persistence, admin FS
  walks, PIL/ffmpeg/ffprobe work off the loop
- download queue: prune idle per-user scopes; cancelled leader releases
  joined duplicate requests
- worker subprocess: disable log sinks (stderr error protocol and log
  rotation no longer race the parent)
- third-party loggers route through app handlers instead of lastResort
- tiktok: fix mojibake reaction emoji; per-user settings in doc callback;
  per-URL locks for short-URL expansion; parallel photo-set downloads
- ci: fix broken yt-dlp auto-bump workflow baseline
- db: drop redundant analytics_events indexes (migration 000010)

Refactoring:
- services/inline/send_flow.py: shared inline delivery flow for all five
  platforms (~640 duplicated lines removed)
- services/media/audio_flow.py: shared MP3 pipeline (was duplicated 6x
  across youtube/youtube_inline/soundcloud/spotify)
- CobaltMediaService base + shared Cobalt response parsing
- services/platforms/ytdlp_helpers.py: shared yt-dlp scaffolding
- register_inline_send_handlers: one chosen_inline_result/callback
  registration factory for 8 handlers
- merge pinterest/threads single-media and media-group flows
- logging decorator factory; remove ~11 dead repository methods

Tests: 470 -> 518 passing, coverage 77.3% -> 80.9% (CI gate 74%).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-26 16:25:59 +03:00

132 lines
3.6 KiB
Python

from handlers.bot_profile_cache import (
_bot_avatar_file_id,
_bot_avatar_path,
_bot_id,
_bot_identity_lock,
_bot_username,
_ensure_bot_identity,
_get_bot_id,
get_bot_avatar_file_id,
get_bot_avatar_thumbnail,
get_bot_url,
get_bot_username, # noqa: F401
)
from handlers.inline_utils import (
RawAnswerInlineQuery,
build_inline_album_result,
build_inline_status_editor,
build_start_deeplink_url,
register_inline_send_handlers,
run_inline_send_callback,
safe_answer_inline_query,
sanitize_inline_results,
)
from handlers.logging_utils import (
build_request_id,
log_context_scope,
log_duration,
with_callback_logging,
with_chosen_inline_logging,
with_inline_query_logging,
with_inline_send_logging,
with_message_logging,
)
from handlers.telegram_ui_utils import (
_chat_action_cache,
_message_edit_cache,
_send_with_reaction,
build_progress_status,
build_queue_busy_text,
build_queue_status,
build_rate_limit_text,
get_message_text,
handle_download_error,
handle_download_backpressure_error,
handle_video_too_large,
should_skip_duplicate_business_message,
load_user_settings,
make_retry_status_notifier,
make_status_text_progress_updater,
maybe_delete_user_message,
react_to_message,
remove_file,
resolve_settings_target_id,
retry_async_operation,
safe_delete_message,
safe_edit_inline_media,
safe_edit_inline_text,
safe_edit_text,
send_chat_action_if_needed,
)
def make_backpressure_handler(message, business_id):
"""Build the shared backpressure closure used by the single-media flows:
forwards DownloadRateLimitError/DownloadQueueBusyError to the user-facing
handler, showing service status only outside business chats."""
async def _handle_backpressure(exc: Exception) -> None:
await handle_download_backpressure_error(
exc,
message=message,
show_service_status=business_id is None,
)
return _handle_backpressure
__all__ = [
"_bot_avatar_file_id",
"_bot_avatar_path",
"_bot_id",
"_bot_identity_lock",
"_bot_username",
"_chat_action_cache",
"_ensure_bot_identity",
"_get_bot_id",
"_message_edit_cache",
"_send_with_reaction",
"RawAnswerInlineQuery",
"build_inline_album_result",
"build_inline_status_editor",
"build_progress_status",
"build_queue_busy_text",
"build_queue_status",
"build_rate_limit_text",
"build_request_id",
"build_start_deeplink_url",
"get_bot_avatar_file_id",
"get_bot_avatar_thumbnail",
"get_bot_url",
"get_message_text",
"handle_download_error",
"handle_download_backpressure_error",
"handle_video_too_large",
"should_skip_duplicate_business_message",
"load_user_settings",
"log_context_scope",
"log_duration",
"make_backpressure_handler",
"make_retry_status_notifier",
"make_status_text_progress_updater",
"maybe_delete_user_message",
"react_to_message",
"register_inline_send_handlers",
"remove_file",
"resolve_settings_target_id",
"retry_async_operation",
"run_inline_send_callback",
"safe_answer_inline_query",
"safe_delete_message",
"safe_edit_inline_media",
"safe_edit_inline_text",
"safe_edit_text",
"sanitize_inline_results",
"send_chat_action_if_needed",
"should_skip_duplicate_business_message",
"with_callback_logging",
"with_chosen_inline_logging",
"with_inline_query_logging",
"with_inline_send_logging",
"with_message_logging",
]