Skip to content

Commit

Permalink
Minor fix regarding converting
Browse files Browse the repository at this point in the history
Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Jan 29, 2024
1 parent e68cba9 commit de3d837
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 104 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,37 +396,37 @@ also.
- Install docker-compose

```
sudo apt install docker-compose
sudo apt install docker-compose-plugin
```

- Build and run Docker image or to view current running image:

```
sudo docker-compose up
sudo docker compose up
```

- After editing files with nano for example (nano start.sh):
- After editing files with nano for example (nano start.sh) or git pull:

```
sudo docker-compose up --build
sudo docker compose up --build
```

- To stop the running image:

```
sudo docker-compose stop
sudo docker compose stop
```

- To run the image:

```
sudo docker-compose start
sudo docker compose start
```

- To get latest log from already running image (after mounting the folder):
- To get log from already running image (after mounting the folder):

```
sudo docker-compose up
sudo docker compose logs --follow
```

- Tutorial video from Tortoolkit repo for docker-compose and checking ports
Expand Down
2 changes: 1 addition & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

load_dotenv("config.env", override=True)

Intervals = {"status": {}, "qb": "", "jd": ""}
Intervals = {"status": {}, "qb": "", "jd": "", "stopAll": False}
QbTorrents = {}
jd_downloads = {}
DRIVES_NAMES = []
Expand Down
5 changes: 4 additions & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from aiofiles import open as aiopen
from aiofiles.os import path as aiopath, remove
from asyncio import gather, create_subprocess_exec
from asyncio import gather, create_subprocess_exec, sleep
from os import execl as osexecl
from psutil import (
disk_usage,
Expand Down Expand Up @@ -112,6 +112,7 @@ async def start(client, message):


async def restart(_, message):
Intervals["stopAll"] = True
restart_message = await sendMessage(message, "Restarting...")
if scheduler.running:
scheduler.shutdown(wait=False)
Expand All @@ -122,7 +123,9 @@ async def restart(_, message):
if st := Intervals["status"]:
for intvl in list(st.values()):
intvl.cancel()
await sleep(1)
await sync_to_async(clean_all)
await sleep(1)
proc1 = await create_subprocess_exec(
"pkill", "-9", "-f", "gunicorn|aria2c|qbittorrent-nox|ffmpeg|rclone|java"
)
Expand Down
191 changes: 103 additions & 88 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ async def proceedExtract(self, dl_path, gid):
LOGGER.error(
f"{stderr}. Unable to extract archive splits!. Path: {f_path}"
)
elif code == -9:
self.cancelled = True
return ""
if (
not self.seed
and self.suproc is not None
Expand Down Expand Up @@ -502,6 +505,9 @@ async def proceedExtract(self, dl_path, gid):
)
self.newDir = ""
return dl_path
else:
self.cancelled = True
return ""
except NotSupportedExtractionArchive:
LOGGER.info(
f"Not any valid archive, uploading file as it is. Path: {dl_path}"
Expand Down Expand Up @@ -580,6 +586,9 @@ async def proceedCompress(self, dl_path, gid, o_files, ft_delete):
stderr = stderr.decode().strip()
LOGGER.error(f"{stderr}. Unable to zip this path: {dl_path}")
return dl_path
else:
self.cancelled = True
return ""

async def proceedSplit(self, up_dir, m_size, o_files, gid):
checked = False
Expand Down Expand Up @@ -626,55 +635,57 @@ async def generateSampleVideo(self, dl_path, gid, unwanted_files, ft_delete):
async with task_dict_lock:
task_dict[self.mid] = SampleVideoStatus(self, gid)

async with cpu_eater_lock:
checked = False
if await aiopath.isfile(dl_path):
if (await get_document_type(dl_path))[0]:
if not checked:
checked = True
LOGGER.info(f"Creating Sample video: {self.name}")
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 and not self.newDir:
self.newDir = f"{self.dir}10000"
newfolder = newfolder.replace(self.dir, self.newDir)
await makedirs(newfolder, exist_ok=True)
await gather(
copy2(dl_path, f"{newfolder}/{name}"),
move(res, f"{newfolder}/SAMPLE.{name}"),
)
else:
await makedirs(newfolder, exist_ok=True)
await gather(
move(dl_path, f"{newfolder}/{name}"),
move(res, f"{newfolder}/SAMPLE.{name}"),
)
return newfolder
return dl_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_)
if f_path in unwanted_files:
continue
if (await get_document_type(f_path))[0]:
if not checked:
checked = True
LOGGER.info(f"Creating Sample videos: {self.name}")
if self.cancelled:
return False
res = await createSampleVideo(
self, f_path, sample_duration, part_duration
)
if res:
ft_delete.append(res)
return dl_path
checked = False
if await aiopath.isfile(dl_path):
if (await get_document_type(dl_path))[0]:
checked = True
await cpu_eater_lock.acquire()
LOGGER.info(f"Creating Sample video: {self.name}")
res = await createSampleVideo(
self, dl_path, sample_duration, part_duration, True
)
cpu_eater_lock.release()
if res:
newfolder = ospath.splitext(dl_path)[0]
name = dl_path.rsplit("/", 1)[1]
if self.seed and not self.newDir:
self.newDir = f"{self.dir}10000"
newfolder = newfolder.replace(self.dir, self.newDir)
await makedirs(newfolder, exist_ok=True)
await gather(
copy2(dl_path, f"{newfolder}/{name}"),
move(res, f"{newfolder}/SAMPLE.{name}"),
)
else:
await makedirs(newfolder, exist_ok=True)
await gather(
move(dl_path, f"{newfolder}/{name}"),
move(res, f"{newfolder}/SAMPLE.{name}"),
)
return newfolder
return dl_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_)
if f_path in unwanted_files:
continue
if (await get_document_type(f_path))[0]:
if not checked:
checked = True
await cpu_eater_lock.acquire()
LOGGER.info(f"Creating Sample videos: {self.name}")
if self.cancelled:
cpu_eater_lock.release()
return False
res = await createSampleVideo(
self, f_path, sample_duration, part_duration
)
if res:
ft_delete.append(res)
if checked:
cpu_eater_lock.release()
return dl_path

