Skip to content

Commit

Permalink
add -> rar support to unpack , ls to pathlib and unload to loader
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed May 6, 2020
1 parent 0b9be46 commit cd30964
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Aptfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tree
tree
2 changes: 2 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
"buildpacks": [
{
"url": "https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git"
}, {
"url": "https://github.com/HasibulKabir/heroku-buildpack-rarlab.git"
}, {
"url": "https://github.com/opendoor-labs/heroku-buildpack-p7zip"
}, {
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pymongo
pySmartDL
python-dotenv
pytz
rarfile
requests
search-engine-parser
selenium
Expand Down
2 changes: 1 addition & 1 deletion userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Config:

WELCOME_DELETE_TIMEOUT = 120

AUTOPIC_TIMEOUT = 120
AUTOPIC_TIMEOUT = 300

ALLOWED_CHATS = Filters.chat([])

Expand Down
20 changes: 19 additions & 1 deletion userge/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Client as RawClient, Message as RawMessage,
Filters, MessageHandler, InlineKeyboardMarkup,
ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply)
from pyrogram.client.handlers.handler import Handler
from pyrogram.api import functions

from userge import logging, Config
Expand All @@ -40,6 +41,7 @@ class Userge(RawClient):
"""Userge, the userbot"""
def __init__(self) -> None:
self._help_dict: Dict[str, Dict[str, str]] = {}
self._handlers: Dict[str, List[Tuple[Handler, int]]] = {}
self._imported: List[ModuleType] = []
self._tasks: List[Callable[[Any], Any]] = []
self._channel = self.getCLogger(__name__)
Expand Down Expand Up @@ -446,6 +448,14 @@ def _add_help(self,
else:
self._help_dict.update({mname: {cname: chelp}})

def _add_handler(self, module: str, handler: Handler, group: int) -> None:
mname = module.split('.')[-1]
if mname in self._handlers:
self._handlers[mname].append((handler, group))
else:
self._handlers[mname] = [(handler, group)]
self.add_handler(handler, group)

def _build_decorator(self,
log: str,
filters: Filters,
Expand All @@ -459,7 +469,7 @@ async def _template(_: RawClient, __: RawMessage) -> None:
_LOG.debug(_LOG_STR, f"Loading => [ async def {func.__name__}(message) ] " + \
f"from {func.__module__} `{log}`")
self._add_help(func.__module__, **kwargs)
self.add_handler(MessageHandler(_template, filters), group)
self._add_handler(func.__module__, MessageHandler(_template, filters), group)
return func
return _decorator

Expand All @@ -470,6 +480,14 @@ def load_plugin(self, name: str) -> None:
importlib.import_module(f"userge.plugins.{name}"))
_LOG.debug(_LOG_STR, f"Imported {self._imported[-1].__name__} Plugin Successfully")

def unload_plugin(self, module: str) -> Optional[List[str]]:
"""unload plugin from userge"""
if module not in self._handlers:
return None
for handler_, group_ in self._handlers[module]:
self.remove_handler(handler_, group_)
return list(self._help_dict.pop(module))

def load_plugins(self) -> None:
"""Load all Plugins"""
self._imported.clear()
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/misc/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def down_load_media(message: Message):
estimated_total_time)
await message.try_to_edit(
text=progress_str, disable_web_page_preview=True)
await asyncio.sleep(3)
await asyncio.sleep(6)
except Exception as e:
await message.err(e)
else:
Expand Down
6 changes: 3 additions & 3 deletions userge/plugins/misc/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ async def upload(self) -> None:
self._cancel()
if self._progress is not None:
await self._message.try_to_edit(self._progress)
await asyncio.sleep(3)
await asyncio.sleep(6)
if dl_loc and os.path.exists(dl_loc):
os.remove(dl_loc)
end_t = datetime.now()
Expand Down Expand Up @@ -766,7 +766,7 @@ async def download(self) -> None:
self._cancel()
if self._progress is not None:
await self._message.try_to_edit(self._progress)
await asyncio.sleep(3)
await asyncio.sleep(6)
end_t = datetime.now()
m_s = (end_t - start_t).seconds
if isinstance(self._output, HttpError):
Expand Down Expand Up @@ -794,7 +794,7 @@ async def copy(self) -> None:
self._cancel()
if self._progress is not None:
await self._message.try_to_edit(self._progress)
await asyncio.sleep(3)
await asyncio.sleep(6)
end_t = datetime.now()
m_s = (end_t - start_t).seconds
if isinstance(self._output, HttpError):
Expand Down
50 changes: 43 additions & 7 deletions userge/plugins/misc/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from typing import Union, List, Tuple, Sequence
from ctypes import c_bool, c_int, c_wchar_p

from rarfile import RarFile, is_rarfile

from userge import userge, Message, Config, pool
from userge.utils import humanbytes, time_formatter
from userge.utils.exceptions import ProcessCanceled
Expand Down Expand Up @@ -133,6 +135,8 @@ def _unpack(file_path: str,
is_finished) -> None:
if is_zipfile(file_path):
u_type = ZipFile
elif is_rarfile(file_path):
u_type = RarFile
else:
u_type = tar_open
with u_type(file_path, 'r') as p_f:
Expand Down Expand Up @@ -210,14 +214,17 @@ def get_info(self) -> Sequence[Tuple[str, int]]:
if is_zipfile(self._file_path):
with ZipFile(self._file_path, 'r') as z_f:
return tuple((z_.filename, z_.file_size) for z_ in z_f.infolist())
elif is_rarfile(self._file_path):
with RarFile(self._file_path, 'r') as r_f:
return tuple((r_.filename, r_.file_size) for r_ in r_f.infolist())
else:
with tar_open(self._file_path, 'r') as t_f:
return tuple((t_.name, t_.size) for t_ in t_f.getmembers())

@staticmethod
def is_supported(file_path: str) -> bool:
"""Returns file is supported or not"""
return is_zipfile(file_path) or is_tarfile(file_path)
return is_zipfile(file_path) or is_tarfile(file_path) or is_rarfile(file_path)


class SCLib(_BaseLib):
Expand Down Expand Up @@ -339,6 +346,33 @@ def combine(self) -> None:
pool.submit_thread(self._combine_worker, file_list)


@userge.on_cmd('ls', about={
'header': "list directory",
'usage': "{tr}ls [path]"})
async def ls_dir(message: Message) -> None:
"""list dir"""
path = message.input_str or '.'
if not exists(path):
await message.err("path not exists!")
return
path_ = Path(path)
out = f"<b>PATH</b> : <code>{path}</code>\n\n"
if path_.is_dir():
folders = ''
files = ''
for p_s in path_.iterdir():
if p_s.is_file():
size = os.stat(str(p_s)).st_size
files += f"📄 <code>{p_s.name}</code> <i>({humanbytes(size)})</i>\n"
else:
folders += f"📁 <code>{p_s.name}</code>\n"
out += folders + files
else:
size = os.stat(str(path_)).st_size
out += f"📄 <code>{path_.name}</code> <i>({humanbytes(size)})</i>\n"
await message.edit_or_send_as_file(out, parse_mode='html')


@userge.on_cmd('setdir', about={
'header': "set temporary working directory",
'usage': "{tr}setdir [path / name]"})
Expand Down Expand Up @@ -427,7 +461,7 @@ async def split_(message: Message) -> None:
s_obj.eta,
s_obj.completed_files,
s_obj.total_files))
await sleep(3)
await sleep(6)
if s_obj.output:
await message.err(s_obj.output)
else:
Expand Down Expand Up @@ -482,7 +516,7 @@ async def combine_(message: Message) -> None:
c_obj.eta,
c_obj.completed_files,
c_obj.total_files))
await sleep(3)
await sleep(6)
if c_obj.output:
await message.err(c_obj.output)
else:
Expand Down Expand Up @@ -536,7 +570,7 @@ async def _pack_helper(message: Message, tar: bool = False) -> None:
p_obj.final_file_path,
p_obj.completed_files,
p_obj.total_files))
await sleep(3)
await sleep(6)
if p_obj.output:
await message.err(p_obj.output)
else:
Expand All @@ -549,7 +583,8 @@ async def _pack_helper(message: Message, tar: bool = False) -> None:

@userge.on_cmd('unpack', about={
'header': "unpack packed file",
'usage': "{tr}unpack [zip file path | tar file path]"})
'usage': "{tr}unpack [file path]",
'types': ['zip', 'tar', 'rar']})
async def unpack_(message: Message) -> None:
"""unpack"""
file_path = message.input_str
Expand Down Expand Up @@ -581,7 +616,7 @@ async def unpack_(message: Message) -> None:
p_obj.final_file_path,
p_obj.completed_files,
p_obj.total_files))
await sleep(3)
await sleep(6)
if p_obj.output:
await message.err(p_obj.output)
else:
Expand All @@ -594,7 +629,8 @@ async def unpack_(message: Message) -> None:

@userge.on_cmd('packinfo', about={
'header': "File content of the pack",
'usage': "{tr}packinfo [zip file path | tar file path]"})
'usage': "{tr}packinfo [file path]",
'types': ['zip', 'tar', 'rar']})
async def packinfo_(message: Message) -> None:
"""packinfo"""
file_path = message.input_str
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/tools/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
async def getplugins(message: Message):
raw_ = get_all_plugins()
all_plugins = ['/'.join(i.split('.')) for i in raw_]
out_str = f"**--({len(raw_)}) Plugins Loaded!--**\n\n"
out_str = f"**--({len(raw_)}) Plugins Available!--**\n\n"
for plugin in all_plugins:
out_str += f" `{plugin}.py`\n"
await message.edit(text=out_str, del_in=0)
18 changes: 18 additions & 0 deletions userge/plugins/tools/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ async def load_cmd_handler(message: Message):
await message.edit("`Reply to Plugin`")


