Skip to content

Commit

Permalink
add .cleardir , .direct and deldog things
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Apr 22, 2020
1 parent 6390ebb commit 3ec6e56
Show file tree
Hide file tree
Showing 16 changed files with 696 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ log.txt
gen
unknown_errors.txt
logs/
bin/
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
"description": "Set True if it is TeamDrive",
"value": "True"
},
"DOWN_PATH": {
"description": "Set your working directory (name or path)",
"required": false
},
"PREFERRED_LANGUAGE": {
"description": "Your Languge ( ex: if english => 'en' )",
"required": false
Expand Down
4 changes: 4 additions & 0 deletions config.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ G_DRIVE_IS_TD = False
# ----------- OPTIONAL ----------- #


# Set your working directory (name or path)
DOWN_PATH = "downloads/"


# Your Languge ( ex: if english => 'en' )
PREFERRED_LANGUAGE = ""

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
git+https://github.com/pyrogram/pyrogram.git@asyncio-dev
BeautifulSoup
cowpy
dnspython
emoji
Expand Down
35 changes: 29 additions & 6 deletions userge/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@


import os
import sys
import shutil
from typing import Set

import heroku3
from git import Repo
from pySmartDL import SmartDL
from dotenv import load_dotenv
from pyrogram import Filters

from userge import logging

LOG = logging.getLogger(__name__)

if sys.version_info[0] < 3 or sys.version_info[1] < 6:
LOG.info("You MUST have a python version of at least 3.6 !")
sys.exit()

CONFIG_FILE = "config.env"

if os.path.isfile(CONFIG_FILE):
Expand All @@ -28,7 +34,7 @@

if os.environ.get("_____REMOVE_____THIS_____LINE_____", None):
LOG.error("Please remove the line mentioned in the first hashtag from the config.env file")
quit(1)
sys.exit()


class Config:
Expand All @@ -48,7 +54,7 @@ class Config:

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

DOWN_PATH = "downloads/"
DOWN_PATH = os.environ.get("DOWN_PATH", "downloads/")

SCREENSHOT_API = os.environ.get("SCREENSHOT_API", None)

Expand Down Expand Up @@ -86,13 +92,12 @@ class Config:


if Config.SUDO_TRIGGER == '.':
raise Exception("Invalid SUDO_TRIGGER!")

LOG.info("Invalid SUDO_TRIGGER!, You can't use `.` as SUDO_TRIGGER")
sys.exit()

if not os.path.isdir(Config.DOWN_PATH):
LOG.info("Creating Download Path...")
os.makedirs(Config.DOWN_PATH)

os.mkdir(Config.DOWN_PATH)

if Config.HEROKU_API_KEY:
LOG.info("Checking Heroku App...")
Expand All @@ -117,3 +122,21 @@ class Config:
shutil.rmtree(tmp_heroku_git_path)

break

if not os.path.exists('bin'):
LOG.info("Creating BIN...")
os.mkdir('bin')

BINS = {
"https://raw.githubusercontent.com/yshalsager/megadown/master/megadown":
"bin/megadown",
"https://raw.githubusercontent.com/yshalsager/cmrudl.py/master/cmrudl.py":
"bin/cmrudl"}

LOG.info("Downloading BINs...")

for binary, path in BINS.items():
LOG.debug(f"Downloading {binary}...")
downloader = SmartDL(binary, path, progress_bar=False)
downloader.start()
os.chmod(path, 0o755)
4 changes: 2 additions & 2 deletions userge/core/_userge/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ def __build_decorator(self,

def __decorator(func: PYROFUNC) -> PYROFUNC:

async def __template(_: RawClient, message: RawMessage) -> None:
async def __template(_: RawClient, __: RawMessage) -> None:

await func(Message(self, message, **kwargs))
await func(Message(_, __, **kwargs))

LOG.debug(LOG_STR, f"Loading => [ async def {func.__name__}(message) ] " + \
f"from {func.__module__} `{log}`")
Expand Down
38 changes: 31 additions & 7 deletions userge/core/_userge/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@ class CLogger:

def __init__(self, client_: 'client.Userge', name: str) -> None:
self.__client = client_
self.__string = "**logger** : #" + name.split('.')[-1].upper() + "\n\n{}"
self.__string = self.__gen_string(name)

@staticmethod
def __gen_string(name: str) -> str:
return "**logger** : #" + name.split('.')[-1].upper() + "\n\n{}"

def update(self, name: str) -> None:
"""
update current logger name.
Parameters:
name (``str``):
New name to logger.
Returns:
None
"""

self.__string = self.__gen_string(name)

async def log(self, text: str) -> None:
"""
Expand Down Expand Up @@ -63,11 +80,18 @@ async def fwd_msg(self,
"""

LOG.debug(
LOG_STR, f"logging msg : {message_} to channel : {Config.LOG_CHANNEL_ID}")
LOG_STR, f"forwarding msg : {message_} to channel : {Config.LOG_CHANNEL_ID}")

if Config.LOG_CHANNEL_ID:
await self.__client.forward_messages(chat_id=Config.LOG_CHANNEL_ID,
from_chat_id=message_.chat.id,
message_ids=(message_.message_id),
as_copy=as_copy,
remove_caption=remove_caption)
if message_.media:
await self.log("**Forwarding Message...**")

await self.__client.forward_messages(chat_id=Config.LOG_CHANNEL_ID,
from_chat_id=message_.chat.id,
message_ids=(
message_.message_id),
as_copy=as_copy,
remove_caption=remove_caption)

else:
await self.log(message_.text)
41 changes: 30 additions & 11 deletions userge/core/_userge/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self,
**self.__msg_to_dict(message))

