Skip to content

Commit 442b3aa

Browse files
committed
Dec 13
1 parent 6c80b21 commit 442b3aa

20 files changed

Lines changed: 212 additions & 127 deletions

File tree

bot/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def format(self, record: LogRecord) -> str:
9696
rss_dict = {}
9797
auth_chats = {}
9898
excluded_extensions = ["aria2", "!qB"]
99+
included_extensions = []
99100
drives_names = []
100101
drives_ids = []
101102
index_urls = []

bot/core/config_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Config:
1818
DATABASE_URL: str = ""
1919
DEFAULT_UPLOAD: str = "gd"
2020
EXCLUDED_EXTENSIONS: str = ""
21+
INCLUDED_EXTENSIONS: str = ""
2122
FFMPEG_CMDS: ClassVar[dict[str, list[str]]] = {}
2223
FILELION_API: str = ""
2324
GDRIVE_ID: str = ""

bot/core/startup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
drives_ids,
1515
drives_names,
1616
excluded_extensions,
17+
included_extensions,
1718
index_urls,
1819
nzb_options,
1920
qbit_options,
@@ -269,6 +270,11 @@ async def update_variables():
269270
x = x.lstrip(".")
270271
excluded_extensions.append(x.strip().lower())
271272

273+
if Config.INCLUDED_EXTENSIONS:
274+
fx = Config.INCLUDED_EXTENSIONS.split()
275+
for x in fx:
276+
x = x.lstrip(".")
277+
included_extensions.append(x.strip().lower())
272278
if Config.GDRIVE_ID:
273279
drives_names.append("Main")
274280
drives_ids.append(Config.GDRIVE_ID)

bot/helper/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
cores,
2121
cpu_eater_lock,
2222
excluded_extensions,
23+
included_extensions,
2324
intervals,
2425
multi_tags,
2526
task_dict,
@@ -137,6 +138,7 @@ def __init__(self):
137138
self.subproc = None
138139
self.thumb = None
139140
self.excluded_extensions = []
141+
self.included_extensions = []
140142
self.files_to_proceed = []
141143
self.is_super_chat = self.message.chat.type.name in [
142144
"SUPERGROUP",
@@ -227,6 +229,9 @@ async def before_start(self):
227229
if "EXCLUDED_EXTENSIONS" not in self.user_dict
228230
else ["aria2", "!qB"]
229231
)
232+
self.included_extensions = self.user_dict.get("INCLUDED_EXTENSIONS") or (
233+
included_extensions if "INCLUDED_EXTENSIONS" not in self.user_dict else []
234+
)
230235
if not self.rc_flags:
231236
if self.user_dict.get("RCLONE_FLAGS"):
232237
self.rc_flags = self.user_dict["RCLONE_FLAGS"]

bot/helper/ext_utils/bot_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ def arg_parser(items, arg_base):
191191
else:
192192
sub_list = []
193193
for j in range(i + 1, total):
194-
if items[j] in arg_base and part != "-c" != items[j]:
194+
if items[j] in arg_base:
195+
if part == "-c" and items[j] == "-c":
196+
sub_list.append(items[j])
197+
continue
195198
if part in bool_arg_set and not sub_list:
196199
arg_base[part] = True
197200
break

bot/helper/ext_utils/files_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,20 @@ def get_mime_type(file_path: str) -> str:
237237

238238
async def remove_excluded_files(fpath, ee):
239239
for root, _, files in await sync_to_async(walk, fpath):
240+
if root.strip().endswith("/yt-dlp-thumb"):
241+
continue
240242
for f in files:
241243
if f.strip().lower().endswith(tuple(ee)):
242244
await remove(ospath.join(root, f))
243245

246+
async def remove_non_included_files(fpath, ie):
247+
for root, _, files in await sync_to_async(walk, fpath):
248+
if root.strip().endswith("/yt-dlp-thumb"):
249+
continue
250+
for f in files:
251+
if f.strip().lower().endswith(tuple(ie)):
252+
continue
253+
await remove(ospath.join(root, f))
244254

