Skip to content

Commit

Permalink
remove megasdk, add jd, fix shorteners_list and others minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Jan 29, 2025
1 parent 55346eb commit e932946
Show file tree
Hide file tree
Showing 26 changed files with 1,971 additions and 360 deletions.
13 changes: 3 additions & 10 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def format(self, record: LogRecord) -> str:

LOGGER = getLogger(__name__)

intervals = {"status": {}, "qb": "", "stopAll": False}
intervals = {"status": {}, "qb": "", "jd": "", "stopAll": False}
qb_torrents = {}
user_data = {}
aria2_options = {}
Expand All @@ -83,6 +83,7 @@ def format(self, record: LogRecord) -> str:
queued_up = {}
status_dict = {}
task_dict = {}
jd_downloads = {}
rss_dict = {}
non_queued_dl = set()
non_queued_up = set()
Expand All @@ -92,6 +93,7 @@ def format(self, record: LogRecord) -> str:
qb_listener_lock = Lock()
cpu_eater_lock = Lock()
same_directory_lock = Lock()
jd_listener_lock = Lock()
extension_filter = ["aria2", "!qB"]
drives_names = []
drives_ids = []
Expand Down Expand Up @@ -130,13 +132,4 @@ def format(self, record: LogRecord) -> str:
subprocess.run(["xria", "--conf-path=/usr/src/app/a2c.conf"], check=False)


if os.path.exists("shorteners.txt"):
with open("shorteners.txt", "r+") as f:
lines = f.readlines()
for line in lines:
temp = line.strip().split()
if len(temp) == 2:
shorteners_list.append({"domain": temp[0], "api_key": temp[1]})


scheduler = AsyncIOScheduler(timezone=str(get_localzone()), event_loop=bot_loop)
5 changes: 4 additions & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

bot_loop.run_until_complete(load_settings())


from .core.aeon_client import TgClient
from .core.handlers import add_handlers
from .helper.ext_utils.bot_utils import create_help_buttons, new_task, sync_to_async
from .helper.ext_utils.files_utils import clean_all, exit_clean_up
from .helper.ext_utils.jdownloader_booter import jdownloader
from .helper.ext_utils.telegraph_helper import telegraph
from .helper.listeners.aria2_listener import start_aria2_listener
from .helper.mirror_leech_utils.rclone_utils.serve import rclone_serve_booter
Expand All @@ -50,6 +50,8 @@
COMMANDS = {
"MirrorCommand": "- Start mirroring",
"LeechCommand": "- Start leeching",
"JdMirrorCommand": "- Mirror using Jdownloader",
"JdLeechCommand": "- Leech using jdownloader",
"YtdlCommand": "- Mirror yt-dlp supported link",
"YtdlLeechCommand": "- Leech through yt-dlp supported link",
"CloneCommand": "- Copy file/folder to Drive",
Expand Down Expand Up @@ -120,6 +122,7 @@ async def main():
sync_to_async(update_qb_options),
sync_to_async(update_aria2_options),
set_commands(),
jdownloader.boot(),
)
await gather(
save_settings(),
Expand Down
4 changes: 2 additions & 2 deletions bot/core/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Config:
GDRIVE_ID = ""
INCOMPLETE_TASK_NOTIFIER = False
INDEX_URL = ""
JD_EMAIL = ""
JD_PASS = ""
IS_TEAM_DRIVE = False
LEECH_DUMP_CHAT = ""
LEECH_FILENAME_PREFIX = ""
LEECH_SPLIT_SIZE = 2097152000
MEDIA_GROUP = False
MIXED_LEECH = False
MEGA_EMAIL = ""
MEGA_PASSWORD = ""
NAME_SUBSTITUTE = ""
OWNER_ID = 0
QUEUE_ALL = 0
Expand Down
10 changes: 10 additions & 0 deletions bot/core/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,21 @@ def add_handlers():
BotCommands.MirrorCommand,
CustomFilters.authorized,
),
"jd_mirror": (
jd_mirror,
BotCommands.JdMirrorCommand,
CustomFilters.authorized,
),
"leech": (
leech,
BotCommands.LeechCommand,
CustomFilters.authorized,
),
"jd_leech": (
jd_leech,
BotCommands.JdLeechCommand,
CustomFilters.authorized,
),
"get_rss_menu": (
get_rss_menu,
BotCommands.RssCommand,
Expand Down
18 changes: 18 additions & 0 deletions bot/core/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
aria2,
aria2_options,
drives_ids,
shorteners_list,
drives_names,
extension_filter,
index_urls,
Expand Down Expand Up @@ -222,6 +223,23 @@ async def load_configurations():
f"gunicorn web.wserver:app --bind 0.0.0.0:{PORT} --worker-class gevent",
)