self.message_id: int
self.reply_to_message: RawMessage
self.reply_to_message: Optional[RawMessage]

if self.reply_to_message:
self.reply_to_message = self.__class__(self._client, self.reply_to_message)
Expand Down Expand Up @@ -191,8 +191,9 @@ async def send_as_file(self,
file_name for output file.
caption (``str``, *optional*):
caption for output file.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
delete_message (``bool``, *optional*):
If ``True``, the message will be deleted after sending the file.
Returns:
Expand All @@ -216,6 +217,9 @@ async def send_as_file(self,
os.remove(filename)

if log:
if isinstance(log, str):
self.__channel.update(log)

await self.__channel.fwd_msg(msg)

if delete_message:
Expand All @@ -242,8 +246,9 @@ async def reply(self,
Text of the message to be sent.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
quote (``bool``, *optional*):
If ``True``, the message will be sent as a reply to this message.
If *reply_to_message_id* is passed, this parameter will be ignored.
Expand Down Expand Up @@ -285,6 +290,9 @@ async def reply(self,
reply_markup=reply_markup)

if log:
if isinstance(log, str):
self.__channel.update(log)

await self.__channel.fwd_msg(msg)

del_in = del_in or Config.MSG_DELETE_TIMEOUT
Expand Down Expand Up @@ -314,8 +322,9 @@ async def edit(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
sudo (``bool``, *optional*):
If ``True``, sudo users supported.
parse_mode (``str``, *optional*):
Expand Down Expand Up @@ -360,6 +369,9 @@ async def edit(self,

else:
if log:
if isinstance(log, str):
self.__channel.update(log)

await self.__channel.fwd_msg(msg_)

del_in = del_in or Config.MSG_DELETE_TIMEOUT
Expand Down Expand Up @@ -392,8 +404,9 @@ async def force_edit(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
Expand Down Expand Up @@ -446,8 +459,9 @@ async def err(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
sudo (``bool``, *optional*):
If ``True``, sudo users supported.
parse_mode (``str``, *optional*):
Expand Down Expand Up @@ -495,8 +509,9 @@ async def force_err(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
Expand Down Expand Up @@ -544,8 +559,9 @@ async def try_to_edit(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
sudo (``bool``, *optional*):
If ``True``, sudo users supported.
parse_mode (``str``, *optional*):
Expand Down Expand Up @@ -598,8 +614,9 @@ async def edit_or_send_as_file(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
sudo (``bool``, *optional*):
If ``True``, sudo users supported.
parse_mode (``str``, *optional*):
Expand Down Expand Up @@ -655,8 +672,9 @@ async def reply_or_send_as_file(self,
Text of the message to be sent.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
quote (``bool``, *optional*):
If ``True``, the message will be sent as a reply to this message.
If *reply_to_message_id* is passed, this parameter will be ignored.
Expand Down Expand Up @@ -719,8 +737,9 @@ async def force_edit_or_send_as_file(self,
New text of the message.
del_in (``int``):
Time in Seconds for delete that message.
log (``bool``, *optional*):
log (``bool`` | ``str``, *optional*):
If ``True``, the message will be forwarded to the log channel.
If ``str``, the logger name will be updated.
parse_mode (``str``, *optional*):
By default, texts are parsed using both Markdown and HTML styles.
You can combine both syntaxes together.
Expand Down
2 changes: 1 addition & 1 deletion userge/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


if not os.path.isdir("logs"):
os.makedirs("logs")
os.mkdir("logs")


logging.basicConfig(level=logging.INFO,
Expand Down
4 changes: 4 additions & 0 deletions userge/plugins/misc/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
`.download https://speed.hetzner.de/100MB.bin | testing upload.bin`""")
async def down_load_media(message: Message):
await message.edit("Trying to Download...")

if not os.path.isdir(Config.DOWN_PATH):
os.mkdir(Config.DOWN_PATH)

if message.reply_to_message is not None:
start_t = datetime.now()
c_time = time.time()
Expand Down
3 changes: 3 additions & 0 deletions userge/plugins/misc/gdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ async def download(self) -> None:
if CREDS:
await self.__message.edit("`Loading GDrive Download...`")

if not os.path.isdir(Config.DOWN_PATH):
os.mkdir(Config.DOWN_PATH)

file_id, _ = self.__get_file_id()

Thread(target=self._download, args=(file_id,)).start()
Expand Down
27 changes: 27 additions & 0 deletions userge/plugins/tools/cleardir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2020 by UsergeTeam@Github, < https://github.com/UsergeTeam >.
#
# This file is part of < https://github.com/UsergeTeam/Userge > project,
# and is released under the "GNU v3.0 License Agreement".
# Please see < https://github.com/uaudith/Userge/blob/master/LICENSE >
#
# All rights reserved.


import os
import shutil

from userge import userge, Message, Config


@userge.on_cmd("cleardir", about="__Clear the current working directory__")
async def clear_dir(message: Message):
if not os.path.isdir(Config.DOWN_PATH):
await message.edit(
f'`working path : {Config.DOWN_PATH} not found and just created!`', del_in=5)

else:
shutil.rmtree(Config.DOWN_PATH, True)
await message.edit(
f'`working path : {Config.DOWN_PATH} cleared!`', del_in=5)

os.mkdir(Config.DOWN_PATH)
Loading

0 comments on commit 3ec6e56

Please sign in to comment.