Commit Graph

375 Commits

Author SHA1 Message Date
vvzvlad c9b0d2ffc9 feat(db): replace media file ID JSON store with SQLite
Switch persistence of media file identifiers from a JSON file to a
SQLite database. Introduce DB_PATH, init_db_sync, upsert, update, and
removal functions. Remove json-repair dependency and related locking
logic. Update api_server and post_parser to use the new database
helpers.

BREAKING CHANGE: media_file_ids.json is no longer used; existing
data must be migrated to the new SQLite database.
2026-04-05 20:20:37 +03:00
vvzvlad 17ec871d7c feat(config): switch proxy to SOCKS5 and add auth options
The proxy configuration now uses a SOCKS5 scheme instead of MTProto. The default port is updated to 1080 and optional username/password fields are supported. Docker compose comments are updated accordingly.

BREAKING CHANGE: existing MTProto proxy settings (scheme "mtproto", port 443) are no longer supported and must be migrated to SOCKS5.
2026-04-05 18:18:34 +03:00
vvzvlad 09692edf06 feat(config): add mtproto proxy support
Introduce optional MTProto proxy configuration sourced from environment variables, expose related settings in `config.py`, document the variables in `dockercompose.yml`, and pass the proxy configuration to the Telegram client initialization.
2026-04-05 18:09:23 +03:00
vvzvlad 02c29f8692 fix(telegram): add shutdown guard to avoid duplicate restarts
Add a `_shutting_down` flag to `TelegramClient` to suppress disconnect handling during intentional shutdown. The flag is set before sending SIGTERM in `_restart_app` and checked in `_on_disconnect` to ignore those events. Minor formatting adjustments were also applied to `api_server.py` (try block and `uvicorn.run` arguments).
2026-03-16 04:13:39 +03:00
vvzvlad 14f8db0a32 refactor(client): add optional session param to _on_disconnect 2026-03-16 04:05:39 +03:00
vvzvlad a38a729137 perf(api): add timing diagnostics for media ops
Add monotonic timing instrumentation around media download, semaphore
wait, HTML sanitization, and media file persistence. Log warnings when
operations exceed defined thresholds and provide diagnostic info for
task queue size. This aids performance monitoring without altering
behaviour.
2026-03-16 01:14:24 +03:00
vvzvlad b7b1daa035 chore(logs): remove pyrogram bridge logs file 2026-03-16 01:07:43 +03:00
vvzvlad 4ef7b00c16 refactor(api): split download semaphores and remove diagnostic monitoring
Separate live HTTP media requests and background cache workers by introducing `HTTP_DOWNLOAD_SEMAPHORE` and `BACKGROUND_DOWNLOAD_SEMAPHORE`. Remove the previous global `DOWNLOAD_SEMAPHORE`, diagnostic event‑loop monitor, and related logging. Refactor `download_media_file` to handle large videos with temporary files, clean up zero‑size cache entries, and update access timestamps without semaphore tracking. Adjust background worker and request handling to use the new semaphores, simplifying concurrency control and eliminating unnecessary diagnostics.
2026-03-16 01:07:19 +03:00
vvzvlad 8bcce02368 feat(api): add diagnostic monitoring and semaphore tracking
Introduce a periodic event‑loop monitor that logs active tasks, semaphore
state, queue size and thread‑pool activity. Track the number of coroutine
waiters for the download semaphore and emit detailed diagnostic logs in
download handling and the background worker. This aids debugging of
concurrency and resource‑usage issues.
2026-03-16 01:00:31 +03:00
vvzvlad 7d4322f975 chore(workspace): add VS Code workspace configuration files 2026-03-16 01:00:20 +03:00
vvzvlad 5257c50243 fix(post_parser): correct content order when caption display flag is toggled
The changes ensure that when `show_caption_above` is true, text content is rendered before media, and when false, media is rendered before text. This aligns the actual rendering order with the semantic meaning of the flag, fixing a bug where content ordering did not match user expectations.
2026-01-02 05:38:30 +03:00
vvzvlad 76e163098b feat(post_parser): support show caption above media attribute for message rendering
Add support for Telegram's show_caption_above_media message attribute to conditionally order caption and media content in HTML output. When enabled, media is rendered before the text caption instead of the default text-before-media order.
2026-01-02 05:27:18 +03:00
vvzvlad 5543c0514a feat(post_parser): allow strikethrough tags in HTML sanitization
Add 's' and 'del' tags to the allowed HTML tags list in the sanitization
method to support strikethrough and deleted text formatting.
2026-01-02 02:33:37 +03:00
vvzvlad fef63c1224 chore(rss_generator): add logging for RSS feed date ranges and entry timestamps
Logs the date range (oldest/newest) and total count of posts being added to RSS feeds, plus debug-level logging for each entry's timestamp conversion. This improves observability for feed generation and helps diagnose date-related issues.
2026-01-01 15:02:36 +03:00
vvzvlad 337cabe180 fix(rss_generator): handle Telegram flood wait errors with automatic retry logic
Add flood wait exception handling in both RSS and HTML generation functions.
The bot now automatically waits for the specified duration and retries the
operation when encountering rate limiting from Telegram's API.
2026-01-01 14:57:16 +03:00
vvzvlad 3c2b4ce544 perf(downloads): implement concurrent download queue with semaphore limiting and retry logic
Add queue-based background download system to improve performance and reliability:
- Introduce DOWNLOAD_SEMAPHORE to limit concurrent downloads to 3
- Add asyncio.Queue (maxsize 100) with dedicated worker for background processing
- Implement safe_get_messages and safe_download_media wrappers with timeout protection (30s and 120s)
- Add retry logic for KeyError auth failures with 5s backoff
- Replace synchronous sequential downloads with asynchronous queued processing
- Prevent event loop blocking by queuing files instead of immediate download
2026-01-01 14:14:26 +03:00
vvzvlad deacb8b9a0 refactor rss_generator: offload HTML concatenation and sanitization to asyncio.to_thread to improve CPU performance and prevent event loop blocking 2025-09-15 12:16:53 +03:00
vvzvlad 68a2dbfa04 refactor api_server and post_parser: implement asynchronous JSON file operations with locking mechanism to ensure thread safety, and streamline media file ID persistence logic 2025-09-13 20:47:16 +03:00
vvzvlad 1464ea326e refactor api_server: streamline RSS feed generation by consolidating error handling and removing redundant loop structure 2025-09-13 00:06:36 +03:00
vvzvlad 778aa04904 refactor api_server: implement asynchronous file I/O using asyncio.to_thread for JSON operations, add global python-magic instance for MIME type detection, and enhance delayed file deletion documentation 2025-09-12 23:50:52 +03:00
vvzvlad 43516d6dab refactor api_server and TelegramClient: remove force_exit function and replace with sys.exit for process termination, update restart logic to use SIGTERM instead of SIGKILL for graceful shutdown 2025-06-10 15:03:09 +03:00
vvzvlad 521c3dc394 implement forceful termination in api_server and TelegramClient: add force_exit function for immediate process shutdown, update SIGTERM handler for forceful exit, and adjust restart logic in connection_handler to use SIGKILL 2025-06-10 14:28:51 +03:00
vvzvlad d3366ca7ef refactor tg_cache: update _get_history_from_cache to use max_age_hours instead of max_age_seconds, adjust cache expiration logic accordingly, and remove cache_ttl parameter from cached_get_chat_history 2025-06-10 14:17:16 +03:00
vvzvlad d6c3725b02 implement graceful shutdown in api_server: add SIGTERM handler for proper termination and enhance error handling for server startup issues 2025-06-10 14:06:56 +03:00
vvzvlad 2c9ebf139f refactor connection_handler in TelegramClient: increase wait time to 5 seconds before restart, implement process termination and forking for more reliable restarts 2025-06-10 03:58:33 +03:00
vvzvlad 688b9e1927 refactor tg_cache: update cache file naming convention to use a simpler format by removing '_history' from filename 2025-06-10 03:52:17 +03:00
vvzvlad 0dd4460e35 refactor tg_cache: simplify cache file path generation by removing limit from filename and updating related cache functions 2025-06-10 03:51:51 +03:00
vvzvlad 14081a3d44 refactor rss_generator and tg_cache: replace cached_get_chat with direct client.get_chat calls, update message caching logic to handle limits, and improve cache file naming for message history 2025-06-10 03:48:08 +03:00
vvzvlad 223ae57929 add delay in connection_handler to ensure resources are freed before process restart 2025-06-10 03:38:33 +03:00
vvzvlad 0f992bf3a8 enhance cache expiration logic in _get_from_cache: add randomness to max age check for improved cache validity 2025-06-10 03:28:43 +03:00
vvzvlad c94090c4b5 update _on_disconnect method in TelegramClient: change to async for better handling of disconnections 2025-06-10 03:25:54 +03:00
vvzvlad 95361be6f4 refactor tg_cache: change debug logs to info level for cache operations to enhance log clarity 2025-06-10 03:24:22 +03:00
vvzvlad b7a8f4f60d refactor requirements and rss_generator: replace direct get_chat calls with cached_get_chat for improved performance and update requirements.txt to uncomment Kurigram 2025-06-10 02:40:23 +03:00
vvzvlad 0999e86f2b add timing logs for RSS and HTML generation in api_server and rss_generator 2025-04-27 18:40:31 +04:00
vvzvlad be28a11db6 implement connection handling in TelegramClient: add disconnect tracking and application restart on multiple disconnections 2025-04-27 18:31:29 +04:00
vvzvlad f499dc56a7 refactor api_server: comment out redundant log for cached media files 2025-04-27 18:22:31 +04:00
vvzvlad 45e9ac7a1e refactor post_parser: add line breaks for improved readability in post content 2025-04-27 13:06:39 +04:00
vvzvlad e0bb640a73 refactor post_parser: enhance post text and poll formatting, improve HTML structure 2025-04-26 13:15:03 +04:00
vvzvlad 508be049d9 refactor post_parser: streamline content body construction for improved readability 2025-04-26 12:55:38 +04:00
vvzvlad 36236d4407 refactor post_parser: clean up whitespace and improve PDF placeholder handling 2025-04-26 12:52:18 +04:00
vvzvlad 1bd36b32d8 refactor post_parser: wrap media content in a div for better HTML structure 2025-04-26 12:51:37 +04:00
vvzvlad c340d22fc4 improve error logging 2025-04-26 12:46:22 +04:00
vvzvlad 721302d2e6 oops 2025-04-26 12:42:27 +04:00
vvzvlad b9d5e839df refactor post_parser and rss_generator:move media in body render 2025-04-26 12:38:52 +04:00
vvzvlad e9949a7eab update pylint configurations in test 2025-04-26 02:34:08 +04:00
vvzvlad 58253e15e0 add test for get_all_possible_flags 2025-04-26 02:31:16 +04:00
vvzvlad 422a3925ff fix bug 2025-04-25 16:59:21 +04:00
vvzvlad db0d1a6dc2 refactor _generate_html_body to streamline reply info handling and improve logging 2025-04-20 19:07:30 +03:00
vvzvlad 5c34d62c11 EPIC: support REPLY_TO in rss 2025-04-19 04:49:01 +03:00
vvzvlad 80d6cc5d5a add line break in debug HTML 2025-04-19 04:33:47 +03:00