forked from UsergeTeam/Userge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Udith <[email protected]>
- Loading branch information
Showing
5 changed files
with
91 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,55 @@ | ||
import time | ||
from datetime import datetime | ||
from userge.utils import progress | ||
from userge import userge | ||
from pathlib import Path | ||
from pyrogram import Message | ||
from pyrogram.errors.exceptions import FloodWait | ||
|
||
from userge.utils import ( | ||
progress, | ||
take_screen_shot, | ||
extractMetadata, | ||
createParser | ||
) | ||
|
||
LOGGER = userge.getLogger(__name__) | ||
|
||
|
||
@userge.on_cmd("upload", about="upload files to telegram") | ||
async def uploadtotg(_, message): | ||
async def uploadtotg(_, message: Message): | ||
try: | ||
string = Path(message.text.split(" ", maxsplit=1)[1]) | ||
|
||
except IndexError: | ||
await message.edit("wrong syntax\n`.upload <path>`") | ||
else: | ||
await message.delete() | ||
await explorer(string, message) | ||
await explorer(string, message.chat.id) | ||
|
||
|
||
async def explorer(path: Path, message: Message): | ||
async def explorer(path: Path, chatid): | ||
if path.is_file(): | ||
try: | ||
await upload(path, message.chat.id) | ||
await upload(path, chatid) | ||
except FloodWait as x: | ||
time.sleep(x.x) # asyncio sleep ? | ||
await message.edit(f"`Floodwait occured for {x.x} seconds") | ||
elif path.is_dir(): | ||
for i in path.iterdir(): | ||
await explorer(i, message) | ||
await explorer(i, chatid) | ||
|
||
|
||
async def upload(path: Path, chat_id: int): | ||
|
||
message = await userge.send_message(chat_id, "`Starting ...`") | ||
message_id = message.message_id | ||
size = path.stat().st_size | ||
if int(size) > (1024 * 1024 * 1500): | ||
await message.edit('<i>File Size Too Large(' + str(round(int(size) / (1024 * 1024), 0)) + 'MB)</i> \U0001f61e') | ||
return | ||
|
||
LOGGER.info(path.name) | ||
metadata = extractMetadata(createParser(str(path))) | ||
duration = 0 | ||
width = 0 | ||
height = 0 | ||
title = None | ||
artist = None | ||
thumb = 'resources/userge(8).png' | ||
caption = path.name | ||
if metadata: | ||
if metadata.has("duration"): | ||
duration = metadata.get('duration').seconds | ||
if metadata.has("width"): | ||
width = metadata.get("width") | ||
if metadata.has("height"): | ||
height = metadata.get("height") | ||
if metadata.has("title"): | ||
title = metadata.get("title") | ||
if metadata.has("artist"): | ||
artist = metadata.get("artist") | ||
filename = str(path) | ||
if filename.endswith((".mkv", ".mp4", ".webm")) and duration: | ||
thumb = await take_screen_shot(f'./{filename}', duration) | ||
start_time = time.time() | ||
await message.edit('<i>Trying To Upload......</i> \U0001f9D0') | ||
try: | ||
if filename.endswith((".mp3", ".flac", ".wav", ".m4a")): | ||
await userge.send_audio( | ||
chat_id=chat_id, | ||
audio=filename, | ||
caption=caption, | ||
duration=duration, | ||
title=title, | ||
performer=artist, | ||
thumb=thumb, | ||
progress=progress, | ||
progress_args=( | ||
userge, | ||
message_id, | ||
chat_id, | ||
start_time, | ||
'<i>Uploading......</i>\U0001f60E' | ||
) | ||
) | ||
elif filename.endswith((".mkv", ".mp4", ".webm")): | ||
await userge.send_video( | ||
chat_id=chat_id, | ||
video=filename, | ||
caption=caption, | ||
duration=duration, | ||
width=width, | ||
height=height, | ||
thumb=thumb, | ||
supports_streaming=True, | ||
progress=progress, | ||
progress_args=( | ||
userge, | ||
message_id, | ||
chat_id, | ||
start_time, | ||
'<i>Uploading......</i>\U0001f60E' | ||
) | ||
) | ||
else: | ||
await userge.send_document( | ||
chat_id=chat_id, | ||
document=filename, | ||
thumb=thumb, | ||
caption=caption, | ||
progress=progress, | ||
progress_args=( | ||
userge, | ||
message_id, | ||
chat_id, | ||
start_time, | ||
'<i>Uploading......</i>\U0001f60E' | ||
) | ||
) | ||
|
||
await userge.edit_message_text( | ||
chat_id=chat_id, | ||
message_id=message_id, | ||
text='<i>Uploaded Successfully</i> \U0001f618' | ||
) | ||
except Exception as e: | ||
LOGGER.exception("Exception occurred") | ||
await userge.edit_message_text( | ||
chat_id=chat_id, | ||
message_id=message_id, | ||
text=f"<b>ERROR:</b> <i>{e}</i>" | ||
thumb_image_path = 'resources/userge(8).png' | ||
message = await userge.send_message(chat_id, f"`Uploading {path.name} ...`") | ||
start_t = datetime.now() | ||
c_time = time.time() | ||
doc_caption = path.name | ||
the_real_download_location = await userge.send_document( | ||
chat_id=chat_id, | ||
document=str(path), | ||
thumb=thumb_image_path, | ||
caption=doc_caption, | ||
parse_mode="html", | ||
disable_notification=True, | ||
progress=progress, | ||
progress_args=( | ||
"uploading", message, c_time | ||
) | ||
) | ||
end_t = datetime.now() | ||
ms = (end_t - start_t).seconds | ||
await message.edit(f"Uploaded in {ms} seconds") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,72 @@ | ||
import time | ||
import math | ||
import asyncio | ||
from .tools import humanbytes, TimeFormatter | ||
import time | ||
|
||
|
||
async def progress(current, total, client, message_id, chat_id, start, status): | ||
async def progress( | ||
current, | ||
total, | ||
ud_type, | ||
message, | ||
start | ||
): | ||
now = time.time() | ||
diff = now - start | ||
|
||
if current == total or round(diff % 15) == 0: | ||
if round(diff % 10.00) == 0 or current == total: | ||
# if round(current / total * 100, 0) % 5 == 0: | ||
percentage = current * 100 / total | ||
speed = current / diff | ||
|
||
elapsed_time = round(diff) * 1000 | ||
time_to_completion = round((total - current) / speed) * 1000 | ||
estimated_total_time = elapsed_time + time_to_completion | ||
time_to_completion = await TimeFormatter(milliseconds=time_to_completion) | ||
estimated_total_time = await TimeFormatter(milliseconds=estimated_total_time) | ||
|
||
progress = "<code>{}{}</code>\n<i>{}%</i> | <i>{}ps\n{}</i> | <i>{}</i>\n<b>Total:</b> <i>{}</i>\n<b>Rem:</b> <i>{}</i>".format( | ||
elapsed_time = time_formatter(milliseconds=elapsed_time) | ||
estimated_total_time = time_formatter(milliseconds=estimated_total_time) | ||
|
||
progress = "[{0}{1}] \nP: {2}%\n".format( | ||
''.join(["█" for i in range(math.floor(percentage / 5))]), | ||
''.join(["░" for i in range(20 - math.floor(percentage / 5))]), | ||
round(percentage, 2), | ||
await humanbytes(speed), | ||
await humanbytes(current), | ||
await humanbytes(total), | ||
estimated_total_time if estimated_total_time != '' else "0s", | ||
time_to_completion if time_to_completion != '' else "0s" | ||
) | ||
round(percentage, 2)) | ||
|
||
tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format( | ||
humanbytes(current), | ||
humanbytes(total), | ||
humanbytes(speed), | ||
# elapsed_time if elapsed_time != '' else "0 s", | ||
estimated_total_time if estimated_total_time != '' else "0 s" | ||
) | ||
try: | ||
if current == total: | ||
await client.edit_message_text( | ||
chat_id, | ||
message_id, | ||
text="<i>Completing Process...</i>\n<b>Please Wait !</b>" | ||
await message.edit( | ||
text="{}\n {}".format( | ||
ud_type, | ||
tmp | ||
) | ||
) | ||
except: | ||
pass | ||
|
||
await asyncio.sleep(1) | ||
|
||
await client.edit_message_text( | ||
chat_id, | ||
message_id, | ||
text="{}\n{}".format( | ||
status, | ||
progress | ||
) | ||
) | ||
def humanbytes(size): | ||
# https://stackoverflow.com/a/49361727/4723940 | ||
# 2**10 = 1024 | ||
if not size: | ||
return "" | ||
power = 2**10 | ||
n = 0 | ||
Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'} | ||
while size > power: | ||
size /= power | ||
n += 1 | ||
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B' | ||
|
||
|
||
except Exception: | ||
pass | ||
def time_formatter(milliseconds: int) -> str: | ||
seconds, milliseconds = divmod(int(milliseconds), 1000) | ||
minutes, seconds = divmod(seconds, 60) | ||
hours, minutes = divmod(minutes, 60) | ||
days, hours = divmod(hours, 24) | ||
tmp = ((str(days) + "d, ") if days else "") + \ | ||
((str(hours) + "h, ") if hours else "") + \ | ||
((str(minutes) + "m, ") if minutes else "") + \ | ||
((str(seconds) + "s, ") if seconds else "") + \ | ||
((str(milliseconds) + "ms, ") if milliseconds else "") | ||
return tmp[:-2] |
Oops, something went wrong.