Skip to content

Commit

Permalink
added download location in config.py
Browse files Browse the repository at this point in the history
changes in thumbnail.py to avoid resizing
and some formatting

Signed-off-by: Udith <[email protected]>
  • Loading branch information
uaudith committed Mar 24, 2020
1 parent 9c280e7 commit d3cd05e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
Binary file removed resources/geckodriver
Binary file not shown.
4 changes: 2 additions & 2 deletions userge/core/_userge/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ async def err(self,
del_in: int = -1,
parse_mode: Union[str, None] = object,
disable_web_page_preview: bool = None,
reply_markup: InlineKeyboardMarkup = None) -> None:
reply_markup: InlineKeyboardMarkup = None):
"""
You can send error messages using this method
Expand Down Expand Up @@ -350,7 +350,7 @@ async def force_err(self,
parse_mode: Union[str, None] = object,
disable_web_page_preview: bool = None,
reply_markup: InlineKeyboardMarkup = None,
**kwargs) -> None:
**kwargs):
"""
This will first try to edit that message. If it found any errors
it will reply that message.
Expand Down
6 changes: 3 additions & 3 deletions userge/plugins/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from datetime import datetime
from pySmartDL import SmartDL
from userge import userge, Message
from userge import userge, Message, Config
from userge.utils import progress, humanbytes

LOGGER = userge.getLogger(__name__)
Expand All @@ -23,7 +23,7 @@ async def down_load_media(message: Message):

the_real_download_location = await userge.download_media(
message=message.reply_to_message,
file_name='.',
file_name=Config.DOWN_PATH,
progress=progress,
progress_args=(
"trying to download", message, c_time
Expand All @@ -45,7 +45,7 @@ async def down_load_media(message: Message):
url = url.strip()
custom_file_name = custom_file_name.strip()

download_file_path = os.path.join('.', custom_file_name)
download_file_path = os.path.join(Config.DOWN_PATH, custom_file_name)
downloader = SmartDL(url, download_file_path, progress_bar=False)
downloader.start(blocking=False)
c_time = time.time()
Expand Down
24 changes: 11 additions & 13 deletions userge/plugins/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import time
from datetime import datetime
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
from PIL import Image
from userge import userge, Config, Message
from userge.utils import progress
Expand All @@ -13,7 +11,7 @@
@userge.on_cmd('sthumb', about="__Save thumbnail__")
async def save_thumb_nail(message: Message):
await message.edit("processing ...")
if message.reply_to_message is not None:
if message.reply_to_message is not None and message.reply_to_message.photo:
start_t = datetime.now()
c_time = time.time()

Expand All @@ -24,14 +22,14 @@ async def save_thumb_nail(message: Message):
progress_args=(
"trying to download", message, c_time))

Image.open(downloaded_file_name).convert("RGB").save(downloaded_file_name)
metadata = extractMetadata(createParser(downloaded_file_name))
height = 0
if metadata and metadata.has("height"):
height = metadata.get("height")
img = Image.open(downloaded_file_name)
img.resize((320, height or 320))
img.save(THUMB_PATH, "JPEG")
Image.open(downloaded_file_name).convert("RGB").save(THUMB_PATH, 'JPEG')
# metadata = extractMetadata(createParser(downloaded_file_name))
# height = 0
# if metadata and metadata.has("height"):
# height = metadata.get("height")
# img = Image.open(downloaded_file_name)
# img.resize((320, height or 320))
# img.save(THUMB_PATH, "JPEG")
os.remove(downloaded_file_name)
end_t = datetime.now()
ms = (end_t - start_t).seconds
Expand All @@ -43,7 +41,7 @@ async def save_thumb_nail(message: Message):

@userge.on_cmd('dthumb', about="__Delete thumbnail__")
async def clear_thumb_nail(message: Message):
await message.edit("processing ...")
await message.edit("`processing ...`")
if os.path.exists(THUMB_PATH):
os.remove(THUMB_PATH)

Expand Down Expand Up @@ -86,7 +84,7 @@ async def get_thumb_nail(message: Message):
)
os.remove(downloaded_file_name)
await message.delete()"""

await message.err("issues")

elif os.path.exists(THUMB_PATH):
Expand Down
2 changes: 1 addition & 1 deletion userge/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class Config:

LANG = os.environ.get("PREFERRED_LANGUAGE", "en")

DOWN_PATH = "downloads/"
DOWN_PATH = "./downloads/"
17 changes: 8 additions & 9 deletions userge/utils/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@ async def progress(current: int,
ud_type: str,
message: Message,
start: int):

now = time.time()
diff = now - start
if diff % 10 < 0.1 or current == total:
percentage = current * 100 // total
speed = current // diff
time_to_completion = (total - current) // speed
time_to_completion = await time_formatter(seconds=time_to_completion)
time_to_completion = await time_formatter(seconds=int(time_to_completion))
progress_str = "Progress :: {}%\n".format(int(percentage))

out = progress_str + "{0}\n{1} of {2}\nSpeed: {3}/s\nETA: {4}\n".format(
ud_type,
await humanbytes(current),
await humanbytes(total),
await humanbytes(speed),
time_to_completion if time_to_completion != '' else "0 s"
ud_type,
await humanbytes(current),
await humanbytes(total),
await humanbytes(speed),
time_to_completion if time_to_completion != '' else "0 s"
)

if message.text != out:
try:
await message.edit(out)
except FloodWait:
pass
pass
7 changes: 2 additions & 5 deletions userge/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
async def humanbytes(size):
if not size:
return ""

power = 2**10
power = 1024
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'
return "{:.2f} {}B".format(size, Dic_powerN[n])


async def time_formatter(seconds: int) -> str:
Expand Down

0 comments on commit d3cd05e

Please sign in to comment.