Skip to content

Commit

Permalink
Minor fixes and improvements.
Browse files Browse the repository at this point in the history
N/A
  • Loading branch information
Dawn-India committed Oct 30, 2024
1 parent 5c69284 commit a42d1b3
Show file tree
Hide file tree
Showing 26 changed files with 270 additions and 1,195 deletions.
2 changes: 1 addition & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ async def main():
if config_dict["DATABASE_URL"]:
await database.db_load()
await gather(
jdownloader.initiate(),
jdownloader.boot(),
sync_to_async(clean_all),
bot_settings.initiate_search_tools(),
restart_notification(),
Expand Down
6 changes: 6 additions & 0 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,9 @@ async def substitute(self, dl_path):
if sen
else 0
)
if len(name.encode()) > 255:
LOGGER.error(f"Substitute: {name} is too long")
return dl_path
new_path = ospath.join(
up_dir,
name
Expand Down Expand Up @@ -1806,6 +1809,9 @@ async def substitute(self, dl_path):
if sen
else 0
)
if len(file_.encode()) > 255:
LOGGER.error(f"Substitute: {file_} is too long")
continue
await move(
f_path,
ospath.join(
Expand Down
15 changes: 0 additions & 15 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,6 @@ def update_user_ldata(id_, key, value):
user_data[id_][key] = value


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
)


async def cmd_exec(cmd, shell=False):
if shell:
proc = await create_subprocess_shell(
Expand Down
135 changes: 33 additions & 102 deletions bot/helper/ext_utils/jdownloader_booter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,17 @@
from aioshutil import rmtree
from json import dump
from random import randint
from asyncio import sleep, wait_for
from re import match

from bot import (
config_dict,
LOGGER,
jd_lock,
bot_name
bot_name,
config_dict
)
from .bot_utils import (
cmd_exec,
new_task,
retry_function
new_task
)
from myjd import MyJdApi
from myjd.exception import (
MYJDException,
MYJDAuthFailedException,
MYJDEmailForbiddenException,
MYJDEmailInvalidException,
MYJDErrorEmailNotConfirmedException,
)


class JDownloader(MyJdApi):
Expand All @@ -37,18 +26,8 @@ def __init__(self):
self._username = ""
self._password = ""
self._device_name = ""
self.is_connected = False
self.error = "JDownloader Credentials not provided!"
self.device = None
self.set_app_key("zee")

@new_task
async def initiate(self):
self.device = None
async with jd_lock:
is_connected = await self.jdconnect()
if is_connected:
await self.boot()
await self.connectToDevice()

@new_task
async def boot(self):
Expand All @@ -58,7 +37,13 @@ async def boot(self):
"-f",
"java"
])
self.device = None
if (
not config_dict["JD_EMAIL"] or
not config_dict["JD_PASS"]
):
self.is_connected = False
self.error = "JDownloader Credentials not provided!"
return
self.error = "Connecting... Try agin after couple of seconds"
self._device_name = f"{randint(0, 1000)}@{bot_name}"
jdata = {
Expand All @@ -67,6 +52,20 @@ async def boot(self):
"devicename": f"{self._device_name}",
"email": config_dict["JD_EMAIL"],
}
remote_data = {
"localapiserverheaderaccesscontrollalloworigin": "",
"deprecatedapiport": 3128,
"localapiserverheaderxcontenttypeoptions": "nosniff",
"localapiserverheaderxframeoptions": "DENY",
"externinterfaceenabled": True,
"deprecatedapilocalhostonly": True,
"localapiserverheaderreferrerpolicy": "no-referrer",
"deprecatedapienabled": True,
"localapiserverheadercontentsecuritypolicy": "default-src 'self'",
"jdanywhereapienabled": True,
"externinterfacelocalhostonly": False,
"localapiserverheaderxxssprotection": "1; mode=block",
}
await makedirs(
"/JDownloader/cfg",
exist_ok=True
Expand All @@ -80,6 +79,12 @@ async def boot(self):
jdata,
sf
)
with open(
"/JDownloader/cfg/org.jdownloader.api.RemoteAPIConfig.json",
"w",
) as rf:
rf.truncate(0)
dump(remote_data, rf)
if not await path.exists("/JDownloader/JDownloader.jar"):
pattern = r"JDownloader\.jar\.backup.\d$"
for filename in await listdir("/JDownloader"):
Expand All @@ -95,6 +100,7 @@ async def boot(self):
await rmtree("/JDownloader/update")
await rmtree("/JDownloader/tmp")
cmd = "java -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 -Djava.awt.headless=true -jar /JDownloader/JDownloader.jar"
self.is_connected = True
(
_,
__,
Expand All @@ -103,84 +109,9 @@ async def boot(self):
cmd,
shell=True
)
self.is_connected = False
if code != -9:
await self.boot()

async def jdconnect(self):
if (
not config_dict["JD_EMAIL"]
or not config_dict["JD_PASS"]
):
return False
try:
await self.connect(
config_dict["JD_EMAIL"],
config_dict["JD_PASS"]
)
return True
except (
MYJDAuthFailedException,
MYJDEmailForbiddenException,
MYJDEmailInvalidException,
MYJDErrorEmailNotConfirmedException,
) as err:
self.error = f"{err}".strip()
LOGGER.info(f"Failed to connect with jdownloader! ERROR: {self.error}")
self.device = None
return False
except MYJDException as e:
self.error = f"{e}".strip()
LOGGER.info(
f"Failed to connect with jdownloader! Retrying... ERROR: {self.error}"
)
return await self.jdconnect()

