Skip to content
This repository has been archived by the owner on May 15, 2021. It is now read-only.

Commit

Permalink
πŸ”₯ iYTDL v2.5 (#134)
Browse files Browse the repository at this point in the history
# Improvements
* added regex for faster match
* support for bot mode
* code cleanup
* Faster (switched to pyrogram cuz now it DC id bug fixed)
* Video will now output as mp4 (streamable on TG)
* Improved thumb for videos
* added [Best Audio Option] 😜 320 kbps

# Upstreams

# fixes
* kang.py
* deepsource fixes
  • Loading branch information
code-rgb authored Feb 8, 2021
1 parent e912686 commit b8781c8
Show file tree
Hide file tree
Showing 20 changed files with 398 additions and 920 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ beautifulsoup4>=4.9.1
covid>=2.4.0
cowpy>=1.1.0
dnspython>=2.0.0
emoji>=0.6.0
gitpython>=3.1.11
google-api-python-client>=1.12.8
google-auth-httplib2>=0.0.4
Expand Down
117 changes: 69 additions & 48 deletions userge/core/ext/raw_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
__all__ = ['RawClient']

import asyncio
import time
from typing import Optional, Dict
from math import floor
from typing import Optional, Dict, List
from time import time, perf_counter, sleep

import pyrogram.raw.functions as funcs
import pyrogram.raw.types as types
Expand All @@ -29,11 +30,8 @@
class RawClient(Client):
""" userge raw client """
DUAL_MODE = False
LAST_OUTGOING_TIME = time.time()

LAST_OUTGOING_TIME = time()
REQ_LOGS: Dict[int, 'ChatReq'] = {}
DELAY_BET_MSG_REQ = 1
MSG_REQ_PER_MIN = 20
REQ_LOCK = asyncio.Lock()

def __init__(self, bot: Optional['userge.core.client.UsergeBot'] = None, **kwargs) -> None:
Expand All @@ -46,69 +44,92 @@ async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES,
timeout: float = Session.WAIT_TIMEOUT, sleep_threshold: float = None):
key = 0
if isinstance(data, (funcs.messages.SendMessage,
funcs.messages.SendMedia,
funcs.messages.SendMultiMedia,
funcs.messages.EditMessage,
funcs.messages.ForwardMessages)):
if isinstance(data, funcs.messages.ForwardMessages):
tmp = data.to_peer
else:
tmp = data.peer
if isinstance(tmp, (types.InputPeerChannel, types.InputPeerChannelFromMessage)):
key = int(tmp.channel_id)
elif isinstance(tmp, types.InputPeerChat):
key = int(tmp.chat_id)
elif isinstance(tmp, (types.InputPeerUser, types.InputPeerUserFromMessage)):
key = int(tmp.user_id)
if isinstance(data, funcs.messages.SendMedia) and isinstance(
data.media, (types.InputMediaUploadedDocument,
types.InputMediaUploadedPhoto)):
tmp = None
if tmp:
if isinstance(tmp, (types.InputPeerChannel, types.InputPeerChannelFromMessage)):
key = int(tmp.channel_id)
elif isinstance(tmp, types.InputPeerChat):
key = int(tmp.chat_id)
elif isinstance(tmp, (types.InputPeerUser, types.InputPeerUserFromMessage)):
key = int(tmp.user_id)
elif isinstance(data, funcs.channels.DeleteMessages):
if isinstance(data.channel, (types.InputChannel, types.InputChannelFromMessage)):
key = int(data.channel.channel_id)
if key:
async def slp(to_sl: float) -> None:
if to_sl > 0.1:
if to_sl > 1:
_LOG.info(_LOG_STR, to_sl, key)
else:
_LOG.debug(_LOG_STR, to_sl, key)
await asyncio.sleep(to_sl)
async with self.REQ_LOCK:
if key in self.REQ_LOGS:
chat_req = self.REQ_LOGS[key]
else:
chat_req = self.REQ_LOGS[key] = ChatReq()
diff = chat_req.small_diff
if 0 < diff < self.DELAY_BET_MSG_REQ:
await slp(1 - diff)
diff = chat_req.big_diff
if diff >= 60:
chat_req.reset()
elif chat_req.count > self.MSG_REQ_PER_MIN:
await slp(60 - diff)
chat_req.reset()
else:
chat_req.update()
try:
req = self.REQ_LOGS[key]
except KeyError:
req = self.REQ_LOGS[key] = ChatReq()
async with req.lock:
now = perf_counter()
req.update(now - 60)
if req.has:
to_sl = 0.0
diff = now - req.last
if 0 < diff < 1:
to_sl = 1 - diff
diff = now - req.first
if req.count > 18:
to_sl = max(to_sl, 60 - diff)
if to_sl > 0:
if to_sl > 1:
_LOG.info(_LOG_STR, to_sl, key)
else:
_LOG.debug(_LOG_STR, to_sl, key)
await asyncio.sleep(to_sl)
now += to_sl
count = 0
counter = floor(now - 1)
for r in self.REQ_LOGS.values():
if r.has and r.last > counter:
count += 1
if count > 25:
_LOG.info(_LOG_STR, 1, key)
sleep(1)
now += 1
req.add(now)
return await super().send(data, retries, timeout, sleep_threshold)


