Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
- Fix jdownloader google drive link when token not exists
- Fix convert media while mirror 1 file only
- Fix seed when convert media enabled
- Fix upload old files that have been converted

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Jan 23, 2024
1 parent 7020b81 commit b9cc330
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 228 deletions.
159 changes: 105 additions & 54 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from aiofiles.os import path as aiopath, remove
from aiofiles.os import path as aiopath, remove, makedirs
from asyncio import sleep, create_subprocess_exec
from asyncio.subprocess import PIPE
from os import walk, path as ospath
from secrets import token_urlsafe
from aioshutil import move, copy2

from bot import (
DOWNLOAD_DIR,
Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(self):
self.splitSize = 0
self.maxSplitSize = 0
self.multi = 0
self.size = 0
self.isLeech = False
self.isQbit = False
self.isJd = False
Expand All @@ -103,6 +105,7 @@ def __init__(self):
self.forceRun = False
self.forceDownload = False
self.forceUpload = False
self.isTorrent = False
self.suproc = None
self.thumb = None
self.extensionFilter = []
Expand Down Expand Up @@ -150,7 +153,7 @@ async def beforeStart(self):
if "excluded_extensions" not in self.userDict
else ["aria2", "!qB"]
)
if not self.isYtDlp:
if not self.isYtDlp and not self.isJd:
if self.link not in ["rcl", "gdl"]:
await self.isTokenExists(self.link, "dl")
elif self.link == "rcl":
Expand Down Expand Up @@ -395,12 +398,12 @@ async def initBulk(self, input_list, bulk_start, bulk_end, obj):
"Reply to text file or to telegram message that have links seperated by new line!",
)

async def proceedExtract(self, dl_path, size, gid):
async def proceedExtract(self, dl_path, gid):
pswd = self.extract if isinstance(self.extract, str) else ""
try:
LOGGER.info(f"Extracting: {self.name}")
async with task_dict_lock:
task_dict[self.mid] = ExtractStatus(self, size, gid)
task_dict[self.mid] = ExtractStatus(self, gid)
if await aiopath.isdir(dl_path):
if self.seed:
self.newDir = f"{self.dir}10000"
Expand Down Expand Up @@ -507,15 +510,15 @@ async def proceedExtract(self, dl_path, size, gid):
self.newDir = ""
return dl_path

