Some fixes and improvemnts for jdownloader

Signed-off-by: anasty17 <e.anastayyar@gmail.com>
This commit is contained in:
anasty17 2024-10-08 17:50:49 +03:00
parent 094c79a459
commit 854a4851be
No known key found for this signature in database
GPG Key ID: EAAC8A2DF2861DE4
4 changed files with 57 additions and 47 deletions

View File

@ -2,11 +2,11 @@ from aiofiles.os import path, makedirs, listdir, rename
from aioshutil import rmtree
from json import dump
from random import randint
from asyncio import sleep
from asyncio import sleep, wait_for
from re import match
from bot import config_dict, LOGGER, jd_lock, bot_name
from .bot_utils import cmd_exec, new_task
from .bot_utils import cmd_exec, new_task, retry_function
from myjd import MyJdApi
from myjd.exception import (
MYJDException,
@ -129,5 +129,17 @@ class JDownloader(MyJdApi):
LOGGER.info("JDownloader Device have been Connected!")
return True
async def check_jdownloader_state(self):
try:
await wait_for(retry_function(self.device.jd.version), timeout=10)
except:
is_connected = await self.jdconnect()
if not is_connected:
raise MYJDException(self.error)
await self.boot()
isDeviceConnected = await self.connectToDevice()
if not isDeviceConnected:
raise MYJDException(self.error)
jdownloader = JDownloader()

View File

@ -53,18 +53,9 @@ async def _jd_listener():
intervals["jd"] = ""
break
try:
await wait_for(
retry_function(jdownloader.device.jd.version), timeout=10
)
await jdownloader.check_jdownloader_state()
except:
is_connected = await jdownloader.jdconnect()
if not is_connected:
LOGGER.error(jdownloader.error)
continue
jdownloader.boot()
isDeviceConnected = await jdownloader.connectToDevice()
if not isDeviceConnected:
continue
continue
try:
packages = await jdownloader.device.downloads.query_packages(
[{"finished": True, "saveTo": True}]

View File

@ -84,6 +84,20 @@ class JDownloaderHelper:
return not self.listener.is_cancelled
async def get_online_packages(path, state="grabbing"):
if state == "grabbing":
queued_downloads = await retry_function(
jdownloader.device.linkgrabber.query_packages, [{"saveTo": True}]
)
return [qd["uuid"] for qd in queued_downloads if qd["saveTo"].startswith(path)]
else:
download_packages = await retry_function(
jdownloader.device.downloads.query_packages,
[{"saveTo": True}],
)
return [dl["uuid"] for dl in download_packages if dl["saveTo"].startswith(path)]
async def add_jd_download(listener, path):
try:
async with jd_lock:
@ -91,18 +105,8 @@ async def add_jd_download(listener, path):
jd_downloads[gid] = {"status": "collect", "path": path}
if jdownloader.device is None:
raise MYJDException(jdownloader.error)
try:
await wait_for(
retry_function(jdownloader.device.jd.version), timeout=10
)
except:
is_connected = await jdownloader.jdconnect()
if not is_connected:
raise MYJDException(jdownloader.error)
jdownloader.boot()
isDeviceConnected = await jdownloader.connectToDevice()
if not isDeviceConnected:
raise MYJDException(jdownloader.error)
await jdownloader.check_jdownloader_state()
if not jd_downloads:
await retry_function(jdownloader.device.linkgrabber.clear_list)
@ -147,13 +151,11 @@ async def add_jd_download(listener, path):
],
)
await sleep(0.5)
await sleep(1)
while await retry_function(jdownloader.device.linkgrabber.is_collecting):
pass
start_time = time()
online_packages = []
listener.size = 0
corrupted_packages = []
remove_unknown = False
name = ""
@ -180,8 +182,7 @@ async def add_jd_download(listener, path):
raise MYJDException(error)
for pack in queued_downloads:
online = pack.get("onlineCount", 1)
if online == 0:
if pack.get("onlineCount", 1) == 0:
error = f"{pack.get('name', '')}"
LOGGER.error(error)
corrupted_packages.append(pack["uuid"])
@ -277,14 +278,9 @@ async def add_jd_download(listener, path):
del jd_downloads[gid]
return
else:
queued_downloads = await retry_function(
jdownloader.device.linkgrabber.query_packages, [{"saveTo": True}]
)
online_packages = [
qd["uuid"]
for qd in queued_downloads
if qd["saveTo"].startswith(path)
]
online_packages = await get_online_packages(path)
if not online_packages:
raise MYJDException("This Download have been removed manually!")
async with jd_lock:
jd_downloads[gid]["ids"] = online_packages
@ -302,6 +298,13 @@ async def add_jd_download(listener, path):
async with queue_dict_lock:
non_queued_dl.add(listener.mid)
await jdownloader.check_jdownloader_state()
online_packages = await get_online_packages(path)
if not online_packages:
raise MYJDException("This Download have been removed manually!")
async with jd_lock:
jd_downloads[gid]["ids"] = online_packages
await retry_function(
jdownloader.device.linkgrabber.move_to_downloadlist,
package_ids=online_packages,
@ -309,13 +312,17 @@ async def add_jd_download(listener, path):
await sleep(1)
download_packages = await retry_function(
jdownloader.device.downloads.query_packages,
[{"saveTo": True}],
)
online_packages = [
dl["uuid"] for dl in download_packages if dl["saveTo"].startswith(path)
]
online_packages = await get_online_packages(path, "down")
if not online_packages:
online_packages = await get_online_packages(path)
if not online_packages:
raise MYJDException("This Download have been removed manually!")
await retry_function(
jdownloader.device.linkgrabber.move_to_downloadlist,
package_ids=online_packages,
)
await sleep(1)
online_packages = await get_online_packages(path, "down")
if not online_packages:
raise MYJDException("This Download have been removed manually!")

View File

@ -108,8 +108,8 @@ class JDownloaderStatus:
LOGGER.info(f"Cancelling Download: {self.name()}")
await retry_function(
jdownloader.device.downloads.remove_links,
package_ids=jd_downloads[int(self._gid)]["ids"],
package_ids=jd_downloads[self._gid]["ids"],
)
async with jd_lock:
del jd_downloads[int(self._gid)]
del jd_downloads[self._gid]
await self.listener.on_download_error("Download cancelled by user!")