Skip to content

Commit

Permalink
few formatings and webss.py for screenshots
Browse files Browse the repository at this point in the history
Signed-off-by: Udith <[email protected]>
  • Loading branch information
uaudith committed Mar 24, 2020
1 parent e73377e commit dc01ee9
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 66 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ emoji
wget
nest_asyncio
requests
pySmartDL
pySmartDL
selenium
Binary file added resources/geckodriver
Binary file not shown.
4 changes: 2 additions & 2 deletions userge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from userge.utils import Config, logging
from userge.core import Userge, Filters, get_collection
from userge.core import Userge, Filters, get_collection, Message


userge = Userge() # userge is the client name
userge = Userge() # userge is the client name
4 changes: 2 additions & 2 deletions userge/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from ._userge import Userge, Filters
from ._database import get_collection
from ._userge import Userge, Filters, Message
from ._database import get_collection
2 changes: 1 addition & 1 deletion userge/core/_userge/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .client import Userge, Filters
from .client import Userge, Filters, Message
2 changes: 1 addition & 1 deletion userge/core/_userge/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class Base:
_MAIN_STRING = "<<<! ##### ___{}___ ##### !>>>"
_SUB_STRING = "<<<! {} !>>>"
_LOG = logging.getLogger(__name__)
_LOG = logging.getLogger(__name__)
25 changes: 9 additions & 16 deletions userge/core/_userge/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os
import re
import asyncio
import nest_asyncio
from userge.utils import Config, logging
from typing import Dict, Union, Any, Callable
from .base import Base
from .message import Message
from pyrogram import Client, Filters, MessageHandler
from pyrogram.errors.exceptions import MessageAuthorRequired

logging.getLogger("pyrogram").setLevel(logging.WARNING)

Expand Down Expand Up @@ -46,11 +43,8 @@ async def get_user_dict(self,
if fname and lname:
full_name = fname + ' ' + lname

elif fname:
full_name = fname

elif lname:
full_name = lname
elif fname or lname:
full_name = fname or lname

else:
full_name = "user"
Expand All @@ -65,8 +59,8 @@ def on_cmd(self,
about: str,
group: int = 0,
trigger: str = '.',
only_me: bool = True,
**kwargs) -> Callable[[PYROFUNC], PYROFUNC]:
only_me: bool = True
) -> Callable[[PYROFUNC], PYROFUNC]:

found = [i for i in '()[]+*.\\|?:' if i in command]

Expand All @@ -87,8 +81,8 @@ def on_cmd(self,

return self.__build_decorator(log=f"On .{command_name} Command",
filters=filters_,
group=group,
**kwargs)
group=group
)

def on_new_member(self,
welcome_chats: Filters.chat,
Expand Down Expand Up @@ -130,11 +124,10 @@ def __add_help(self,
def __build_decorator(self,
log: str,
filters: Filters,
group: int,
**kwargs) -> Callable[[PYROFUNC], PYROFUNC]:
group: int
) -> Callable[[PYROFUNC], PYROFUNC]:

def __decorator(func: PYROFUNC) -> PYROFUNC:

async def __template(_: Client,
message: Message) -> None:

Expand All @@ -160,4 +153,4 @@ def begin(self) -> None:
self.run()

self._LOG.info(
self._MAIN_STRING.format("Exiting Userge"))
self._MAIN_STRING.format("Exiting Userge"))
49 changes: 24 additions & 25 deletions userge/core/_userge/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@


class Message(Base, Msg):

__ERROR_MSG_DELETE_TIMEOUT = 3
__ERROR_MSG_DELETE_TIMEOUT = 5
__ERROR_STRING = "**ERROR**: `{}`"

def __init__(self,
Expand All @@ -24,8 +23,8 @@ def __init__(self,

super().__init__(client=client, **kwargs_)

self.__filtered_input_str: str = None
self.__flags: Dict[str, str] = None
self.__filtered_input_str: str or None = None
self.__flags: Dict[str, str] or None = None
self.__kwargs = kwargs

@property
Expand Down Expand Up @@ -57,7 +56,7 @@ def __filter(self) -> None:
del_pre: bool = self.__kwargs.get('del_pre', False)
input_str: str = self.matches[0].group(1) or ''

text: List[str] = []
text: List[str] or str = []
flags: Dict[str, Union[str, bool, int]] = {}

for i in input_str.strip().split():
Expand Down Expand Up @@ -95,10 +94,10 @@ async def send_as_file(self,
self._SUB_STRING.format(f"Uploading {filename} To Telegram"))

await self._client.send_document(chat_id=self.chat.id,
document=filename,
caption=caption,
disable_notification=True,
reply_to_message_id=reply_to_id)
document=filename,
caption=caption,
disable_notification=True,
reply_to_message_id=reply_to_id)

os.remove(filename)

Expand All @@ -113,8 +112,8 @@ async def reply(self,
disable_web_page_preview: bool = None,
disable_notification: bool = None,
reply_to_message_id: int = None,
reply_markup: InlineKeyboardMarkup = None,
**kwargs) -> Msg:
reply_markup: InlineKeyboardMarkup = None
) -> Msg:

if quote is None:
quote = self.chat.type != "private"
Expand All @@ -141,15 +140,15 @@ async def edit(self,
del_in: int = -1,
parse_mode: Union[str, None] = object,
disable_web_page_preview: bool = None,
reply_markup: InlineKeyboardMarkup = None,
**kwargs) -> Msg:
reply_markup: InlineKeyboardMarkup = None
) -> Msg:

msg = await self._client.edit_message_text(chat_id=self.chat.id,
message_id=self.message_id,
text=text,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
reply_markup=reply_markup)
msg = await self._client.edit_message_text(chat_id=self.chat.id,
message_id=self.message_id,
text=text,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
reply_markup=reply_markup)