245255
async def join_files(opath):
246256
files = await listdir(opath)
@@ -329,7 +339,7 @@ async def _sevenz_progress(self):
329339
break
330340
line = line.decode().strip()
331341
if match := re_search(pattern, line):
332-
self._listener.subsize = int(match[1] or match[2])
342+
self._listener.subsize = int(match[1] or match[2] or match[3])
333343
s = b""
334344
while not (
335345
self._listener.is_cancelled

bot/helper/ext_utils/help_messages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@
392392
"INDEX_URL": "Send Index URL. Timeout: 60 sec",
393393
"UPLOAD_PATHS": "Send Dict of keys that have path values. Example: {'path 1': 'remote:rclonefolder', 'path 2': 'gdrive1 id', 'path 3': 'tg chat id', 'path 4': 'mrcc:remote:', 'path 5': b:@username} . Timeout: 60 sec",
394394
"EXCLUDED_EXTENSIONS": "Send excluded extensions separated by space without dot at beginning. Timeout: 60 sec",
395+
"INCLUDED_EXTENSIONS": "Send included extensions separated by space without dot at beginning. Timeout: 60 sec",
395396
"NAME_SUBSTITUTE": r"""Word Subtitions. You can add pattern instead of normal text. Timeout: 60 sec
396397
NOTE: You must add \ before any character, those are the characters: \^$.|?*+()[]{}-
397398
Example: script/code/s | mirror/leech | tea/ /s | clone | cpu/ | \[mltb\]/mltb | \\text\\/text/s

bot/helper/ext_utils/media_utils.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import contextlib
2-
from asyncio import create_subprocess_exec, gather, wait_for
2+
from asyncio import create_subprocess_exec, gather, sleep, wait_for
33
from asyncio.subprocess import PIPE
44
from os import path as ospath
55
from re import escape
@@ -11,7 +11,7 @@
1111
from aioshutil import rmtree
1212
from PIL import Image
1313

14-
from bot import DOWNLOAD_DIR, LOGGER, cpu_no
14+
from bot import DOWNLOAD_DIR, LOGGER, cpu_no, threads, cores
1515

1616
from .bot_utils import cmd_exec, sync_to_async
1717
from .files_utils import get_mime_type, is_archive, is_archive_split
@@ -131,8 +131,8 @@ async def take_ss(video_file, ss_nb) -> bool:
131131
output = f"{dirpath}/SS.{name}_{i:02}.png"
132132
cmd = [
133133
"taskset",
134-
"-c",
135-
f"{cores}",
134+
"-c",
135+
f"{cores}",
136136
"xtra",
137137
"-hide_banner",
138138
"-loglevel",
@@ -146,7 +146,7 @@ async def take_ss(video_file, ss_nb) -> bool:
146146
"-frames:v",
147147
"1",
148148
"-threads",
149-
f"{max(1, cpu_no // 2)}",
149+
f"{threads}",
150150
output,
151151
]
152152
cap_time += interval
@@ -176,8 +176,8 @@ async def get_audio_thumbnail(audio_file):
176176
output = ospath.join(output_dir, f"{time()}.jpg")
177177
cmd = [
178178
"taskset",
179-
"-c",
180-
f"{cores}",
179+
"-c",
180+
f"{cores}",
181181
"xtra",
182182
"-hide_banner",
183183
"-loglevel",
@@ -188,7 +188,7 @@ async def get_audio_thumbnail(audio_file):
188188
"-vcodec",
189189
"copy",
190190
"-threads",
191-
f"{max(1, cpu_no // 2)}",
191+
f"{threads}",
192192
output,
193193
]
194194
try:
@@ -218,8 +218,8 @@ async def get_video_thumbnail(video_file, duration):
218218
duration = duration // 2
219219
cmd = [
220220
"taskset",
221-
"-c",
222-
f"{cores}",
221+
"-c",
222+
f"{cores}",
223223
"xtra",
224224
"-hide_banner",
225225
"-loglevel",
@@ -264,8 +264,8 @@ async def get_multiple_frames_thumbnail(video_file, layout, keep_screenshots):
264264
output = ospath.join(output_dir, f"{time()}.jpg")
265265
cmd = [
266266
"taskset",
267-
"-c",
268-
f"{cores}",
267+
"-c",
268+
f"{cores}",
269269
"xtra",
270270
"-hide_banner",
271271
"-loglevel",
@@ -283,7 +283,7 @@ async def get_multiple_frames_thumbnail(video_file, layout, keep_screenshots):
283283
"-f",
284284
"mjpeg",
285285
"-threads",
286-
f"{max(1, cpu_no // 2)}",
286+
f"{threads}",
287287
output,
288288
]
289289
try:
@@ -388,6 +388,7 @@ async def _ffmpeg_progress(self):
388388
except Exception:
389389
self._progress_raw = 0
390390
self._eta_raw = 0
391+
391392

392393
async def ffmpeg_cmds(self, ffmpeg, f_path):
393394
self.clear()
@@ -483,8 +484,8 @@ async def convert_video(self, video_file, ext, retry=False):
483484
if retry:
484485
cmd = [
485486
"taskset",
486-
"-c",
487-
f"{cores}",
487+
"-c",
488+
f"{cores}",
488489
"xtra",
489490
"-hide_banner",
490491
"-loglevel",
@@ -500,7 +501,7 @@ async def convert_video(self, video_file, ext, retry=False):
500501
"-c:a",
501502
"aac",
502503
"-threads",
503-
f"{max(1, cpu_no // 2)}",
504+
f"{threads}",
504505
output,
505506
]
506507
if ext == "mp4":
@@ -512,8 +513,8 @@ async def convert_video(self, video_file, ext, retry=False):
512513
else:
513514
cmd = [
514515
"taskset",
515-
"-c",
516-
f"{cores}",
516+
"-c",
517+
f"{cores}",
517518
"xtra",
518519
"-hide_banner",
519520
"-loglevel",
@@ -527,7 +528,7 @@ async def convert_video(self, video_file, ext, retry=False):
527528
"-c",
528529
"copy",
529530
"-threads",
530-
f"{max(1, cpu_no // 2)}",
531+
f"{threads}",
531532
output,
532533
]
533534
if self._listener.is_cancelled:
@@ -567,8 +568,8 @@ async def convert_audio(self, audio_file, ext):
567568
output = f"{base_name}.{ext}"
568569
cmd = [
569570
"taskset",
570-
"-c",
571-
f"{cores}",
571+
"-c",
572+
f"{cores}",
572573
"xtra",
573574
"-hide_banner",
574575
"-loglevel",
@@ -578,7 +579,7 @@ async def convert_audio(self, audio_file, ext):
578579
"-i",
579580
audio_file,
580581
"-threads",
581-
f"{max(1, cpu_no // 2)}",
582+
f"{threads}",
582583
output,
583584
]
584585
if self._listener.is_cancelled:
@@ -641,8 +642,8 @@ async def sample_video(self, video_file, sample_duration, part_duration):
641642

642643
cmd = [
643644
"taskset",
644-
"-c",
645-
f"{cores}",
645+
"-c",
646+
f"{cores}",
646647
"xtra",
647648
"-hide_banner",
648649
"-loglevel",
@@ -662,7 +663,7 @@ async def sample_video(self, video_file, sample_duration, part_duration):
662663
"-c:a",
663664
"aac",
664665
"-threads",
665-
f"{max(1, cpu_no // 2)}",
666+
f"{threads}",
666667
output_file,
667668
]
668669

@@ -706,8 +707,8 @@ async def split(self, f_path, file_, parts, split_size):
706707
out_path = f_path.replace(file_, f"{base_name}.part{i:03}{extension}")
707708
cmd = [
708709
"taskset",
709-
"-c",
710-
f"{cores}",
710+
"-c",
711+
f"{cores}",
711712
"xtra",
712713
"-hide_banner",
713714
"-loglevel",
@@ -731,7 +732,7 @@ async def split(self, f_path, file_, parts, split_size):
731732
"-c",
732733
"copy",
733734
"-threads",
734-
f"{max(1, cpu_no // 2)}",
735+
f"{threads}",
735736
out_path,
736737
]
737738
if not multi_streams:

bot/helper/listeners/task_listener.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
get_path_size,
3333
join_files,
3434
remove_excluded_files,
35+
remove_non_included_files
3536
)
3637
from bot.helper.ext_utils.links_utils import is_gdrive_id
3738
from bot.helper.ext_utils.status_utils import get_readable_file_size
@@ -196,10 +197,14 @@ async def on_download_complete(self):
196197
else:
197198
up_dir = self.dir
198199
up_path = dl_path
199-
await remove_excluded_files(
200-
self.up_dir or self.dir,
201-
self.excluded_extensions,
202-
)
200+
if not self.included_extensions:
201+
await remove_excluded_files(
202+
self.up_dir or self.dir, self.excluded_extensions
203+
)
204+
else:
205+
await remove_non_included_files(
206+
self.up_dir or self.dir, self.included_extensions
207+
)
203208
if not Config.QUEUE_ALL:
204209
async with queue_dict_lock:
205210
if self.mid in non_queued_dl:
@@ -217,7 +222,10 @@ async def on_download_complete(self):
217222
self.name = up_path.replace(f"{up_dir}/", "").split("/", 1)[0]
218223
self.size = await get_path_size(up_dir)
219224
self.clear()
220-
await remove_excluded_files(up_dir, self.excluded_extensions)
225+
if not self.included_extensions:
226+
await remove_excluded_files(up_dir, self.excluded_extensions)
227+
else:
228+
await remove_non_included_files(up_dir, self.included_extensions)
221229

222230
if self.watermark:
223231
up_path = await self.proceed_watermark(

0 commit comments

Comments
 (0)