async def proceedCompress(self, dl_path, size, gid):
async def proceedCompress(self, dl_path, gid):
pswd = self.compress if isinstance(self.compress, str) else ""
if self.seed and self.isLeech:
if self.seed and self.isLeech and not self.newDir:
self.newDir = f"{self.dir}10000"
up_path = f"{self.newDir}/{self.name}.zip"
else:
up_path = f"{dl_path}.zip"
async with task_dict_lock:
task_dict[self.mid] = ZipStatus(self, size, gid)
task_dict[self.mid] = ZipStatus(self, gid)
if self.equalSplits:
size = await get_path_size(dl_path)
parts = -(-size // self.splitSize)
Expand Down Expand Up @@ -560,7 +563,7 @@ async def proceedCompress(self, dl_path, size, gid):
LOGGER.error(f"{stderr}. Unable to zip this path: {dl_path}")
return dl_path

async def proceedSplit(self, up_dir, m_size, o_files, size, gid):
async def proceedSplit(self, up_dir, m_size, o_files, gid):
checked = False
for dirpath, _, files in await sync_to_async(walk, up_dir, topdown=False):
for file_ in files:
Expand All @@ -570,7 +573,7 @@ async def proceedSplit(self, up_dir, m_size, o_files, size, gid):
if not checked:
checked = True
async with task_dict_lock:
task_dict[self.mid] = SplitStatus(self, size, gid)
task_dict[self.mid] = SplitStatus(self, gid)
LOGGER.info(f"Splitting: {self.name}")
res = await split_file(
f_path, f_size, dirpath, self.splitSize, self
Expand All @@ -593,7 +596,7 @@ async def proceedSplit(self, up_dir, m_size, o_files, size, gid):
m_size.append(f_size)
o_files.append(file_)

async def generateSampleVideo(self, dl_path, size, gid):
async def generateSampleVideo(self, dl_path, gid, unwanted_files):
data = self.sampleVideo.split(":") if isinstance(self.sampleVideo, str) else ""
if data:
sample_duration = int(data[0]) if data[0] else 60
Expand All @@ -603,7 +606,7 @@ async def generateSampleVideo(self, dl_path, size, gid):
part_duration = 4

async with task_dict_lock:
task_dict[self.mid] = SampleVideoStatus(self, size, gid)
task_dict[self.mid] = SampleVideoStatus(self, gid)

async with cpu_eater_lock:
checked = False
Expand All @@ -612,15 +615,30 @@ async def generateSampleVideo(self, dl_path, size, gid):
if not checked:
checked = True
LOGGER.info(f"Creating Sample video: {self.name}")
return await createSampleVideo(
res = await createSampleVideo(
self, dl_path, sample_duration, part_duration, True
)
if res:
newfolder = ospath.splitext(dl_path)[0]
name = dl_path.rsplit("/", 1)[1]
if self.seed:
self.newDir = f"{self.dir}10000"
newfolder = newfolder.replace(self.dir, self.newDir)
await makedirs(newfolder, exist_ok=True)
if self.seed:
await copy2(dl_path, f"{newfolder}/{name}")
else:
await move(dl_path, f"{newfolder}/{name}")
await move(res, f"{newfolder}/SAMPLE.{name}")
return self.newDir
else:
for dirpath, _, files in await sync_to_async(
walk, dl_path, topdown=False
):
for file_ in files:
f_path = ospath.join(dirpath, file_)
if f_path in unwanted_files:
continue
if (await get_document_type(f_path))[0]:
if not checked:
checked = True
Expand All @@ -632,9 +650,9 @@ async def generateSampleVideo(self, dl_path, size, gid):
return res
return dl_path

async def convertMedia(self, up_dir, size, gid):
async def convertMedia(self, dl_path, gid, o_files, m_size):
async with task_dict_lock:
task_dict[self.mid] = MediaConvertStatus(self, size, gid)
task_dict[self.mid] = MediaConvertStatus(self, gid)

fvext = []
if self.convertVideo:
Expand Down Expand Up @@ -673,46 +691,79 @@ async def convertMedia(self, up_dir, size, gid):
astatus = ""

checked = False
for dirpath, _, files in await sync_to_async(walk, up_dir, topdown=False):
for file_ in files:
if self.cancelled:
return False
f_path = ospath.join(dirpath, file_)
is_video, is_audio, _ = await get_document_type(f_path)
if (
is_video
and vext
and not f_path.endswith(f".{vext}")
and (
vstatus == "+"
and f_path.endswith(tuple(fvext))
or vstatus == "-"
and not f_path.endswith(tuple(fvext))
or not vstatus
)
):
if not checked:
checked = True
LOGGER.info(f"Converting: {self.name}")
await convert_video(self, f_path, vext)
if self.cancelled:

async def proceedConvert(m_path):
nonlocal checked
is_video, is_audio, _ = await get_document_type(m_path)
if (
is_video
and vext
and not m_path.endswith(f".{vext}")
and (
vstatus == "+"
and m_path.endswith(tuple(fvext))
or vstatus == "-"
and not m_path.endswith(tuple(fvext))
or not vstatus
)
):
if not checked:
checked = True
LOGGER.info(f"Converting: {self.name}")
res = await convert_video(self, m_path, vext)
return False if self.cancelled else res
elif (
is_audio
and not is_video
and not m_path.endswith(f".{aext}")
and (
astatus == "+"
and m_path.endswith(tuple(faext))
or astatus == "-"
and not m_path.endswith(tuple(faext))
or not astatus
)
):
if not checked:
checked = True
LOGGER.info(f"Converting: {self.name}")
res = await convert_audio(self, m_path, aext)
return False if self.cancelled else res
else:
return False

if await aiopath.isfile(dl_path):
output_file = await proceedConvert(dl_path)
if output_file:
if self.seed:
self.newDir = f"{self.dir}10000"
new_output_file = output_file.replace(self.dir, self.newDir)
await makedirs(self.newDir, exist_ok=True)
await move(output_file, new_output_file)
return new_output_file
else:
try:
await remove(dl_path)
except:
return False
elif (
is_audio
and not is_video
and not f_path.endswith(f".{aext}")
and (
astatus == "+"
and f_path.endswith(tuple(faext))
or astatus == "-"
and not f_path.endswith(tuple(faext))
or not astatus
)
):
if not checked:
checked = True
LOGGER.info(f"Converting: {self.name}")
await convert_audio(self, f_path, aext)
return output_file
return dl_path
else:
for dirpath, _, files in await sync_to_async(walk, dl_path, topdown=False):
for file_ in files:
if self.cancelled:
return False
return True
f_path = ospath.join(dirpath, file_)
res = await proceedConvert(f_path)
if res:
if self.seed and not self.newDir:
o_files.append(f_path)
fsize = await aiopath.getsize(f_path)
m_size.append(fsize)
else:
try:
await remove(f_path)
except:
return False

return dl_path
47 changes: 24 additions & 23 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from PIL import Image
from aiofiles.os import remove, path as aiopath, makedirs
from aioshutil import move
from asyncio import create_subprocess_exec, gather, wait_for
from asyncio.subprocess import PIPE
from os import path as ospath, cpu_count
Expand All @@ -18,41 +17,47 @@ async def convert_video(listener, video_file, ext):
output = f"{base_name}.{ext}"
cmd = ["ffmpeg", "-i", video_file, "-c", "copy", output]
if listener.cancelled:
return
return False
async with subprocess_lock:
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.suproc.communicate()
if listener.cancelled:
return
return False
code = listener.suproc.returncode
if code == 0:
await remove(video_file)
elif code != -9:
return output
elif code == -9:
return False
else:
stderr = stderr.decode().strip()
LOGGER.error(
f"{stderr}. Something went wrong while converting video, mostly file is corrupted. Path: {video_file}"
)
return False


async def convert_audio(listener, audio_file, ext):
base_name = ospath.splitext(audio_file)[0]
output = f"{base_name}.{ext}"
cmd = ["ffmpeg", "-i", audio_file, output]
if listener.cancelled:
return
return False
async with subprocess_lock:
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.suproc.communicate()
if listener.cancelled:
return
return False
code = listener.suproc.returncode
if code == 0:
await remove(audio_file)
elif code != -9:
return output
elif code == -9:
return False
else:
stderr = stderr.decode().strip()
LOGGER.error(
f"{stderr}. Something went wrong while converting audio, mostly file is corrupted. Path: {audio_file}"
)
return False


async def createThumb(msg, _id=""):
Expand Down Expand Up @@ -356,7 +361,9 @@ async def split_file(
if listener.cancelled:
return False
code = listener.suproc.returncode
if code != 0:
if code == -9:
return False
elif code != 0:
stderr = stderr.decode().strip()
try:
await remove(out_path)
Expand Down Expand Up @@ -432,15 +439,15 @@ async def split_file(
if listener.cancelled:
return False
code = listener.suproc.returncode
if code != 0:
if code == -9:
return False
elif code != 0:
stderr = stderr.decode().strip()
LOGGER.error(f"{stderr}. Split Document: {path}")
return True


async def createSampleVideo(
listener, video_file, sample_duration, part_duration, oneFile=False
):
async def createSampleVideo(listener, video_file, sample_duration, part_duration):
filter_complex = ""
dir, name = video_file.rsplit("/", 1)
output_file = f"{dir}/SAMPLE.{name}"
Expand Down Expand Up @@ -491,19 +498,13 @@ async def createSampleVideo(
return False
listener.suproc = await create_subprocess_exec(*cmd, stderr=PIPE)
_, stderr = await listener.suproc.communicate()
if listener.cancelled:
return False
code = listener.suproc.returncode
if code == -9:
return False
elif code == 0:
if oneFile:
newDir, _ = ospath.splitext(video_file)
await makedirs(newDir, exist_ok=True)
await gather(
move(video_file, f"{newDir}/{name}"),
move(output_file, f"{newDir}/SAMPLE.{name}"),
)
return newDir
return True
return output_file
else:
stderr = stderr.decode().strip()
LOGGER.error(
Expand Down
Loading

0 comments on commit b9cc330

Please sign in to comment.