feat(hooks): run configurable command after each successful download - #55
Open
rfsbraz wants to merge 1 commit into
Open
feat(hooks): run configurable command after each successful download#55rfsbraz wants to merge 1 commit into
rfsbraz wants to merge 1 commit into
Conversation
Contributor
Testing this PROption 1 — Docker Compose override: Create a services:
telegram-downloader:
build: https://github.com/rfsbraz/telegram-downloader.git#refs/pull/55/headThen run: docker compose up --buildOption 2 — Direct build and run: docker build https://github.com/rfsbraz/telegram-downloader.git#refs/pull/55/head -t telegram-downloader:pr-55
docker run --rm -it telegram-downloader:pr-55 |
There was a problem hiding this comment.
Pull request overview
Adds a configurable post-download hook that runs an external command after each successful download, enabling integrations (extract/convert/scan/move) without modifying the downloader core (Fixes #35).
Changes:
- Introduces
post_download_hookandpost_download_hook_timeoutconfiguration, loaded from env/YAML and documented in README. - Executes the hook after successful downloads with placeholder substitution and timeout handling.
- Adds unit tests for command building/substitution and hook execution semantics (success, non-zero exit, missing command, timeout, invalid syntax).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/client/test_hooks.py | Adds unit tests for placeholder substitution, injection safety, execution, and timeout behavior. |
| src/main.py | Invokes the post-download hook after each successful download. |
| src/config/schema.py | Adds config schema fields for the hook template and timeout. |
| src/config/loader.py | Ensures env var parsing supports the new hook fields and timeout integer coercion. |
| src/client/hooks.py | Implements hook command building and async subprocess execution with logging + timeout. |
| README.md | Documents the new hook settings and available placeholders. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+52
to
+56
| argv = [] | ||
| for token in shlex.split(template): | ||
| for name in PLACEHOLDERS: | ||
| token = token.replace("{" + name + "}", values[name]) | ||
| argv.append(token) |
Comment on lines
+93
to
+101
| try: | ||
| process = await asyncio.create_subprocess_exec( | ||
| *argv, | ||
| stdout=asyncio.subprocess.PIPE, | ||
| stderr=asyncio.subprocess.PIPE, | ||
| ) | ||
| except Exception as e: | ||
| log.warning(f"Post-download hook failed to start for {file_path.name}: {e}") | ||
| return |
Comment on lines
+103
to
+116
| try: | ||
| stdout, stderr = await asyncio.wait_for( | ||
| process.communicate(), timeout=timeout | ||
| ) | ||
| except asyncio.TimeoutError: | ||
| process.kill() | ||
| await process.wait() | ||
| log.warning( | ||
| f"Post-download hook timed out after {timeout}s for {file_path.name}" | ||
| ) | ||
| return | ||
| except Exception as e: | ||
| log.warning(f"Post-download hook failed for {file_path.name}: {e}") | ||
| return |
Comment on lines
+116
to
+118
| # Post-download hook: shell command run after each successful download. | ||
| # Supports placeholders: {file}, {dir}, {filename}, {extension}, {source}, {size} | ||
| post_download_hook: Optional[str] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #35
TDL_POST_DOWNLOAD_HOOKsetting runs a command after each successful download, with{file},{dir},{filename},{extension},{source}and{size}placeholdersTDL_POST_DOWNLOAD_HOOK_TIMEOUT(default 300s) kills hung hooks