Skip to content

Conversation

@5hojib
Copy link
Member

@5hojib 5hojib commented Oct 21, 2025

Summary by Sourcery

Introduce a new NAME_PREFIX feature to allow custom filename prefixes throughout the download and upload pipeline, remove the deprecated LEECH_FILENAME_PREFIX setting, implement prefix application logic, and integrate an InstagramResolver for video downloads via InstaDL API.

New Features:

  • Add NAME_PREFIX option to prefix leeched/downloaded file names via UI, config, and CLI (-np)
  • Integrate InstagramResolver with TrueLinkResolver for downloading Instagram videos using external InstaDL API

Enhancements:

  • Rename and remove legacy LEECH_FILENAME_PREFIX usage in code, replacing with NAME_PREFIX
  • Implement proceed_name_prefix method to apply filename prefixes in TaskListener download flow
  • Register InstagramResolver during startup

Documentation:

  • Update configuration docs and config_sample to replace LEECH_FILENAME_PREFIX with NAME_PREFIX
  • Revise help messages to reference NAME_PREFIX instead of LEECH_FILENAME_PREFIX

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 21, 2025

Reviewer's Guide

This PR introduces a unified NAME_PREFIX feature to replace the old LEECH_FILENAME_PREFIX, implements logic to apply that prefix to downloaded and leeched files, cleans up legacy prefix code in the uploader, and integrates an Instagram resolver into the TrueLink framework.

Sequence diagram for applying NAME_PREFIX during file processing

sequenceDiagram
participant Listener
participant HelperCommon
participant TaskListener
participant FileSystem

Listener->>HelperCommon: Set name_prefix from user_dict or Config
TaskListener->>HelperCommon: Call proceed_name_prefix(dl_path)
HelperCommon->>FileSystem: Rename file(s) with name_prefix if needed
FileSystem-->>HelperCommon: Return new path
HelperCommon-->>TaskListener: Return updated path
Loading

Class diagram for NAME_PREFIX unification and InstagramResolver addition

classDiagram
class Config {
  JD_PASS: str
  IS_TEAM_DRIVE: bool
  LEECH_DUMP_CHAT: list[str]
  LEECH_SPLIT_SIZE: int
  MEDIA_GROUP: bool
  HYBRID_LEECH: bool
  INSTADL_API: str
  HEROKU_APP_NAME: str
  HEROKU_API_KEY: str
  NAME_PREFIX: str
  _convert(key, value)
}
class InstagramResolver {
  DOMAINS: list[str]
  resolve(url: str): LinkResult | FolderResult
}
Config <|-- InstagramResolver: uses
Loading

Class diagram for removal of LEECH_FILENAME_PREFIX and related uploader changes

classDiagram
class TelegramUploader {
  _media_dict: dict
  _last_msg_in_group: bool
  _up_path: str
  _user_dump: str
  _lcaption: str
  _media_group: bool
  _nprefix: str
  _user_settings()
  _prepare_file(file_, dirpath)
}
Loading

File-Level Changes

Change Details Files
Deprecate LEECH_FILENAME_PREFIX in favor of NAME_PREFIX
  • Removed all references to LEECH_FILENAME_PREFIX
  • Added NAME_PREFIX to Config, sample config, and documentation
  • Updated help messages and user settings menu to reflect NAME_PREFIX
  • Extended mirror_leech and ytdlp modules to accept -np CLI argument
bot/core/config_manager.py
config_sample.py
docs/CONFIGURATIONS.md
bot/helper/ext_utils/help_messages.py
bot/modules/users_settings.py
bot/modules/mirror_leech.py
bot/modules/ytdlp.py
bot/helper/mirror_leech_utils/telegram_uploader.py
Initialize and resolve name_prefix in common listener
  • Added name_prefix attribute in initializer
  • Resolved NAME_PREFIX from user_dict or Config in before_start
bot/helper/common.py
Implement proceed_name_prefix routine and invoke on download complete
  • Added proceed_name_prefix method to rename files/directories with prefix
  • Called proceed_name_prefix in task_listener after download
  • Recomputed is_file and name after renaming
