Skip to content

Commit

Permalink
Fix name sub and change usage
Browse files Browse the repository at this point in the history
- use ffprobe to know document type first

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Sep 10, 2024
1 parent 96b2335 commit 198f736
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,17 @@ quotes, even if it's `Int`, `Bool` or `List`.
- `USE_SERVICE_ACCOUNTS`: Whether to use Service Accounts or not, with google-api-python-client. For this to work
see [Using Service Accounts](https://github.com/anasty17/mirror-leech-telegram-bot#generate-service-accounts-what-is-service-account)
section below. Default is `False`. `Bool`
- `NAME_SUBSTITUTE`: Add word/letter/sentense/pattern to remove or replace with other words with sensitive case or without. **Notes**:
- `NAME_SUBSTITUTE`: Add word/letter/character/sentense/pattern to remove or replace with other words with sensitive case or without. **Notes**:
1. Seed will get disbaled while using this option
2. Before any character you must add `\BACKSLASH`, those are the characters: `\^$.|?*+()[]{}-`
* Example-1: `text : code : s | mirror : leech | tea : : s | clone`
- text will get replaced by code with sensitive case
- mirror will get replaced by leech
- tea will get removed with sensitive case
- clone will get removed
* Example-2: `\(text\) | \[test\] : test | \\text\\ : text : s`
- `(text)` will get removed
- `[test]` will get replaced by test
- `\text\` will get replaced by text with sensitive case
* Example: script/code/s | mirror/leech | tea/ /s | clone | cpu/ | \[mltb\]/mltb | \\text\\/text/s
- script will get replaced by code with sensitive case
- mirror will get replaced by leech
- tea will get replaced by space with sensitive case
- clone will get removed
- cpu will get replaced by space
- [mltb] will get replaced by mltb
- \text\ will get replaced by text with sensitive case

**3. GDrive Tools**

Expand Down
42 changes: 27 additions & 15 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async def before_start(self):
)
)
if self.name_sub:
self.name_sub = [x.split(" : ") for x in self.name_sub.split(" | ")]
self.name_sub = [x.split("/") for x in self.name_sub.split(" | ")]
self.seed = False
self.extension_filter = self.user_dict.get("excluded_extensions") or (
global_extension_filter
Expand Down Expand Up @@ -963,27 +963,39 @@ async def substitute(self, dl_path):
if await aiopath.isfile(dl_path):
up_dir, name = dl_path.rsplit("/", 1)
for substitution in self.name_sub:
sen = False
pattern = substitution[0]
res = (
substitution[1] if len(substitution) > 1 and substitution[1] else ""
)
sen = len(substitution) > 2 and substitution[2] == "s"
new_name = sub(rf"{pattern}", res, name, flags=I if sen else 0)
new_path = ospath.join(up_dir, new_name)
if len(substitution) > 1:
if len(substitution) > 2:
sen = substitution[2] == "s"
res = substitution[1]
elif len(substitution[1]) == 0:
res = " "
else:
res = substitution[1]
else:
res = ""
name = sub(rf"{pattern}", res, name, flags=I if sen else 0)
new_path = ospath.join(up_dir, name)
await move(dl_path, new_path)
return new_path
else:
for dirpath, _, files in await sync_to_async(walk, dl_path, topdown=False):
for file_ in files:
f_path = ospath.join(dirpath, file_)
for substitution in self.name_sub:
sen = False
pattern = substitution[0]
res = (
substitution[1]
if len(substitution) > 1 and substitution[1]
else ""
)
sen = len(substitution) > 2 and substitution[2] == "s"
new_name = sub(rf"{pattern}", res, file_, flags=I if sen else 0)
await move(f_path, ospath.join(dirpath, new_name))
if len(substitution) > 1:
if len(substitution) > 2:
sen = substitution[2] == "s"
res = substitution[1]
elif len(substitution[1]) == 0:
res = " "
else:
res = substitution[1]
else:
res = ""
file_ = sub(rf"{pattern}", res, file_, flags=I if sen else 0)
await move(f_path, ospath.join(dirpath, file_))
return dl_path
18 changes: 12 additions & 6 deletions bot/helper/ext_utils/help_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,18 @@
/cmd rcl or rclonePath -up rclonePath or rc or rcl
/cmd mrcc:rclonePath -up rcl or rc(if you have add rclone path from usetting) (to use user config)"""

name_sub = """<b>Name Substitution</b>: -ns
/cmd link -ns tea : coffee : s|ACC : : s|mP4
This will affect on all files. Format: wordToReplace : wordToReplaceWith : sensitiveCase
1. tea will get replaced by coffee with sensitive case because I have added `s` last of the option.
2. ACC will get removed because I have added nothing between to replace with sensitive case because I have added `s` last of the option.
3. mP4 will get removed because I have added nothing to replace with
name_sub = r"""<b>Name Substitution</b>: -ns
/cmd link -ns script/code/s | mirror/leech | tea/ /s | clone | cpu/ | \[mltb\]/mltb | \\text\\/text/s
This will affect on all files. Format: wordToReplace/wordToReplaceWith/sensitiveCase
Word Subtitions. You can add pattern instead of normal text. Timeout: 60 sec
NOTE: You must add \ before any character, those are the characters: \^$.|?*+()[]{}-
1. script will get replaced by code with sensitive case
2. mirror will get replaced by leech
4. tea will get replaced by space with sensitive case
5. clone will get removed
6. cpu will get replaced by space
7. [mltb] will get replaced by mltb
8. \text\ will get replaced by text with sensitive case
"""

mixed_leech = """Mixed leech: -ml
Expand Down
8 changes: 4 additions & 4 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ async def get_document_type(path):
mime_type = await sync_to_async(get_mime_type, path)
if mime_type.startswith("image"):
return False, False, True
if mime_type.startswith("audio"):
return False, True, False
if not mime_type.startswith("video") and not mime_type.endswith("octet-stream"):
return is_video, is_audio, is_image
try:
result = await cmd_exec(
[
Expand All @@ -205,6 +201,10 @@ async def get_document_type(path):
is_video = True
except Exception as e:
LOGGER.error(f"Get Document Type: {e}. Mostly File not found! - File: {path}")
if mime_type.startswith("audio"):
return False, True, False
if not mime_type.startswith("video") and not mime_type.endswith("octet-stream"):
return is_video, is_audio, is_image
if mime_type.startswith("video"):
is_video = True
return is_video, is_audio, is_image
Expand Down
13 changes: 6 additions & 7 deletions bot/modules/users_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,15 +748,14 @@ async def edit_user_settings(client, query):
buttons.data_button("Close", f"userset {user_id} close")
emsg = r"""Word Subtitions. You can add pattern instead of normal text. Timeout: 60 sec
NOTE: You must add \ before any character, those are the characters: \^$.|?*+()[]{}-
Example-1: text : code : s|mirror : leech|tea : : s|clone
1. text will get replaced by code with sensitive case
Example: script/code/s | mirror/leech | tea/ /s | clone | cpu/ | \[mltb\]/mltb | \\text\\/text/s
1. script will get replaced by code with sensitive case
2. mirror will get replaced by leech
4. tea will get removed with sensitive case
4. tea will get replaced by space with sensitive case
5. clone will get removed
Example-2: \(text\) | \[test\] : test | \\text\\ : text : s
1. (text) will get removed
2. [test] will get replaced by test
3. \text\ will get replaced by text with sensitive case
6. cpu will get replaced by space
7. [mltb] will get replaced by mltb
8. \text\ will get replaced by text with sensitive case
"""
emsg += f"Your Current Value is {user_dict.get('name_sub') or 'not added yet!'}"
await edit_message(
Expand Down

0 comments on commit 198f736

Please sign in to comment.