class ChatReq:
def __init__(self) -> None:
self._first = self._last = time.time()
self._count = 0
self._lock = asyncio.Lock()
self._logs: List[float] = []

@property
def lock(self):
return self._lock

@property
def has(self) -> bool:
return len(self._logs) != 0

@property
def small_diff(self) -> float:
return time.time() - self._last
def first(self) -> float:
return self._logs[0]

@property
def big_diff(self) -> float:
return time.time() - self._first
def last(self) -> Optional[float]:
return self._logs[-1]

@property
def count(self) -> float:
return self._count
def count(self) -> int:
return len(self._logs)

def reset(self) -> None:
self._first = self._last = time.time()
self._count = 1
def add(self, log: float) -> None:
self._logs.append(log)

def update(self) -> None:
self._last = time.time()
self._count += 1
def update(self, t: float) -> None:
self._logs = [i for i in self._logs if i > t]
4 changes: 2 additions & 2 deletions userge/plugins/admin/gadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import time

from emoji import get_emoji_regexp
from pyrogram.errors import (
FloodWait,
PeerIdInvalid,
Expand All @@ -15,6 +14,7 @@
from pyrogram.types import ChatPermissions

from userge import Message, userge
from userge.utils.functions import get_emoji_regex

CHANNEL = userge.getCLogger(__name__)

Expand Down Expand Up @@ -47,7 +47,7 @@ async def promote_usr(message: Message):
)
return
if custom_rank:
custom_rank = get_emoji_regexp().sub("", custom_rank)
custom_rank = get_emoji_regex().sub("", custom_rank)
if len(custom_rank) > 15:
custom_rank = custom_rank[:15]
try:
Expand Down
30 changes: 11 additions & 19 deletions userge/plugins/bot/opinion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# All rights reserved.


import asyncio
import os

import ujson
from pyrogram import filters
from pyrogram.errors import BadRequest, FloodWait
from pyrogram.types import CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup

from userge import Config, Message, userge
from userge.utils import xbot

if not os.path.exists("userge/xcache"):
os.mkdir("userge/xcache")
Expand Down Expand Up @@ -84,29 +85,23 @@ async def choice_cb(_, c_q: CallbackQuery):
data[str(opinion_id)] = view_data
with open(PATH, "w") as outfile:
ujson.dump(data, outfile)

agree_data += f" {view_data[1]['agree']}"
disagree_data += f" {view_data[1]['disagree']}"

opinion_data = [
[
InlineKeyboardButton(agree_data, callback_data=f"op_y_{opinion_id}"),
InlineKeyboardButton(disagree_data, callback_data=f"op_n_{opinion_id}"),
],
[InlineKeyboardButton("πŸ“Š Stats", callback_data=f"opresult_{opinion_id}")],
]

await xbot.edit_inline_reply_markup(
c_q.inline_message_id, reply_markup=InlineKeyboardMarkup(opinion_data)
)

# await userge.bot.edit_inline_reply_markup(
# c_q.inline_message_id, reply_markup=InlineKeyboardMarkup(opinion_data)
# )
# except FloodWait as e:
# await asyncio.sleep(e.x)
# except BadRequest:
# return
try:
await c_q.edit_message_reply_markup(
reply_markup=InlineKeyboardMarkup(opinion_data)
)
except FloodWait as e:
await asyncio.sleep(e.x)
except BadRequest:
return

@userge.bot.on_callback_query(filters.regex(pattern=r"^opresult_(\d+)$"))
async def choice_result_cb(_, c_q: CallbackQuery):
Expand All @@ -124,10 +119,7 @@ async def choice_result_cb(_, c_q: CallbackQuery):
msg += f"β€’ πŸ‘€ `{total} People voted`\n\n"
msg += f"β€’ πŸ‘ `{agreed}% People Agreed`\n\n"
msg += f"β€’ πŸ‘Ž `{disagreed}% People Disagreed`\n\n"

await xbot.edit_inline_text(c_q.inline_message_id, msg)

# await userge.bot.edit_inline_text(c_q.inline_message_id, msg)
await c_q.edit_message_text(msg)
else:
a = await userge.get_me()
if a.username:
Expand Down
Loading

0 comments on commit b8781c8

Please sign in to comment.