Skip to content

Commit

Permalink
Fix some bugs in jdowloader while using multi
Browse files Browse the repository at this point in the history
- Now you do jd sync while downloading (not while collecting)(not recommended)
- Remove youtube links restrcition in jdownloader (i didn't try youtube download with jd yet)

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Oct 7, 2024
1 parent dea66d7 commit cb16cb3
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 291 deletions.
1 change: 1 addition & 0 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ async def retry_function(func, *args, **kwargs):
try:
return await func(*args, **kwargs)
except:
await sleep(0.2)
return await retry_function(func, *args, **kwargs)


Expand Down
71 changes: 30 additions & 41 deletions bot/helper/listeners/jdownloader_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
from ..ext_utils.status_utils import get_task_by_gid


@new_task
async def update_download(gid, value):
try:
async with jd_lock:
del value["ids"][0]
new_gid = value["ids"][0]
jd_downloads[new_gid] = value
if task := await get_task_by_gid(f"{gid}"):
task._gid = new_gid
async with jd_lock:
del jd_downloads[gid]
except:
pass


@new_task
async def remove_download(gid):
if intervals["stopAll"]:
Expand All @@ -29,15 +14,15 @@ async def remove_download(gid):
jdownloader.device.downloads.remove_links,
package_ids=[gid],
)
if task := await get_task_by_gid(f"{gid}"):
if task := await get_task_by_gid(gid):
await task.listener.on_download_error("Download removed manually!")
async with jd_lock:
del jd_downloads[gid]


@new_task
async def _on_download_complete(gid):
if task := await get_task_by_gid(f"{gid}"):
if task := await get_task_by_gid(gid):
if task.listener.select:
async with jd_lock:
await retry_function(
Expand Down Expand Up @@ -82,35 +67,39 @@ async def _jd_listener():
continue
try:
packages = await jdownloader.device.downloads.query_packages(
[{"finished": True}]
[{"finished": True, "saveTo": True}]
)
except:
continue
finished = [
pack["uuid"] for pack in packages if pack.get("finished", False)
]

all_packages = [pack["uuid"] for pack in packages]
for k, v in list(jd_downloads.items()):
if v["status"] == "down":
if k in all_packages:
for index, pid in enumerate(v["ids"]):
if pid not in all_packages:
del jd_downloads[k]["ids"][index]
for d_gid, d_dict in list(jd_downloads.items()):
if d_dict["status"] == "down":
for index, pid in enumerate(d_dict["ids"]):
if pid not in all_packages:
del jd_downloads[d_gid]["ids"][index]
if len(jd_downloads[d_gid]["ids"]) == 0:
path = jd_downloads[d_gid]["path"]
jd_downloads[d_gid]["ids"] = [
dl["uuid"]
for dl in all_packages
if dl["saveTo"].startswith(path)
]
if len(jd_downloads[d_gid]["ids"]) == 0:
await remove_download(d_gid)

else:
cdi = jd_downloads[k]["ids"]
if len(cdi) > 1:
await update_download(k, v)
else:
await remove_download(k)
for gid in finished:
if gid in jd_downloads and jd_downloads[gid]["status"] == "down":
is_finished = all(
did in finished for did in jd_downloads[gid]["ids"]
)
if is_finished:
jd_downloads[gid]["status"] = "done"
await _on_download_complete(gid)
completed_packages = [
pack["uuid"] for pack in packages if pack.get("finished", False)
]
if completed_packages:
for d_gid, d_dict in list(jd_downloads.items()):
if d_dict["status"] == "down":
is_finished = all(
did in completed_packages for did in d_dict["ids"]
)
if is_finished:
jd_downloads[d_gid]["status"] = "done"
await _on_download_complete(d_gid)


async def on_download_start():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ def direct_link_generator(link):
domain = urlparse(link).hostname
if not domain:
raise DirectDownloadLinkException("ERROR: Invalid URL")
if "youtube.com" in domain or "youtu.be" in domain:
raise DirectDownloadLinkException("ERROR: Use ytdl cmds for Youtube links")
elif "yadi.sk" in link or "disk.yandex." in link:
return yandex_disk(link)
elif "mediafire.com" in domain:
Expand Down
Loading

0 comments on commit cb16cb3

Please sign in to comment.