async def convertMedia(self, dl_path, gid, o_files, m_size, ft_delete):
fvext = []
Expand Down Expand Up @@ -732,6 +743,7 @@ async def proceedConvert(m_path):
):
if not checked:
checked = True
await cpu_eater_lock.acquire()
LOGGER.info(f"Converting: {self.name}")
res = await convert_video(self, m_path, vext)
return False if self.cancelled else res
Expand All @@ -749,6 +761,7 @@ async def proceedConvert(m_path):
):
if not checked:
checked = True
await cpu_eater_lock.acquire()
LOGGER.info(f"Converting: {self.name}")
res = await convert_audio(self, m_path, aext)
return False if self.cancelled else res
Expand All @@ -758,42 +771,44 @@ async def proceedConvert(m_path):
async with task_dict_lock:
task_dict[self.mid] = MediaConvertStatus(self, gid)

async with cpu_eater_lock:
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
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
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)
ft_delete.append(res)
else:
try:
await remove(f_path)
except:
return False

return dl_path
if await aiopath.isfile(dl_path):
output_file = await proceedConvert(dl_path)
if checked:
cpu_eater_lock.release()
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
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:
cpu_eater_lock.release()
return False
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)
ft_delete.append(res)
else:
try:
await remove(f_path)
except:
cpu_eater_lock.release()
return False
if checked:
cpu_eater_lock.release()
return dl_path
4 changes: 2 additions & 2 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def convert_video(listener, video_file, ext, retry=False):
else:
stderr = stderr.decode().strip()
LOGGER.error(
f"{stderr}. Something went wrong while converting video, mostly file is corrupted. Path: {video_file}"
f"{f'{stderr}. ' if retry else ''}Something went wrong while converting video, mostly file need specific codec. {'Retrying with specific codec' if not retry else ''}. Path: {video_file}"
)
if not retry:
if await aiopath.exists(output):
Expand Down Expand Up @@ -86,7 +86,7 @@ async def convert_audio(listener, audio_file, ext):
else:
stderr = stderr.decode().strip()
LOGGER.error(
f"{stderr}. Something went wrong while converting audio, mostly file is corrupted. Path: {audio_file}"
f"{stderr}. Something went wrong while converting audio, mostly file need specific codec. Path: {audio_file}"
)
if await aiopath.exists(output):
await remove(output)
Expand Down
6 changes: 5 additions & 1 deletion bot/helper/listeners/aria2_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from asyncio import sleep
from time import time

from bot import aria2, task_dict_lock, task_dict, LOGGER, config_dict
from bot import aria2, task_dict_lock, task_dict, LOGGER, config_dict, Intervals
from bot.helper.ext_utils.bot_utils import (
new_thread,
bt_selection_buttons,
Expand Down Expand Up @@ -87,6 +87,8 @@ async def _onDownloadComplete(api, gid):
LOGGER.info(f"onDownloadComplete: {download.name} - Gid: {gid}")
if task := await getTaskByGid(gid):
await task.listener.onDownloadComplete()
if Intervals["stopAll"]:
return
await sync_to_async(api.remove, [download], force=True, files=True)


Expand Down Expand Up @@ -125,6 +127,8 @@ async def _onBtDownloadComplete(api, gid):
except Exception as e:
LOGGER.error(f"{e} GID: {gid}")
await task.listener.onDownloadComplete()
if Intervals["stopAll"]:
return
download = download.live
if task.listener.seed:
if download.is_complete:
Expand Down
2 changes: 2 additions & 0 deletions bot/helper/listeners/jdownloader_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async def _onDownloadComplete(gid):
package_ids=jd_downloads[gid]["ids"],
)
await task.listener.onDownloadComplete()
if Intervals["stopAll"]:
return
await retry_function(
jdownloader.device.downloads.remove_links,
package_ids=jd_downloads[gid]["ids"],
Expand Down
2 changes: 2 additions & 0 deletions bot/helper/listeners/qbit_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ async def _onDownloadComplete(tor):
except:
pass
await task.listener.onDownloadComplete()
if Intervals["stopAll"]:
return
client = await sync_to_async(get_qb_client)
if task.listener.seed:
async with task_dict_lock:
Expand Down
3 changes: 2 additions & 1 deletion bot/helper/mirror_utils/status_utils/aria2_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

def get_download(gid, old_info=None):
try:
return aria2.get_download(gid)
res = aria2.get_download(gid)
return res if res else old_info
except Exception as e:
LOGGER.error(f"{e}: Aria2c, Error while getting torrent info")
return old_info
Expand Down
Loading

0 comments on commit de3d837

Please sign in to comment.