bot/helper/common.py
bot/helper/listeners/task_listener.py
Clean up legacy prefix logic in TelegramUploader
  • Removed _lprefix attribute and related renaming code
  • Replaced prefix logic with new _nprefix resolution
bot/helper/mirror_leech_utils/telegram_uploader.py
Integrate InstagramResolver into TrueLink
  • Imported and registered InstagramResolver with TrueLinkResolver
  • Added new insta_resolver implementing BaseResolver for Instagram API
bot/core/startup.py
bot/helper/mirror_leech_utils/download_utils/insta_resolver.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link

Summary of Changes

Hello @5hojib, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new capability for the bot to download content directly from Instagram by leveraging an external InstaDL API. Alongside this new feature, it undertakes a significant refactoring of the file naming prefix functionality, transitioning from LEECH_FILENAME_PREFIX to a more generalized NAME_PREFIX. This change centralizes the logic for applying prefixes to downloaded files and directories, ensuring a more robust and consistent naming convention across various operations.

Highlights

  • Instagram Downloader Integration: Added support for downloading content from Instagram by integrating with an external InstaDL API, enabling the bot to resolve and fetch Instagram media.
  • Filename Prefix Refactoring: The LEECH_FILENAME_PREFIX setting has been renamed to NAME_PREFIX and its implementation has been refactored for more consistent application across all downloaded files and directories.
  • Centralized Prefix Application: A new proceed_name_prefix method was introduced to handle the renaming of files and folders by applying the configured NAME_PREFIX after download completion.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Consolidate the NAME_PREFIX assignment and application code into a shared utility to reduce duplication in common.py, task_listener.py, and telegram_uploader.py.
  • Review the or-chaining fallback for NAME_PREFIX to ensure empty strings or explicitly set prefixes aren’t being skipped unintentionally.
  • In InstagramResolver.resolve, catch more specific exceptions instead of a broad except Exception so you don’t mask other errors.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consolidate the NAME_PREFIX assignment and application code into a shared utility to reduce duplication in `common.py`, `task_listener.py`, and `telegram_uploader.py`.
- Review the `or`-chaining fallback for NAME_PREFIX to ensure empty strings or explicitly set prefixes aren’t being skipped unintentionally.
- In InstagramResolver.resolve, catch more specific exceptions instead of a broad `except Exception` so you don’t mask other errors.

## Individual Comments

### Comment 1
<location> `bot/helper/common.py:1041-1044` </location>
<code_context>

         return dl_path

+    async def proceed_name_prefix(self, dl_path):
+        def add_prefix(name):
+            if name.startswith(self.name_prefix):
+                return name
+            return f"{self.name_prefix} {name}"
+
+        if self.is_file:
+            up_dir, name = dl_path.rsplit("/", 1)
+            new_name = add_prefix(name)
+            if new_name == name:
+                return dl_path
+            new_path = ospath.join(up_dir, new_name)
+            with contextlib.suppress(Exception):
</code_context>

<issue_to_address>
**suggestion:** Consider handling cases where name_prefix is empty or whitespace.

If self.name_prefix is empty or whitespace, the function may produce filenames with unintended leading spaces. Consider skipping prefixing in these cases.

```suggestion
        def add_prefix(name):
            if not self.name_prefix or self.name_prefix.strip() == "":
                return name
            if name.startswith(self.name_prefix):
                return name
            return f"{self.name_prefix} {name}"
```
</issue_to_address>

### Comment 2
<location> `bot/helper/common.py:1051-1054` </location>
<code_context>
+            if new_name == name:
+                return dl_path
+            new_path = ospath.join(up_dir, new_name)
+            with contextlib.suppress(Exception):
+                await move(dl_path, new_path)
+            return new_path
+
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Suppressing all exceptions during file move may hide critical errors.

Logging suppressed exceptions will help identify and address issues like permission errors or failed moves, reducing the risk of silent failures.

