Skip to content

Commit

Permalink
Merge pull request #33 from Jigarvarma2005/main
Browse files Browse the repository at this point in the history
'Refactored by Sourcery' (#1)
  • Loading branch information
Mrvishal2k2 authored Oct 31, 2023
2 parents b307131 + 9891a94 commit aa7cbf2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
12 changes: 7 additions & 5 deletions root/plugins/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def rep_rename_call(c, m):


async def renamer(c,m,as_file=False):
bot_msg = await c.get_messages(m.chat.id, m.reply_to_message.id)
bot_msg = await c.get_messages(m.chat.id, m.reply_to_message.id)
todown = bot_msg.reply_to_message # msg with media
new_f_name = m.text # new name
media = todown.document or todown.video or todown.audio or todown.voice or todown.video_note or todown.animation
Expand All @@ -62,7 +62,7 @@ async def renamer(c,m,as_file=False):
return await m.reply_text(text=f"Limits of telegram file name is 64 charecters only\nReduce some and try again.")

d_msg = await m.reply_text(Translation.DOWNLOAD_MSG,True)
d_location = Config.DOWNLOAD_LOCATION + "/" + str(m.chat.id) + "/"
d_location = f"{Config.DOWNLOAD_LOCATION}/{str(m.chat.id)}/"
d_time = time.time()

try:
Expand Down Expand Up @@ -97,7 +97,8 @@ async def renamer(c,m,as_file=False):
u_msg = await m.reply_text(Translation.UPLOAD_MSG,quote=True)

# try to get thumb to use for later upload
thumb_image_path = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id) + ".jpg"
thumb_image_path = (
f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}.jpg")
if not os.path.exists(thumb_image_path):
mes = await thumb(m.from_user.id)
if mes is not None:
Expand Down Expand Up @@ -134,7 +135,7 @@ async def convert_call(c,m):

usr_msg = m.message.reply_to_message
d_msg = await m.message.edit_text(Translation.DOWNLOAD_MSG)
d_location = Config.DOWNLOAD_LOCATION + "/" + str(m.from_user.id) + "/"
d_location = f"{Config.DOWNLOAD_LOCATION}/{str(m.from_user.id)}/"
d_time = time.time()

try:
Expand Down Expand Up @@ -167,7 +168,8 @@ async def convert_call(c,m):
u_msg = await usr_msg.reply_text(Translation.UPLOAD_MSG,quote=True)

# try to get thumb to use later while uploading..
thumb_image_path = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id) + ".jpg"
thumb_image_path = (
f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}.jpg")
if not os.path.exists(thumb_image_path):
mes = await thumb(m.from_user.id)
if mes is not None:
Expand Down
17 changes: 10 additions & 7 deletions root/plugins/custom_thumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,42 @@
async def save_photo(c,m):
v = await m.reply_text("Saving Thumbnail",True)
if m.media_group_id is not None:
download_location = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id) + "/" + str(m.media_group_id) + "/"
download_location = f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}/{str(m.media_group_id)}/"
os.makedirs(download_location, exist_ok=True)
await df_thumb(m.from_user.id, m.id)
await c.download_media(
message=m,
file_name=download_location
)
else:
download_location = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id) + ".jpg"
download_location = (
f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}.jpg"
)
await df_thumb(m.from_user.id, m.id)
await c.download_media(
message=m,
file_name=download_location
)
)
try:
await v.edit_text("Thumbnail Saved Successfully.. 😍")
except Exception as e:
log.error(f"#Error {e}")

@Client.on_message(filters.command(["deletethumb"]))
async def delete_thumbnail(c,m):
download_location = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id)
download_location = f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}"
try:
os.remove(download_location + ".jpg")
os.remove(f"{download_location}.jpg")
await del_thumb(m.from_user.id)
except Exception as e:
log.error(f"Error in removing thumb {e}")
pass
await m.reply_text("Thumbnail was removed Successfully 😋",quote=True)

@Client.on_message(filters.command(["showthumb"]))
async def show_thumbnail(c,m):
thumb_image_path = Config.DOWNLOAD_LOCATION + "/thumb/" + str(m.from_user.id) + ".jpg"
thumb_image_path = (
f"{Config.DOWNLOAD_LOCATION}/thumb/{str(m.from_user.id)}.jpg"
)
msgg = await m.reply_text("Checking Thumbnail...",quote=True)

if not os.path.exists(thumb_image_path):
Expand Down
36 changes: 26 additions & 10 deletions root/plugins/main_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,44 @@
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(__name__)

@Client.on_message(filters.document | filters.video | filters.audio | filters.voice | filters.video_note | filters.animation)
@Client.on_message(filters.document | filters.video | filters.audio | filters.voice | filters.video_note | filters.animation)
async def rename_filter(c,m):
media = m.document or m.video or m.audio or m.voice or m.video_note or m.animation
text = ""
button = []
try:
filename = media.file_name
text += f"FileName:\n{filename}\n"
except:
# some files dont gib name ..
filename = None

