Skip to content

Commit

Permalink
check environment while importing voice_call + change translate delim
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Feb 4, 2022
1 parent b04d90d commit e904c05
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
21 changes: 7 additions & 14 deletions userge/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from userge.utils import time_formatter
from userge.utils.exceptions import UsergeBotNotFound
from .database import get_collection
from .ext import RawClient, pool
from .ext import RawClient
from .methods import Methods

_LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -116,7 +116,7 @@ async def _load_plugins(self) -> None:
for name in get_all_plugins():
try:
await self.load_plugin(name)
except ImportError as i_e:
except Exception as i_e:
_LOG.error(_LOG_STR, f"[{name}] - {i_e}")
await self.finalize_load()
_LOG.info(_LOG_STR, f"Imported ({len(_IMPORTED)}) Plugins => "
Expand All @@ -130,7 +130,7 @@ async def reload_plugins(self) -> int:
for imported in _IMPORTED:
try:
reloaded_ = importlib.reload(imported)
except ImportError as i_e:
except Exception as i_e:
_LOG.error(_LOG_STR, i_e)
else:
reloaded.append(reloaded_.__name__)
Expand Down Expand Up @@ -272,19 +272,9 @@ async def _shutdown(_sig: signal.Signals) -> None:
self.loop.add_signal_handler(
sig, lambda _sig=sig: self.loop.create_task(_shutdown(_sig)))

def _close_loop() -> None:
try:
self.loop.run_until_complete(_waiter())
except RuntimeError:
pass
self.loop.close()
_LOG.info(_LOG_STR, "Loop Closed !")
pool._stop() # pylint: disable=protected-access

try:
self.loop.run_until_complete(self.start())
except RuntimeError:
_close_loop()
return

for task in self._tasks:
Expand All @@ -305,7 +295,10 @@ def _close_loop() -> None:
except (asyncio.exceptions.CancelledError, RuntimeError):
pass
finally:
_close_loop()
try:
self.loop.run_until_complete(_waiter())
except RuntimeError:
pass
if _SEND_SIGNAL:
os.kill(os.getpid(), signal.SIGUSR1)

Expand Down
14 changes: 8 additions & 6 deletions userge/core/ext/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
__all__ = ['submit_thread', 'run_in_thread']

import asyncio
from typing import Any, Callable
import atexit
from concurrent.futures import ThreadPoolExecutor, Future
from functools import wraps, partial
from typing import Any, Callable

from userge import logging, Config

_LOG = logging.getLogger(__name__)
_LOG_STR = "<<<! |||| %s |||| !>>>"
_EXECUTOR = ThreadPoolExecutor(Config.WORKERS)
# pylint: disable=protected-access
_MAX = _EXECUTOR._max_workers


def submit_thread(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Future:
Expand All @@ -37,10 +40,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:


def _stop():
_EXECUTOR.shutdown(wait=False)
# pylint: disable=protected-access
_LOG.info(_LOG_STR, f"Stopped Pool : {_EXECUTOR._max_workers} Workers")
_EXECUTOR.shutdown()
_LOG.info(_LOG_STR, f"Stopped Pool : {_MAX} Workers")


# pylint: disable=protected-access
_LOG.info(_LOG_STR, f"Started Pool : {_EXECUTOR._max_workers} Workers")
atexit.register(_stop)
_LOG.info(_LOG_STR, f"Started Pool : {_MAX} Workers")
4 changes: 2 additions & 2 deletions userge/plugins/utils/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def translateme(message: Message):
is_poll = True
text = f'{replied.poll.question}'
for option in replied.poll.options:
text += f'\n,\n{option.text}'
text += f'\n\n\n{option.text}'
else:
text = replied.text or replied.caption
if not text:
Expand All @@ -62,7 +62,7 @@ async def translateme(message: Message):
return await message.err("Invalid destination language.")

if is_poll:
options = reply_text.text.split('\n,\n')
options = reply_text.text.split('\n\n\n')
if len(options) > 1:
question = options.pop(0)
await asyncio.gather(
Expand Down
23 changes: 10 additions & 13 deletions userge/plugins/utils/voice_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import json
import os
import re
import shutil
import shlex
import shutil
from json.decoder import JSONDecodeError
from traceback import format_exc
from typing import List, Tuple

Expand All @@ -26,23 +27,15 @@
CallbackQuery,
Message as RawMessage)
from pyrogram.types.messages_and_media.message import Str

from youtubesearchpython import VideosSearch

from json.decoder import JSONDecodeError

from userge import userge, Message, pool, filters, get_collection, Config
from userge.utils import time_formatter, import_ytdl, progress, runcmd
from userge.utils.exceptions import StopConversation

from pytgcalls import PyTgCalls
from pytgcalls import PyTgCalls, StreamType
from pytgcalls.exceptions import (
NodeJSNotInstalled,
TooOldNodeJSVersion,
NoActiveGroupCall,
AlreadyJoinedError,
NotInGroupCallError
)
from pytgcalls.types import Update
from pytgcalls.types.input_stream import (
AudioVideoPiped,
AudioPiped,
Expand All @@ -51,12 +44,16 @@
from pytgcalls.types.stream import (
StreamAudioEnded
)
from pytgcalls.types import Update
from pytgcalls import StreamType
from youtubesearchpython import VideosSearch

from userge import userge, Message, pool, filters, get_collection, Config
from userge.utils import time_formatter, import_ytdl, progress, runcmd
from userge.utils.exceptions import StopConversation

# https://github.com/pytgcalls/pytgcalls/blob/master/pytgcalls/mtproto/mtproto_client.py#L18
userge.__class__.__module__ = 'pyrogram.client'
call = PyTgCalls(userge, overload_quiet_mode=True)
call._env_checker.check_environment() # pylint: disable=protected-access


CHANNEL = userge.getCLogger(__name__)
Expand Down

0 comments on commit e904c05

Please sign in to comment.