Skip to content

Commit

Permalink
tweaking the progress.py
Browse files Browse the repository at this point in the history
Signed-off-by: Udith <[email protected]>
  • Loading branch information
uaudith committed Mar 23, 2020
1 parent aec6e5b commit 1da0e6d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 79 deletions.
1 change: 0 additions & 1 deletion run
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash

python3 -m userge
82 changes: 37 additions & 45 deletions userge/plugins/upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import time
import asyncio
from datetime import datetime
from pathlib import Path

Expand All @@ -14,59 +13,55 @@
thumb_image_path = 'resources/userge(8).png'


@userge.on_cmd("upload",
about="upload files to telegram")
@userge.on_cmd("upload", about="upload files to telegram")
async def uploadtotg(_, message: Message):
try:
string = Path(message.text.split(" ", maxsplit=1)[1])

except IndexError:
await userge.send_err(message,
text="wrong syntax\n`.upload <path>`")

await message.edit("wrong syntax\n`.upload <path>`")
else:
await message.delete()
await explorer(string, message.chat.id)


async def explorer(path: Path,
chatid: int):
async def explorer(path: Path, chatid):
if path.is_file():
try:
await upload(path, chatid)
await userge.send_chat_action(chatid, "cancel")
except FloodWait as x:
await asyncio.sleep(x.x + 5)
await explorer(path, chatid)

time.sleep(x.x) # asyncio sleep ?
elif path.is_dir():
for i in path.iterdir():
await explorer(i, chatid)


async def upload(path: Path, chat_id: int):
if path.name.endswith((".mkv", ".mp4", ".webm")):
await userge.send_chat_action(chat_id, "upload_video")
await vid_upload(chat_id, path)
else:
await userge.send_chat_action(chat_id, "upload_document")
await doc_upload(chat_id, path)


async def doc_upload(chat_id, path):
message = await userge.send_message(chat_id,
text=f"`Uploading {path.name} ...`")

message = await userge.send_message(chat_id, f"`Uploading {path.name} ...`")
start_t = datetime.now()
c_time = time.time()

await userge.send_document(chat_id=chat_id,
document=str(path),
thumb=thumb_image_path,
caption=path.name,
parse_mode="html",
disable_notification=True,
progress=progress,
progress_args=(
"uploading", message, c_time))

the_real_download_location = await userge.send_document(
chat_id=chat_id,
document=str(path),
thumb=thumb_image_path,
caption=path.name,
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")
Expand All @@ -75,29 +70,26 @@ async def doc_upload(chat_id, path):
async def vid_upload(chat_id, path):
strpath = str(path)
metadata = extractMetadata(createParser(strpath))

thumb = await take_screen_shot(strpath, metadata.get("duration").seconds) \
if (metadata and metadata.has("duration")) else \
thumb_image_path

message = await userge.send_message(chat_id,
text=f"`Uploading {path.name} ...` as a video")

thumb = await take_screen_shot(strpath, metadata.get("duration").seconds) if (
metadata and metadata.has("duration")) else \
thumb_image_path
message = await userge.send_message(chat_id, f"`Uploading {path.name} ...` as a video")
start_t = datetime.now()
c_time = time.time()

await userge.send_video(chat_id=chat_id,
video=strpath,
thumb=thumb if thumb else thumb_image_path,
caption=path.name,
parse_mode="html",
disable_notification=True,
progress=progress,
progress_args=(
"uploading", message, time.time()))

the_real_download_location = await userge.send_video(
chat_id=chat_id,
video=strpath,
duration=metadata.get("duration").seconds,
thumb=thumb if thumb else thumb_image_path,
caption=path.name,
parse_mode="html",
disable_notification=True,
progress=progress,
progress_args=(
"uploading", message, c_time
)
)
os.remove(thumb)
end_t = datetime.now()
ms = (end_t - start_t).seconds

await message.edit(f"Uploaded in {ms} seconds")
84 changes: 51 additions & 33 deletions userge/utils/progress.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,60 @@
import math
from pyrogram import Message
from pyrogram.errors.exceptions import FloodWait
import time
from .tools import humanbytes, time_formatter


async def progress(current,
total,
ud_type,
message,
start):
async def progress(
current,
total,
ud_type,
message: Message,
start
):
now = time.time()
diff = now - start
if diff % 10 < 0.05 or current == total:
percentage = current * 100 // total
speed = current // diff
time_to_completion = (total - current) // speed
time_to_completion = time_formatter(seconds=time_to_completion)
progress_str = "Progress :: {}%\n".format(int(percentage))
out = progress_str + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
humanbytes(current),
humanbytes(total),
humanbytes(speed),
time_to_completion if time_to_completion != '' else "0 s"
)
text = "{}\n{}".format(
ud_type,
out
)
if message.text != text:
try:
await message.edit(text)
except FloodWait:
pass

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

elapsed_time = await time_formatter(milliseconds=elapsed_time)
estimated_total_time = await 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))
def humanbytes(size):
# https://stackoverflow.com/a/49361727/4723940
# 2**10 = 1024
if not size:
return ""
power = 1024
n = 0
Dic_powerN = {0: ' ', 1: 'Ki', 2: 'Mi', 3: 'Gi', 4: 'Ti'}
while size > power:
size /= power
n += 1
return "{:.2f} {}B".format(size, Dic_powerN[n])

tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
await humanbytes(current),
await humanbytes(total),
await humanbytes(speed),
# elapsed_time if elapsed_time != '' else "0 s",
estimated_total_time if estimated_total_time != '' else "0 s"
)

try:
await message.edit("{}\n {}".format(ud_type, tmp))

except:
pass
def time_formatter(seconds: int) -> str:
minutes, seconds = divmod(int(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 "")
return tmp[:-2]

0 comments on commit 1da0e6d

Please sign in to comment.