async def connectToDevice(self):
self.error = "Connecting to device..."
await sleep(0.5)
while True:
self.device = None
if (
not config_dict["JD_EMAIL"]
or not config_dict["JD_PASS"]
):
self.error = "JDownloader Credentials not provided!"
await cmd_exec([
"pkill",
"-9",
"-f",
"java"
])
return False
try:
await self.update_devices()
if not (devices := self.list_devices()):
continue
for device in devices:
if self._device_name == device["name"]:
self.device = self.get_device(f"{self._device_name}")
break
else:
continue
except:
continue
break
await self.device.enable_direct_connection()
self.error = ""
LOGGER.info("JDownloader is ready to use!")
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()
9 changes: 3 additions & 6 deletions bot/helper/ext_utils/task_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from asyncio import (
Event,
sleep
)
from asyncio import Event

from bot import (
config_dict,
Expand Down Expand Up @@ -148,13 +145,13 @@ async def check_running_tasks(listener, state="dl"):
async def start_dl_from_queued(mid: int):
queued_dl[mid].set()
del queued_dl[mid]
await sleep(0.7)
non_queued_dl.add(mid)


async def start_up_from_queued(mid: int):
queued_up[mid].set()
del queued_up[mid]
await sleep(0.7)
non_queued_up.add(mid)


async def start_from_queued():
Expand Down
2 changes: 0 additions & 2 deletions bot/helper/listeners/aria2_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
)
from ..task_utils.status_utils.aria2_status import Aria2Status
from ..telegram_helper.message_utils import (
auto_delete_message,
delete_links,
delete_message,
send_message,
update_status_message,
Expand Down
24 changes: 6 additions & 18 deletions bot/helper/listeners/jdownloader_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
jd_downloads,
intervals
)
from ..ext_utils.bot_utils import (
new_task,
retry_function
)
from ..ext_utils.bot_utils import new_task
from ..ext_utils.jdownloader_booter import jdownloader
from ..ext_utils.status_utils import get_task_by_gid

Expand All @@ -17,9 +14,8 @@
async def remove_download(gid):
if intervals["stopAll"]:
return
await retry_function(
jdownloader.device.downloads.remove_links, # type: ignore
package_ids=jd_downloads[gid]["ids"],
await jdownloader.device.downloads.remove_links(
package_ids=jd_downloads[gid]["ids"]
)
if task := await get_task_by_gid(gid):
await task.listener.on_download_error("Download removed manually!")
Expand All @@ -32,8 +28,7 @@ async def _on_download_complete(gid):
if task := await get_task_by_gid(gid):
if task.listener.select:
async with jd_lock:
await retry_function(
jdownloader.device.downloads.cleanup, # type: ignore
await jdownloader.device.downloads.cleanup(
"DELETE_DISABLED",
"REMOVE_LINKS_AND_DELETE_FILES",
"SELECTED",
Expand All @@ -44,8 +39,7 @@ async def _on_download_complete(gid):
return
async with jd_lock:
if gid in jd_downloads:
await retry_function(
jdownloader.device.downloads.remove_links, # type: ignore
await jdownloader.device.downloads.remove_links(
package_ids=jd_downloads[gid]["ids"],
)
del jd_downloads[gid]
Expand All @@ -60,11 +54,7 @@ async def _jd_listener():
intervals["jd"] = ""
break
try:
await jdownloader.check_jdownloader_state()
except:
continue
try:
packages = await jdownloader.device.downloads.query_packages( # type: ignore
packages = await jdownloader.device.downloads.query_packages(
[{
"finished": True,
"saveTo": True
Expand All @@ -78,8 +68,6 @@ async def _jd_listener():
for pack
in packages
}
if not all_packages:
continue
for (
d_gid,
d_dict
Expand Down
1 change: 0 additions & 1 deletion bot/helper/listeners/nzb_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
limit_checker,
stop_duplicate_check
)
from ..telegram_helper.message_utils import auto_delete_message


async def _remove_job(nzo_id, mid):
Expand Down
10 changes: 3 additions & 7 deletions bot/helper/listeners/qbit_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
stop_duplicate_check
)
from ..task_utils.status_utils.qbit_status import QbittorrentStatus
from ..telegram_helper.message_utils import (
auto_delete_message,
delete_links,
update_status_message
)
from ..telegram_helper.message_utils import update_status_message


async def _remove_torrent(hash_, tag):
Expand Down Expand Up @@ -154,10 +150,10 @@ async def _avg_speed_check(tor):
LOGGER.info(
f"Task is slower than minimum download speed: {task.listener.name} | {get_readable_file_size(dl_speed)}ps"
)
_on_download_error(
await _on_download_error(
min_speed,
tor
) # type: ignore
)


@new_task
Expand Down
2 changes: 0 additions & 2 deletions bot/helper/listeners/task_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ async def on_download_complete(self):
await event.wait() # type: ignore
if self.is_cancelled:
return
async with queue_dict_lock:
non_queued_up.add(self.mid)
LOGGER.info(f"Start from Queued/Upload: {self.name}")

self.size = await get_path_size(up_dir)
Expand Down
Loading

0 comments on commit a42d1b3

Please sign in to comment.