```suggestion
            new_path = ospath.join(up_dir, new_name)
            try:
                await move(dl_path, new_path)
            except Exception as e:
                import logging
                logging.error(f"Failed to move file from {dl_path} to {new_path}: {e}", exc_info=True)
            return new_path
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +1041 to +1044
def add_prefix(name):
if name.startswith(self.name_prefix):
return name
return f"{self.name_prefix} {name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider handling cases where name_prefix is empty or whitespace.

If self.name_prefix is empty or whitespace, the function may produce filenames with unintended leading spaces. Consider skipping prefixing in these cases.

Suggested change
def add_prefix(name):
if name.startswith(self.name_prefix):
return name
return f"{self.name_prefix} {name}"
def add_prefix(name):
if not self.name_prefix or self.name_prefix.strip() == "":
return name
if name.startswith(self.name_prefix):
return name
return f"{self.name_prefix} {name}"

Comment on lines +1051 to +1054
new_path = ospath.join(up_dir, new_name)
with contextlib.suppress(Exception):
await move(dl_path, new_path)
return new_path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Suppressing all exceptions during file move may hide critical errors.

Logging suppressed exceptions will help identify and address issues like permission errors or failed moves, reducing the risk of silent failures.

Suggested change
new_path = ospath.join(up_dir, new_name)
with contextlib.suppress(Exception):
await move(dl_path, new_path)
return new_path
new_path = ospath.join(up_dir, new_name)
try:
await move(dl_path, new_path)
except Exception as e:
import logging
logging.error(f"Failed to move file from {dl_path} to {new_path}: {e}", exc_info=True)
return new_path

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors LEECH_FILENAME_PREFIX to a more generic NAME_PREFIX and adds an Instagram resolver. The refactoring is a good improvement, making the prefix feature available for all downloads. However, I've identified a critical issue in the new proceed_name_prefix function related to unsafe file renaming and error suppression. There's also a high-severity bug in the user settings display logic. Additionally, I've provided some suggestions to improve code maintainability and documentation.

Comment on lines +1040 to +1065
async def proceed_name_prefix(self, dl_path):
def add_prefix(name):
if name.startswith(self.name_prefix):
return name
return f"{self.name_prefix} {name}"

if self.is_file:
up_dir, name = dl_path.rsplit("/", 1)
new_name = add_prefix(name)
if new_name == name:
return dl_path
new_path = ospath.join(up_dir, new_name)
with contextlib.suppress(Exception):
await move(dl_path, new_path)
return new_path

for dirpath, _, files in await sync_to_async(walk, dl_path, topdown=False):
for file_ in files:
f_path = ospath.join(dirpath, file_)
new_name = add_prefix(file_)
if new_name == file_:
continue
with contextlib.suppress(Exception):
await move(f_path, ospath.join(dirpath, new_name))

return dl_path

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This new function has two critical issues:

  1. Broad Exception Suppression: Using contextlib.suppress(Exception) hides all errors during file renaming (move). If the operation fails (e.g., due to invalid characters in the prefix, permissions issues, or a full disk), the error is silently ignored. This can lead to unexpected behavior. It's better to catch specific exceptions (like OSError) and log them.
  2. Unsanitized Prefix in Filename: The self.name_prefix can contain HTML tags (a feature inherited from the old LEECH_FILENAME_PREFIX), but these are not stripped before being used in a filename. Characters like < and > are invalid in filenames on most operating systems. This will cause the move operation to fail, and the failure will be hidden by suppress(Exception).

I suggest refactoring to handle exceptions gracefully and to sanitize the prefix.

    async def proceed_name_prefix(self, dl_path):
        from re import sub as re_sub
        # Sanitize the prefix by removing HTML tags, as they are invalid in filenames.
        sanitized_prefix = re_sub("<.*?>", "", self.name_prefix).strip()
        if not sanitized_prefix:
            return dl_path

        def add_prefix(name):
            if name.startswith(sanitized_prefix):
                return name
            return f"{sanitized_prefix} {name}"

        if self.is_file:
            up_dir, name = dl_path.rsplit("/", 1)
            new_name = add_prefix(name)
            if new_name == name:
                return dl_path
            new_path = ospath.join(up_dir, new_name)
            try:
                await move(dl_path, new_path)
                return new_path
            except Exception as e:
                LOGGER.error(f"Failed to apply prefix and rename '{dl_path}' to '{new_path}': {e}")
                return dl_path

        for dirpath, _, files in await sync_to_async(walk, dl_path, topdown=False):
            for file_ in files:
                f_path = ospath.join(dirpath, file_)
                new_name = add_prefix(file_)
                if new_name == file_:
                    continue
                try:
                    await move(f_path, ospath.join(dirpath, new_name))
                except Exception as e:
                    LOGGER.error(f"Failed to apply prefix and rename file '{f_path}': {e}")

        return dl_path

Comment on lines +321 to +327
if user_dict.get("NAME_PREFIX", False):
np_msg = user_dict["NAME_PREFIX"]
elif "NAME_PREFIX" not in user_dict:
np_msg = excluded_extensions
else:
np_msg = "None"
buttons.data_button("Name Prefix", f"userset {user_id} menu NAME_PREFIX")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There seems to be a copy-paste error in the logic for setting np_msg. When NAME_PREFIX is not defined in user_dict, np_msg is incorrectly assigned the value of excluded_extensions, which is a list of file extensions. This will result in a confusing value being displayed to the user for the "Name Prefix" setting. The logic should instead fall back to Config.NAME_PREFIX or "None".

Suggested change
if user_dict.get("NAME_PREFIX", False):
np_msg = user_dict["NAME_PREFIX"]
elif "NAME_PREFIX" not in user_dict:
np_msg = excluded_extensions
else:
np_msg = "None"
buttons.data_button("Name Prefix", f"userset {user_id} menu NAME_PREFIX")
if user_dict.get("NAME_PREFIX"):
np_msg = user_dict["NAME_PREFIX"]
elif "NAME_PREFIX" not in user_dict and Config.NAME_PREFIX:
np_msg = Config.NAME_PREFIX
else:
np_msg = "None"
buttons.data_button("Name Prefix", f"userset {user_id} menu NAME_PREFIX")

Comment on lines +201 to +205
self.name_prefix = (
self.name_prefix
or self.user_dict.get("NAME_PREFIX", False)
or (Config.NAME_PREFIX if "NAME_PREFIX" not in self.user_dict else "")
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for setting self.name_prefix is a bit complex and hard to follow due to chained or operators and a nested ternary expression. For better readability and maintainability, consider refactoring this into a more explicit if/elif/else structure.

        if self.name_prefix:
            pass  # Already set by command-line arg
        elif "NAME_PREFIX" in self.user_dict:
            self.name_prefix = self.user_dict["NAME_PREFIX"]
        else:
            self.name_prefix = Config.NAME_PREFIX

Comment on lines +265 to +271
if self.name_prefix:
up_path = await self.proceed_name_prefix(up_path)
if self.is_cancelled:
return
self.is_file = await aiopath.isfile(up_path)
self.name = up_path.replace(f"{up_dir}/", "").split("/", 1)[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The lines to update self.is_file and self.name are repeated three times in this function (before this block, inside this block, and after). This code duplication can make maintenance more difficult. Consider refactoring this logic into a private helper method to avoid repetition. For example:

async def _update_path_details(self, up_path, up_dir):
    self.is_file = await aiopath.isfile(up_path)
    self.name = up_path.replace(f"{up_dir}/", "").split("/", 1)[0]

Then you can call await self._update_path_details(up_path, up_dir) in the respective places.

| `HEROKU_APP_NAME` | `str` | Name of your Heroku app, used to get `BASE_URL` automatically. |
| `HEROKU_API_KEY` | `str` | API key for accessing and controlling your Heroku app. | No newline at end of file
| `HEROKU_API_KEY` | `str` | API key for accessing and controlling your Heroku app. |
| `NAME_PREFIX` | `str` | Name prefix. | No newline at end of file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The description for NAME_PREFIX is very brief. To help users understand its purpose, consider expanding it to explain what it does.

Suggested change
| `NAME_PREFIX` | `str` | Name prefix. |
| `NAME_PREFIX` | `str` | Prefix to add to all downloaded file/folder names. |

@5hojib 5hojib merged commit 5e334e5 into main Dec 25, 2025
5 checks passed
@5hojib 5hojib deleted the beta branch December 25, 2025 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant