Skip to content

Commit

Permalink
added upload progress [terminal] (#8)
Browse files Browse the repository at this point in the history
* ...

* added progress arg

* added tqdm progressbar
  • Loading branch information
arunpt authored Oct 7, 2021
1 parent ce669de commit db87f4e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 57 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ python = ">=3.8,<4"
Pyrogram = "1.2.9"
TgCrypto = "1.2.2"
hachoir = "3.1.1"
tqdm = "4.62.3"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
Expand Down
29 changes: 14 additions & 15 deletions uploadgram/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

import math
from asyncio import sleep
from pyrogram.errors import (
FloodWait
)
from pyrogram.types import (
Message
)
from pyrogram.errors import FloodWait
from pyrogram.types import Message
from time import time
from .humanbytes import humanbytes
from .time_formatter import time_formatter
Expand All @@ -33,10 +29,15 @@ async def progress_for_pyrogram(
total: int,
message: Message,
sfw: int,
ud_type: str
pbar: bool,
ud_type: str,
):
now = time()
diff = now - sfw
if pbar is not None:
pbar.update(current)
if pbar and current == total:
pbar.set_description("uploaded")
if round(diff % 10.00) == 0 or current == total:
# if round(current / total * 100, 0) % 5 == 0:
try:
Expand All @@ -56,21 +57,19 @@ async def progress_for_pyrogram(
progress = "[{0}{1}] \nP: {2}%\n".format(
"".join(["█" for _ in range(math.floor(percentage / 5))]),
"".join(["░" for _ in range(20 - math.floor(percentage / 5))]),
round(percentage, 2))
round(percentage, 2),
)

tmp = progress + "{0} of {1}\nSpeed: {2}/s\nETA: {3}\n".format(
humanbytes(current),
humanbytes(total),
humanbytes(speed),
estimated_total_time if estimated_total_time != "" else "0 seconds"
estimated_total_time
if estimated_total_time != ""
else "0 seconds",
)
try:
await message.edit_text(
text="{}\n {}".format(
ud_type,
tmp
)
)
await message.edit_text(text="{}\n {}".format(ud_type, tmp))
except FloodWait as e:
await sleep(e.x)
except: # noqa: E722
Expand Down
4 changes: 2 additions & 2 deletions uploadgram/run_shell_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@


import asyncio
from typing import List
from typing import List, Tuple


async def run_command(shell_command: List) -> (int, int, str, str):
async def run_command(shell_command: List) -> Tuple[int, int, str, str]:
""" executes a shell_command,
and returns the stdout and stderr"""
process = await asyncio.create_subprocess_exec(
Expand Down
16 changes: 14 additions & 2 deletions uploadgram/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async def upload(
delete_on_success: bool = False,
thumbnail_file: str = None,
force_document: bool = False,
custom_caption: str = None
custom_caption: str = None,
console_progress: bool = False
):
# sent a message to verify write permission in the "to"
status_message = await uploadgram.send_message(
Expand All @@ -40,6 +41,7 @@ async def upload(
force_document,
custom_caption,
status_message,
console_progress
)
await status_message.delete()

Expand Down Expand Up @@ -83,7 +85,8 @@ async def moin(
delete_on_success=args.delete_on_success,
thumbnail_file=args.t,
force_document=args.fd,
custom_caption=args.caption
custom_caption=args.caption,
console_progress=args.progress
)
await uploadgram.stop()

Expand Down Expand Up @@ -137,6 +140,15 @@ def main():
default=None,
required=False
)
parser.add_argument(
"--progress",
nargs="?",
type=bool,
const=True,
help="show upload progress in terminal",
default=False,
required=False
)
args = parser.parse_args()
loop = asyncio.get_event_loop()
loop.run_until_complete(moin(args))
Expand Down
86 changes: 48 additions & 38 deletions uploadgram/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@
import os
from time import time
from asyncio import sleep
from tqdm import tqdm
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from pyrogram.types import (
Message
)
from .config import (
TG_AUDIO_TYPES,
TG_MAX_FILE_SIZE,
TG_VIDEO_TYPES
)
from pyrogram.types import Message
from .config import TG_AUDIO_TYPES, TG_MAX_FILE_SIZE, TG_VIDEO_TYPES
from .progress import progress_for_pyrogram
from .take_screen_shot import take_screen_shot

Expand All @@ -36,7 +31,8 @@ async def upload_dir_contents(
thumbnail_file: str,
force_document: bool,
custom_caption: str,
bot_sent_message: Message
bot_sent_message: Message,
console_progress: bool,
):
dir_contents = []
if not os.path.isdir(dir_path):
Expand All @@ -48,10 +44,7 @@ async def upload_dir_contents(
dir_contents = os.listdir(dir_path)
dir_contents.sort()
for dir_cntn in dir_contents:
current_name = os.path.join(
dir_path,
dir_cntn
)
current_name = os.path.join(dir_path, dir_cntn)

if os.path.isdir(current_name):
await upload_dir_contents(
Expand All @@ -60,7 +53,8 @@ async def upload_dir_contents(
thumbnail_file,
force_document,
custom_caption,
bot_sent_message
bot_sent_message,
console_progress,
)

elif os.stat(current_name).st_size < TG_MAX_FILE_SIZE:
Expand All @@ -69,12 +63,10 @@ async def upload_dir_contents(
thumbnail_file,
force_document,
custom_caption,
bot_sent_message
bot_sent_message,
console_progress,
)
if (
isinstance(response_message, Message) and
delete_on_success
):
if isinstance(response_message, Message) and delete_on_success:
os.remove(current_name)

await sleep(10)
Expand All @@ -85,27 +77,39 @@ async def upload_single_file(
thumbnail_file: str,
force_document: bool,
custom_caption: str,
bot_sent_message: Message
bot_sent_message: Message,
console_progress: bool,
):
if not os.path.exists(file_path):
return False
usr_sent_message = bot_sent_message
start_time = time()
b_asen_am_e = os.path.basename(file_path)
caption_al_desc = (
f"<code>{b_asen_am_e}</code>"
)
caption_al_desc = f"<code>{b_asen_am_e}</code>"
if custom_caption:
caption_al_desc = custom_caption

pbar = None
if console_progress:
pbar = tqdm(
total=os.path.getsize(file_path),
unit="iB",
unit_scale=True,
desc="uploading",
colour="green",
unit_divisor=1024,
miniters=1,
)

if file_path.upper().endswith(TG_VIDEO_TYPES) and not force_document:
return await upload_as_video(
usr_sent_message,
bot_sent_message,
file_path,
caption_al_desc,
thumbnail_file,
start_time
start_time,
pbar,
)

elif file_path.upper().endswith(TG_AUDIO_TYPES) and not force_document:
Expand All @@ -115,7 +119,8 @@ async def upload_single_file(
file_path,
caption_al_desc,
thumbnail_file,
start_time
start_time,
pbar,
)

else:
Expand All @@ -125,7 +130,8 @@ async def upload_single_file(
file_path,
caption_al_desc,
thumbnail_file,
start_time
start_time,
pbar,
)


Expand All @@ -135,7 +141,8 @@ async def upload_as_document(
file_path: str,
caption_rts: str,
thumbnail_file: str,
start_time: int
start_time: int,
pbar: tqdm,
):

return await usr_sent_message._client.send_document(
Expand All @@ -148,8 +155,9 @@ async def upload_as_document(
progress_args=(
bot_sent_message,
start_time,
pbar,
"UpLoading to Telegram",
)
),
)


Expand All @@ -159,7 +167,8 @@ async def upload_as_video(
file_path: str,
caption_rts: str,
thumbnail_file: str,
start_time: int
start_time: int,
pbar: tqdm,
):
try:
metadata = extractMetadata(createParser(file_path))
Expand All @@ -171,7 +180,7 @@ async def upload_as_video(
thumb_nail_img = await take_screen_shot(
file_path,
os.path.dirname(os.path.abspath(file_path)),
(duration / 2)
(duration / 2),
)
except AssertionError:
return await upload_as_document(
Expand All @@ -180,7 +189,8 @@ async def upload_as_video(
file_path,
caption_rts,
thumbnail_file,
start_time
start_time,
pbar,
)
try:
metadata = extractMetadata(createParser(thumb_nail_img))
Expand All @@ -203,13 +213,11 @@ async def upload_as_video(
progress_args=(
bot_sent_message,
start_time,
pbar,
"UpLoading to Telegram",
)
),
)
if (
thumb_nail_img and
os.path.exists(thumb_nail_img)
):
if thumb_nail_img and os.path.exists(thumb_nail_img):
os.remove(thumb_nail_img)
return _tmp_m

Expand All @@ -220,7 +228,8 @@ async def upload_as_audio(
file_path: str,
caption_rts: str,
thumbnail_file: str,
start_time: int
start_time: int,
pbar: tqdm,
):
metadata = extractMetadata(createParser(file_path))
duration = 0
Expand Down Expand Up @@ -255,6 +264,7 @@ async def upload_as_audio(
progress_args=(
bot_sent_message,
start_time,
pbar,
"UpLoading to Telegram",
)
),
)

0 comments on commit db87f4e

Please sign in to comment.