if del_in > 0:
await asyncio.sleep(del_in)
Expand All @@ -170,7 +169,7 @@ async def force_edit(self,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
reply_markup=reply_markup,
**kwargs)
)

except:
msg = await self.reply(text=text,
Expand All @@ -195,18 +194,18 @@ async def err(self,
del_in: int = -1,
parse_mode: Union[str, None] = object,
disable_web_page_preview: bool = None,
reply_markup: InlineKeyboardMarkup = None,
**kwargs) -> None:
reply_markup: InlineKeyboardMarkup = None
) -> None:

del_in = del_in if del_in > 0 \
else self.__ERROR_MSG_DELETE_TIMEOUT

await self.edit(text=self.__ERROR_STRING.format(text),
del_in=del_in,
parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview,
reply_markup=reply_markup,
**kwargs)
reply_markup=reply_markup
)

async def force_err(self,
text: str,
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def down_load_media(message):
await message.edit(text=current_message,
disable_web_page_preview=True)

display_message = current_message
# display_message = current_message
await asyncio.sleep(10)

except Exception as e:
Expand Down
18 changes: 8 additions & 10 deletions userge/plugins/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pyrogram import ChatPermissions
from userge import userge


@userge.on_cmd("lock", about="""\
__use this to lock group permissions__
Expand All @@ -18,7 +19,6 @@
**Example:**
`.lock [all | type]`""")

async def lock_perm(message):
"""
this function can lock chat permissions from tg group
Expand All @@ -41,7 +41,7 @@ async def lock_perm(message):
return

get_perm = await userge.get_chat(chat_id)

msg = get_perm.permissions.can_send_messages
media = get_perm.permissions.can_send_media_messages
other = get_perm.permissions.can_send_other_messages
Expand All @@ -66,7 +66,7 @@ async def lock_perm(message):
if lock_type == "msg":
msg = False
perm = "messages"

elif lock_type == "media":
media = False
perm = "audios, documents, photos, videos, video notes, voice notes"
Expand Down Expand Up @@ -108,7 +108,7 @@ async def lock_perm(message):
can_send_polls=polls,
can_change_info=info,
can_invite_users=invite,
can_pin_messages=pin,))
can_pin_messages=pin, ))

await message.edit(text=f"**🔒 Locked {perm} for this chat!**", del_in=3)

Expand All @@ -133,7 +133,6 @@ async def lock_perm(message):
**Example:**
`.unlock [all | type]`""")

async def unlock_perm(_, message):
"""
this function can unlock chat permissions from tg group
Expand All @@ -156,7 +155,7 @@ async def unlock_perm(_, message):
return

get_uperm = await userge.get_chat(chat_id)

umsg = get_uperm.permissions.can_send_messages
umedia = get_uperm.permissions.can_send_media_messages
uother = get_uperm.permissions.can_send_other_messages
Expand All @@ -176,7 +175,7 @@ async def unlock_perm(_, message):
can_change_info=True,
can_invite_users=True,
can_pin_messages=True,
can_add_web_page_previews=True,))
can_add_web_page_previews=True, ))

await message.edit(
text="**🔓 Unlocked all permission from this Chat!**", del_in=3)
Expand Down Expand Up @@ -231,8 +230,8 @@ async def unlock_perm(_, message):
can_send_polls=upolls,
can_change_info=uinfo,
can_invite_users=uinvite,
can_pin_messages=upin,))
can_pin_messages=upin, ))

await message.edit(text=f"**🔓 Unlocked {uperm} for this chat!**", del_in=3)

except:
Expand All @@ -245,7 +244,6 @@ async def unlock_perm(_, message):
**Usage:**
`Allows you to view permission types on/off status in the chat.`""")

async def view_perm(_, message):
"""
this function can check chat permissions from tg group
Expand Down
4 changes: 2 additions & 2 deletions userge/plugins/notes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from userge import userge, get_collection
from userge import userge, get_collection, Message

NOTES_COLLECTION = get_collection("notes")


@userge.on_cmd("notes", about="__List all saved notes__")
async def notes_active(message):
async def notes_active(message: Message):
out = "`There are no saved notes in this chat`"

for note in NOTES_COLLECTION.find({'chat_id': message.chat.id}, {'name': 1}):
Expand Down
4 changes: 1 addition & 3 deletions userge/plugins/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import time
from datetime import datetime
from pathlib import Path

from pyrogram import Message
from pyrogram.errors.exceptions import FloodWait

from userge import userge
from userge import userge, Message
from userge.utils import progress, take_screen_shot, extractMetadata, createParser

LOGGER = userge.getLogger(__name__)
Expand Down
41 changes: 41 additions & 0 deletions userge/plugins/webss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from userge import userge, Message
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from time import time
import os

profile = webdriver.FirefoxProfile()
profile.accept_untrusted_certs = True
options = Options()
options.headless = True
options.set_capability("marionette", False)


@userge.on_cmd("webss", about="__Get snapshot of a website__")
async def webss(message: Message):
await message.edit("`Processing`")
try:
path = await getimg(message.input_str)
except Exception as e:
await message.err(str(e), del_in=10)
else:
await userge.send_document(message.chat.id, path, caption=message.input_str)
if os.path.isfile(path):
os.remove(path)
await message.delete()


async def getimg(url):
driver = webdriver.Firefox(executable_path='resources/geckodriver',
options=options,
firefox_profile=profile)
try:
driver.get(url)
except Exception:
raise
else:
path = f'./screenshot/{time()}screenshot.png'
driver.save_screenshot(path)
return path
finally:
driver.quit()
2 changes: 1 addition & 1 deletion userge/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
runcmd,
humanbytes,
time_formatter
)
)
Loading

0 comments on commit dc01ee9

Please sign in to comment.