Files
Ultroid-fork/plugins/downloadupload.py
Devesh Pal 4d30dd7dd7 [Update] Ultroid v0.6
Co-Authored-By: Amit Sharma <48654350+buddhhu@users.noreply.github.com>
Co-Authored-By: MMETMA <79155572+MMETMA@users.noreply.github.com>
Co-Authored-By: Aditya <me@xditya.me>
Co-Authored-By: marat2509 <93652988+marat2509@users.noreply.github.com>
Co-Authored-By: smartman_ru <14003491+smartmanru@users.noreply.github.com>
Co-Authored-By: Flasho <75789819+flashokillerify@users.noreply.github.com>
Co-Authored-By: ÁÑÑÍHÌLÅTØR SPÄRK <75305464+annihilatorrrr@users.noreply.github.com>
2022-06-06 23:18:16 +05:30

226 lines
7.0 KiB
Python

# Ultroid - UserBot
# Copyright (C) 2021-2022 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}ul <path/to/file>`
Upload files on telegram.
Use following arguments before or after filename as per requirement:
`--stream` to upload as stream.
`--delete` to delete file after uploading.
`--no-thumb` to upload without thumbnail.
• `{i}dl <filename(optional)>`
Reply to file to download.
• `{i}download <DDL> (| filename)`
Download using DDL. Will autogenerate filename if not given.
"""
import asyncio
import glob
import os
import time
from datetime import datetime as dt
from aiohttp.client_exceptions import InvalidURL
from pyUltroid.functions.helper import time_formatter
from pyUltroid.functions.tools import set_attributes
from telethon.errors.rpcerrorlist import MessageNotModifiedError
from . import (
LOGS,
downloader,
eor,
fast_download,
get_all_files,
get_string,
progress,
time_formatter,
ultroid_cmd,
)
@ultroid_cmd(
pattern="download( (.*)|$)",
)
async def down(event):
matched = event.pattern_match.group(1).strip()
msg = await event.eor(get_string("udl_4"))
if not matched:
return await eor(msg, get_string("udl_5"), time=5)
try:
splited = matched.split(" | ")
link = splited[0]
filename = splited[1]
except IndexError:
filename = None
s_time = time.time()
try:
filename, d = await fast_download(
link,
filename,
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(
d,
t,
msg,
s_time,
f"Downloading from {link}",
)
),
)
except InvalidURL:
return await msg.eor("`Invalid URL provided :(`", time=5)
await msg.eor(f"`{filename}` `downloaded in {time_formatter(d*1000)}.`")
@ultroid_cmd(
pattern="dl( (.*)|$)",
)
async def download(event):
if not event.reply_to_msg_id:
return await event.eor(get_string("cvt_3"), time=8)
xx = await event.eor(get_string("com_1"))
s = dt.now()
k = time.time()
if event.reply_to_msg_id:
ok = await event.get_reply_message()
if not ok.media:
return await xx.eor(get_string("udl_1"), time=5)
if hasattr(ok.media, "document"):
file = ok.media.document
mime_type = file.mime_type
filename = event.pattern_match.group(1).strip() or ok.file.name
if not filename:
if "audio" in mime_type:
filename = "audio_" + dt.now().isoformat("_", "seconds") + ".ogg"
elif "video" in mime_type:
filename = "video_" + dt.now().isoformat("_", "seconds") + ".mp4"
try:
result = await downloader(
"resources/downloads/" + filename,
file,
xx,
k,
"Downloading " + filename + "...",
)
except MessageNotModifiedError as err:
return await xx.edit(str(err))
file_name = result.name
else:
d = "resources/downloads/"
file_name = await event.client.download_media(
ok,
d,
progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
progress(
d,
t,
xx,
k,
get_string("com_5"),
),
),
)
e = dt.now()
t = time_formatter(((e - s).seconds) * 1000)
await xx.eor(get_string("udl_2").format(file_name, t))
@ultroid_cmd(
pattern="ul( (.*)|$)",
)
async def _(event):
msg = await event.eor(get_string("com_1"))
match = event.pattern_match.group(1)
if match:
match = match.strip()
if not event.out and match == ".env":
return await event.reply("`You can't do this...`")
stream, force_doc, delete, thumb = (
False,
True,
False,
"resources/extras/ultroid.jpg",
)
if "--stream" in match:
stream = True
force_doc = False
if "--delete" in match:
delete = True
if "--no-thumb" in match:
thumb = None
arguments = ["--stream", "--delete", "--no-thumb"]
if any(item in match for item in arguments):
match = (
match.replace("--stream", "")
.replace("--delete", "")
.replace("--no-thumb", "")
.strip()
)
if match.endswith("/"):
match += "*"
results = glob.glob(match)
if not results and os.path.exists(match):
results = [match]
if not results:
try:
await event.reply(file=match)
return await event.try_delete()
except Exception as er:
LOGS.exception(er)
return await msg.eor("`File doesn't exist or path is incorrect!`")
for result in results:
if os.path.isdir(result):
c, s = 0, 0
for files in get_all_files(result):
attributes = None
if stream:
try:
attributes = await set_attributes(files)
except KeyError as er:
LOGS.exception(er)
try:
file, _ = await event.client.fast_uploader(
files, show_progress=True, event=msg, to_delete=delete
)
await event.client.send_file(
event.chat_id,
file,
supports_streaming=stream,
force_document=force_doc,
thumb=thumb,
attributes=attributes,
caption=f"`Uploaded` `{files}` `in {time_formatter(_*1000)}`",
reply_to=event.reply_to_msg_id or event,
)
s += 1
except (ValueError, IsADirectoryError):
c += 1
break
attributes = None
if stream:
try:
attributes = await set_attributes(result)
except KeyError as er:
LOGS.exception(er)
file, _ = await event.client.fast_uploader(
result, show_progress=True, event=msg, to_delete=delete
)
await event.client.send_file(
event.chat_id,
file,
supports_streaming=stream,
force_document=force_doc,
thumb=thumb,
attributes=attributes,
caption=f"`Uploaded` `{result}` `in {time_formatter(_*1000)}`",
)
await msg.try_delete()