if await aiopath.exists("cfg.zip"):
if await aiopath.exists("/JDownloader/cfg"):
await rmtree("/JDownloader/cfg", ignore_errors=True)
await (
await create_subprocess_exec("7z", "x", "cfg.zip", "-o/JDownloader")
).wait()
await remove("cfg.zip")

if await aiopath.exists("shorteners.txt"):
async with aiopen("shorteners.txt", "r") as f:
lines = await f.readlines()
for line in lines:
temp = line.strip().split()
if len(temp) == 2:
shorteners_list.append({"domain": temp[0], "api_key": temp[1]})


if await aiopath.exists("accounts.zip"):
if await aiopath.exists("accounts"):
await rmtree("accounts")
Expand Down
37 changes: 26 additions & 11 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self):
self.subsize = 0
self.proceed_count = 0
self.is_leech = False
self.is_jd = False
self.is_qbit = False
self.is_clone = False
self.is_ytdlp = False
Expand Down Expand Up @@ -182,25 +183,26 @@ async def before_start(self):
else ["aria2", "!qB"]
)
if self.link not in ["rcl", "gdl"]:
if is_rclone_path(self.link):
if not self.link.startswith("mrcc:") and self.user_dict.get(
"user_tokens",
False,
):
self.link = f"mrcc:{self.link}"
await self.is_token_exists(self.link, "dl")
if not self.is_jd:
if is_rclone_path(self.link):
if not self.link.startswith("mrcc:") and self.user_dict.get(
"user_tokens",
False,
):
self.link = f"mrcc:{self.link}"
await self.is_token_exists(self.link, "dl")
elif is_gdrive_link(self.link):
if not self.link.startswith(
("mtp:", "tp:", "sa:"),
) and self.user_dict.get("user_tokens", False):
self.link = f"mtp:{self.link}"
await self.is_token_exists(self.link, "dl")
elif self.link == "rcl":
if not self.is_ytdlp:
if not self.is_ytdlp and not self.is_jd:
self.link = await RcloneList(self).get_rclone_path("rcd")
if not is_rclone_path(self.link):
raise ValueError(self.link)
elif self.link == "gdl" and not self.is_ytdlp:
elif self.link == "gdl" and not self.is_ytdlp and not self.is_jd:
self.link = await GoogleDriveList(self).get_target_id("gdd")
if not is_gdrive_id(self.link):
raise ValueError(self.link)
Expand Down Expand Up @@ -441,7 +443,7 @@ async def before_start(self):
)

if self.thumb != "none" and is_telegram_link(self.thumb):
msg = (await get_tg_link_message(self.thumb))[0]
msg, _ = (await get_tg_link_message(self.thumb))[0]
self.thumb = (
await create_thumb(msg) if msg.photo or msg.document else ""
)
Expand Down Expand Up @@ -521,6 +523,7 @@ async def run_multi(self, input_list, obj):
nextmsg,
self.is_qbit,
self.is_leech,
self.is_jd,
self.same_dir,
self.bulk,
self.multi_tag,
Expand Down Expand Up @@ -559,6 +562,7 @@ async def init_bulk(self, input_list, bulk_start, bulk_end, obj):
nextmsg,
self.is_qbit,
self.is_leech,
self.is_jd,
self.same_dir,
self.bulk,
self.multi_tag,
Expand Down Expand Up @@ -1071,7 +1075,6 @@ async def proceed_split(self, dl_path, gid):
return None
return None