@userge.on_cmd('unload', about={
'header': "Unload plugin From Userge",
'usage': "{tr}unload [plugin name]"})
async def unload_cmd_handler(message: Message):
plugin = message.input_str
if not plugin:
await message.err("input plugin name!")
return
removed = userge.unload_plugin(plugin)
if removed:
out = f"plugin : `{plugin}` **Unloaded** successfully!\n\n"
out += f"**--removed ({len(removed)}) commands--**\n\n `"
out += '`\n `'.join(removed)
await message.edit(out + '`')
else:
await message.err(f"invalid plugin name -> {plugin}")


@userge.on_cmd('reload', about={'header': "Reload all plugins"})
async def reload_cmd_handler(message: Message):
await message.edit("`Reloading All Plugins`")
Expand Down
10 changes: 4 additions & 6 deletions userge/plugins/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ def _filter_updater(chat_id: int, name: str, content: str) -> None:
else:
FILTERS_DATA[chat_id] = {name: content}


def _filter_deleter(chat_id: int, name: str) -> None:
if chat_id in FILTERS_DATA and name in FILTERS_DATA[chat_id]:
FILTERS_DATA[chat_id].pop(name)
if not FILTERS_DATA[chat_id]:
FILTERS_DATA.pop(chat_id)


for flt in FILTERS_COLLECTION.find():
_filter_updater(flt['chat_id'], flt['name'], flt['content'])

Expand Down Expand Up @@ -88,8 +86,8 @@ async def add_filter(message: Message):
async def chat_filter(message: Message):
input_text = message.text.strip()
for name in FILTERS_DATA[message.chat.id]:
if input_text == name or \
input_text.startswith(f"{name} ") or \
input_text.endswith(f" {name}") or \
f" {name} " in input_text:
if (input_text == name
or input_text.startswith(f"{name} ")
or input_text.endswith(f" {name}")
or f" {name} " in input_text):
await message.reply(FILTERS_DATA[message.chat.id][name])
2 changes: 1 addition & 1 deletion userge/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
__version_mjaor__ = 0
__version_minor__ = 1
__version_micro__ = 4
__version_beta__ = 5
__version_beta__ = 6

__version__ = "{}.{}.{}".format(__version_mjaor__,
__version_minor__,
Expand Down

0 comments on commit cd30964

Please sign in to comment.