text += "Select the desired Option"
button.append([InlineKeyboardButton("Rename as File", callback_data="rename_file")])
# Thanks to albert for mime_type suggestion
button = [
[InlineKeyboardButton("Rename as File", callback_data="rename_file")]
]
# Thanks to albert for mime_type suggestion
if media.mime_type.startswith("video/"):
# how the f the other formats can be uploaded as video
button.append([InlineKeyboardButton("Rename as Video",callback_data="rename_video")])
button.append([InlineKeyboardButton("Convert to File",callback_data="convert_file")])
button.append([InlineKeyboardButton("Convert to Video ",callback_data="convert_video")])
button.extend(
(
[
InlineKeyboardButton(
"Rename as Video", callback_data="rename_video"
)
],
[
InlineKeyboardButton(
"Convert to File", callback_data="convert_file"
)
],
[
InlineKeyboardButton(
"Convert to Video ", callback_data="convert_video"
)
],
)
)
button.append([InlineKeyboardButton("Cancel ❌",callback_data="cancel")])


try:
await m.reply_text(text,quote=True,
Expand Down
14 changes: 6 additions & 8 deletions root/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ def __init__(self, id, msg_id):

async def df_thumb(id, msg_id):
with INSERTION_LOCK:
msg = SESSION.query(Thumbnail).get(id)
if not msg:
msg = Thumbnail(id, msg_id)
SESSION.add(msg)
SESSION.flush()
else:
if msg := SESSION.query(Thumbnail).get(id):
SESSION.delete(msg)
file = Thumbnail(id, msg_id)
SESSION.add(file)
else:
msg = Thumbnail(id, msg_id)
SESSION.add(msg)
SESSION.flush()
SESSION.commit()

async def del_thumb(id):
Expand All @@ -49,7 +48,6 @@ async def del_thumb(id):

async def thumb(id):
try:
t = SESSION.query(Thumbnail).get(id)
return t
return SESSION.query(Thumbnail).get(id)
finally:
SESSION.close()
32 changes: 12 additions & 20 deletions root/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ async def progress_for_pyrogram(
elapsed_time = TimeFormatter(milliseconds=elapsed_time)
estimated_total_time = TimeFormatter(milliseconds=estimated_total_time)

progress = "[{0}{1}] \n<b>◌Progress😉</b>:<code>〘 {2}% 〙</code>\n".format(
''.join(["●" for i in range(math.floor(percentage / 5))]),
''.join(["○" for i in range(20 - math.floor(percentage / 5))]),
round(percentage, 2))
progress = (
"[{0}{1}] \n<b>◌Progress😉</b>:<code>〘 {2}% 〙</code>\n".format(
''.join(["●" for _ in range(math.floor(percentage / 5))]),
''.join(["○" for _ in range(20 - math.floor(percentage / 5))]),
round(percentage, 2),
)
)

tmp = progress + "<b>Done:</b> <code>〘{0}</code><b> of </b><code> {1}〙</code>\n<b>◌Speed🚀</b>:<code>〘 {2}/s 〙</code>\n<b>◌Time Left⏳</b>:<code>〘 {3} 〙</code>\n".format(
humanbytes(current),
Expand All @@ -44,12 +47,7 @@ async def progress_for_pyrogram(
estimated_total_time if estimated_total_time != '' else "0 s"
)
try:
await message.edit(
text="{}\n {}".format(
ud_type,
tmp
)
)
await message.edit(text=f"{ud_type}\n {tmp}")
except:
pass

Expand All @@ -65,7 +63,7 @@ def humanbytes(size):
while size > power:
size /= power
n += 1
return str(round(size, 2)) + " " + Dic_powerN[n] + 'B'
return f"{str(round(size, 2))} {Dic_powerN[n]}B"


def TimeFormatter(milliseconds: int) -> str:
Expand All @@ -79,8 +77,7 @@ def TimeFormatter(milliseconds: int) -> str:
"s, ": (1 * 1000),
"ms ": 1,
}
for age in r_ange_s:
divisor = r_ange_s[age]
for age, divisor in r_ange_s.items():
v_m, remainder = divmod(remainder, divisor)
v_m = int(v_m)
if v_m != 0:
Expand Down Expand Up @@ -115,15 +112,10 @@ async def take_screen_shot(video_file, output_directory, ttl):
stdout, stderr = await process.communicate()
e_response = stderr.decode().strip()
t_response = stdout.decode().strip()
if os.path.lexists(out_put_file_name):
return out_put_file_name
return None
return out_put_file_name if os.path.lexists(out_put_file_name) else None

async def copy_file(input_file, output_dir):
output_file = os.path.join(
output_dir,
str(time.time()) + ".jpg"
)
output_file = os.path.join(output_dir, f"{str(time.time())}.jpg")
# https://stackoverflow.com/a/123212/4723940
copyfile(input_file, output_file)
return output_file
Expand Down

0 comments on commit aa7cbf2

Please sign in to comment.