# change according sync
async def proceed_metadata(self, dl_path, gid):
key = self.metadata
ffmpeg = FFMpeg(self)
Expand All @@ -1096,6 +1099,8 @@ async def proceed_metadata(self, dl_path, gid):
res = await ffmpeg.metadata_watermark_cmds(cmd, dl_path)
if res:
os.replace(temp_file, dl_path)
else:
os.remove(temp_file)
else:
for dirpath, _, files in await sync_to_async(
walk,
Expand Down Expand Up @@ -1132,6 +1137,8 @@ async def proceed_metadata(self, dl_path, gid):
)
if res:
os.replace(temp_file, file_path)
else:
os.remove(temp_file)
if checked:
cpu_eater_lock.release()
return dl_path
Expand Down Expand Up @@ -1160,6 +1167,8 @@ async def proceed_watermark(self, dl_path, gid):
res = await ffmpeg.metadata_watermark_cmds(cmd, dl_path)
if res:
os.replace(temp_file, dl_path)
else:
os.remove(temp_file)
else:
for dirpath, _, files in await sync_to_async(
walk,
Expand Down Expand Up @@ -1195,6 +1204,8 @@ async def proceed_watermark(self, dl_path, gid):
)
if res:
os.replace(temp_file, file_path)
else:
os.remove(temp_file)
if checked:
cpu_eater_lock.release()
return dl_path
Expand Down Expand Up @@ -1223,6 +1234,8 @@ async def proceed_embed_thumb(self, dl_path, gid):
res = await ffmpeg.metadata_watermark_cmds(cmd, dl_path)
if res:
os.replace(temp_file, dl_path)
else:
os.remove(temp_file)
else:
for dirpath, _, files in await sync_to_async(
walk,
Expand Down Expand Up @@ -1258,6 +1271,8 @@ async def proceed_embed_thumb(self, dl_path, gid):
)
if res:
os.replace(temp_file, file_path)
else:
os.remove(temp_file)
if checked:
cpu_eater_lock.release()
return dl_path
2 changes: 1 addition & 1 deletion bot/helper/ext_utils/files_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def exit_clean_up(_, __):
LOGGER.info("Please wait! Bot clean up and stop the running downloads...")
clean_all()
srun(
["pkill", "-9", "-f", "gunicorn|xria|xnox|xtra|xone|7z|split"],
["pkill", "-9", "-f", "gunicorn|xria|xnox|xtra|xone|java|7z|split"],
check=False,
)
exit(0)
Expand Down
2 changes: 2 additions & 0 deletions bot/helper/ext_utils/help_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,10 @@
help_string = f"""
NOTE: Try each command without any argument to see more detalis.
/{BotCommands.MirrorCommand[0]} or /{BotCommands.MirrorCommand[1]}: Start mirroring to cloud.
/{BotCommands.JdMirrorCommand[0]} or /{BotCommands.JdMirrorCommand[1]}: Start Mirroring to cloud using JDownloader.
/{BotCommands.YtdlCommand[0]} or /{BotCommands.YtdlCommand[1]}: Mirror yt-dlp supported link.
/{BotCommands.LeechCommand[0]} or /{BotCommands.LeechCommand[1]}: Start leeching to Telegram.
/{BotCommands.JdLeechCommand[0]} or /{BotCommands.JdLeechCommand[1]}: Start leeching using JDownloader.
/{BotCommands.YtdlLeechCommand[0]} or /{BotCommands.YtdlLeechCommand[1]}: Leech yt-dlp supported link.
/{BotCommands.CloneCommand} [drive_url]: Copy file/folder to Google Drive.
/{BotCommands.CountCommand} [drive_url]: Count file/folder of Google Drive.
Expand Down
97 changes: 97 additions & 0 deletions bot/helper/ext_utils/jdownloader_booter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from json import dump
from random import randint
from re import match

from aiofiles.os import listdir, makedirs, path, rename
from aioshutil import rmtree

from bot import LOGGER
from bot.core.aeon_client import TgClient
from bot.core.config_manager import Config
from myjd import MyJdApi

from .bot_utils import cmd_exec, new_task


class JDownloader(MyJdApi):
def __init__(self):
super().__init__()
self._username = ""
self._password = ""
self._device_name = ""
self.is_connected = False
self.error = "JDownloader Credentials not provided!"

@new_task
async def boot(self):
await cmd_exec(["pkill", "-9", "-f", "java"])
if not Config.JD_EMAIL or not Config.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)}@{TgClient.NAME}"
if await path.exists("/JDownloader/logs"):
LOGGER.info(
"Starting JDownloader... This might take up to 10 sec and might restart once if update available!",
)
else:
LOGGER.info(
"Starting JDownloader... This might take up to 8 sec and might restart once after build!",
)
jdata = {
"autoconnectenabledv2": True,
"password": Config.JD_PASS,
"devicename": f"{self._device_name}",
"email": Config.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)
with open(
"/JDownloader/cfg/org.jdownloader.api.myjdownloader.MyJDownloaderSettings.json",
"w",
) as sf:
sf.truncate(0)
dump(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"):
if match(pattern, filename):
await rename(
f"/JDownloader/{filename}",
"/JDownloader/JDownloader.jar",
)
break
try:
await rmtree("/JDownloader/update")
await rmtree("/JDownloader/tmp")
except Exception:
pass
cmd = "java -Xms256m -Xmx500m -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 -Djava.awt.headless=true -jar /JDownloader/JDownloader.jar"
self.is_connected = True
_, __, code = await cmd_exec(cmd, shell=True)
self.is_connected = False
if code != -9:
await self.boot()


jdownloader = JDownloader()
Loading

2 comments on commit e932946

@shabbirmahmud
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RIP Mega

@5hojib
Copy link
Collaborator Author

@5hojib 5hojib commented on e932946 Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can download mega using jdownloader

Please sign in to comment.