Skip to content

Commit

Permalink
performance enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Aug 20, 2020
1 parent f0d8017 commit c62c70b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 70 deletions.
17 changes: 13 additions & 4 deletions userge/plugins/admin/gban.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
# All rights reserved

import asyncio
from typing import Union

import requests
import aiohttp
import spamwatch
from spamwatch.types import Ban
from pyrogram.errors.exceptions.bad_request_400 import (
ChatAdminRequired, UserAdminInvalid)

from userge import userge, Message, Config, get_collection, Filters
from userge import userge, Message, Config, get_collection, Filters, pool

SAVED_SETTINGS = get_collection("CONFIGS")
GBAN_USER_BASE = get_collection("GBAN_USER")
Expand Down Expand Up @@ -297,7 +299,9 @@ async def gban_at_entry(message: Message):
f"Banned in {message.chat.title}")
)
elif Config.ANTISPAM_SENTRY:
res = requests.get(f'https://api.cas.chat/check?user_id={user_id}').json()
async with aiohttp.ClientSession() as ses:
async with ses.get(f'https://api.cas.chat/check?user_id={user_id}') as resp:
res = await resp.json()
if res['ok']:
reason = ' | '.join(res['result']['messages']) if 'result' in res else None
await asyncio.gather(
Expand All @@ -318,7 +322,7 @@ async def gban_at_entry(message: Message):
f" Banned in {message.chat.title}\n\n$AUTOBAN #id{user_id}")
)
elif Config.SPAM_WATCH_API:
intruder = spamwatch.Client(Config.SPAM_WATCH_API).get_ban(user_id)
intruder = await _get_spamwatch_data(user_id)
if intruder:
await asyncio.gather(
message.client.kick_chat_member(chat_id, user_id),
Expand All @@ -339,3 +343,8 @@ async def gban_at_entry(message: Message):
f"$AUTOBAN #id{user_id}")
)
message.continue_propagation()


@pool.run_in_thread
def _get_spamwatch_data(user_id: int) -> Union[Ban, bool]:
return spamwatch.Client(Config.SPAM_WATCH_API).get_ban(user_id)
14 changes: 5 additions & 9 deletions userge/plugins/fun/kang.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import os
import random
import emoji
import urllib.request

import aiohttp
from PIL import Image
from pyrogram.api.functions.messages import GetStickerSet
from pyrogram.api.types import InputStickerSetShortName
from pyrogram.errors.exceptions.bad_request_400 import YouBlockedUser

from userge import userge, Message, Config, pool
from userge import userge, Message, Config


@userge.on_cmd(
Expand Down Expand Up @@ -92,13 +92,9 @@ async def kang_(message: Message):
packname += "_anim"
packnick += " (Animated)"
cmd = '/newanimated'

@pool.run_in_thread
def get_response():
response = urllib.request.urlopen( # nosec
urllib.request.Request(f'http://t.me/addstickers/{packname}'))
return response.read().decode("utf8").split('\n')
htmlstr = await get_response()
async with aiohttp.ClientSession() as ses:
async with ses.get(f'http://t.me/addstickers/{packname}') as res:
htmlstr = (await res.text()).split('\n')
if (" A <strong>Telegram</strong> user has created "
"the <strong>Sticker&nbsp;Set</strong>.") not in htmlstr:
async with userge.conversation('Stickers', limit=30) as conv:
Expand Down
2 changes: 1 addition & 1 deletion userge/plugins/tools/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def check_update(message: Message):
for i in repo.iter_commits(f'HEAD..{Config.UPSTREAM_REMOTE}/{branch}'):
out += (f"🔨 **#{i.count()}** : "
f"[{i.summary}]({Config.UPSTREAM_REPO.rstrip('/')}/commit/{i}) "
f"👷 __{i.committer}__\n\n")
f"👷 __{i.author}__\n\n")
except GitCommandError as error:
await message.err(error, del_in=5)
return
Expand Down
12 changes: 8 additions & 4 deletions userge/plugins/utils/covid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Author: Sumanjay (https://github.com/cyberboysumanjay) (@cyberboysumanjay)
# All rights reserved.

import requests
import aiohttp
from userge import userge, Message


Expand Down Expand Up @@ -45,7 +45,9 @@ def fill(nw_c, nw_d, ac_c, cr_c, t_c, t_d, t_r, name, rank):

if len(input_) == 0:
try:
data = requests.get("https://sjprojectsapi.herokuapp.com/covid/").json()
async with aiohttp.ClientSession() as ses:
async with ses.get("https://sjprojectsapi.herokuapp.com/covid/") as res:
data = await res.json()
global_data = data['results'][0]
nw_c = global_data['total_new_cases_today']
nw_d = global_data['total_new_deaths_today']
Expand All @@ -61,8 +63,10 @@ def fill(nw_c, nw_d, ac_c, cr_c, t_c, t_d, t_r, name, rank):
await message.edit("Covid API is currently down!\nPlease Try Again Later")
else:
try:
data = requests.get(
"https://sjprojectsapi.herokuapp.com/covid/?country=" + input_).json()
async with aiohttp.ClientSession() as ses:
async with ses.get(
"https://sjprojectsapi.herokuapp.com/covid/?country=" + input_) as res:
data = await res.json()
country_data = data['countrydata'][0]
nw_c = country_data['total_new_cases_today']
nw_d = country_data['total_new_deaths_today']
Expand Down
11 changes: 7 additions & 4 deletions userge/plugins/utils/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#
# All rights reserved.

from requests import get
from emoji import get_emoji_regexp

import aiohttp

from userge import userge, Message, Config

CHANNEL = userge.getCLogger(__name__)
Expand Down Expand Up @@ -41,9 +42,11 @@ async def cur_conv(message: Message):
return

if amount.isdigit():
data = get(
"https://free.currconv.com/api/v7/convert?"
f"apiKey={Config.CURRENCY_API}&q={currency_from}_{currency_to}&compact=ultra").json()
async with aiohttp.ClientSession() as ses:
async with ses.get("https://free.currconv.com/api/v7/convert?"
f"apiKey={Config.CURRENCY_API}&q="
f"{currency_from}_{currency_to}&compact=ultra") as res:
data = await res.json()
result = data[f'{currency_from}_{currency_to}']
result = float(amount) / float(result)
result = round(result, 5)
Expand Down
10 changes: 5 additions & 5 deletions userge/plugins/utils/dic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#
# All rights reserved.

import requests
import aiohttp

from userge import userge, Message

LOG = userge.getLogger(__name__) # logger object

CHANNEL = userge.getCLogger(__name__) # channel logger object


Expand Down Expand Up @@ -91,9 +91,9 @@ def out_print(word1):
await message.edit("`❌Plz enter word to search‼️`", del_in=5)
else:
word = input_
r_req = requests.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}")
r_dec = r_req.json()

async with aiohttp.ClientSession() as ses:
async with ses.get(f"https://api.dictionaryapi.dev/api/v1/entries/en/{word}") as res:
r_dec = await res.json()
v_word = input_
if isinstance(r_dec, list):
r_dec = r_dec[0]
Expand Down
80 changes: 41 additions & 39 deletions userge/plugins/utils/paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
# All rights reserved.

import os
from requests import get, post
from requests.exceptions import HTTPError, Timeout, TooManyRedirects

import aiohttp
from aiohttp import ClientResponseError, ServerTimeoutError, TooManyRedirects

from userge import userge, Message, Config

Expand Down Expand Up @@ -48,30 +49,31 @@ async def paste_(message: Message) -> None:
if flags and len(flags) == 1:
file_ext = '.' + flags[0]
await message.edit("`Pasting text ...`")
if use_neko:
resp = post(NEKOBIN_URL + "api/documents", json={"content": text})
if resp.status_code == 201:
response = resp.json()
key = response['result']['key']
final_url = NEKOBIN_URL + key + file_ext
reply_text = f"**Nekobin** [URL]({final_url})"
await message.edit(reply_text, disable_web_page_preview=True)
else:
await message.err("Failed to reach Nekobin")
else:
resp = post(DOGBIN_URL + "documents", data=text.encode('utf-8'))
if resp.status_code == 200:
response = resp.json()
key = response['key']
final_url = DOGBIN_URL + key
if response['isUrl']:
reply_text = (f"**Shortened** [URL]({final_url})\n"
f"**Dogbin** [URL]({DOGBIN_URL}v/{key})")
else:
reply_text = f"**Dogbin** [URL]({final_url}{file_ext})"
await message.edit(reply_text, disable_web_page_preview=True)
async with aiohttp.ClientSession() as ses:
if use_neko:
async with ses.post(NEKOBIN_URL + "api/documents", json={"content": text}) as resp:
if resp.status == 201:
response = await resp.json()
key = response['result']['key']
final_url = NEKOBIN_URL + key + file_ext
reply_text = f"**Nekobin** [URL]({final_url})"
await message.edit(reply_text, disable_web_page_preview=True)
else:
await message.err("Failed to reach Nekobin")
else:
await message.err("Failed to reach Dogbin")
async with ses.post(DOGBIN_URL + "documents", data=text.encode('utf-8')) as resp:
if resp.status == 200:
response = await resp.json()
key = response['key']
final_url = DOGBIN_URL + key
if response['isUrl']:
reply_text = (f"**Shortened** [URL]({final_url})\n"
f"**Dogbin** [URL]({DOGBIN_URL}v/{key})")
else:
reply_text = f"**Dogbin** [URL]({final_url}{file_ext})"
await message.edit(reply_text, disable_web_page_preview=True)
else:
await message.err("Failed to reach Dogbin")


@userge.on_cmd("getpaste", about={
Expand Down Expand Up @@ -103,17 +105,17 @@ async def get_paste_(message: Message):
else:
await message.err("Is that even a paste url?")
return
resp = get(raw_link)
try:
resp.raise_for_status()
except HTTPError:
await message.err(
f"Request returned an unsuccessful status code -> {HTTPError}")
except Timeout:
await message.err(f"Request timed out -> {Timeout}")
except TooManyRedirects:
await message.err(
f"Request exceeded the configured number of maximum redirections -> {TooManyRedirects}")
else:
await message.edit_or_send_as_file(
f"--Fetched dogbin URL content successfully!--\n\n**Content** :\n`{resp.text}`")
async with aiohttp.ClientSession(raise_for_status=True) as ses:
try:
async with ses.get(raw_link) as resp:
text = await resp.text()
except ServerTimeoutError as e_r:
await message.err(f"Request timed out -> {e_r}")
except TooManyRedirects as e_r:
await message.err("Request exceeded the configured "
f"number of maximum redirections -> {e_r}")
except ClientResponseError as e_r:
await message.err(f"Request returned an unsuccessful status code -> {e_r}")
else:
await message.edit_or_send_as_file("--Fetched Content Successfully!--"
f"\n\n**Content** :\n`{text}`")
11 changes: 7 additions & 4 deletions userge/plugins/utils/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import json
from datetime import datetime

import requests
import aiohttp
from pytz import country_timezones as c_tz, timezone as tz, country_names as c_n

from userge import userge, Message, Config
Expand Down Expand Up @@ -80,10 +80,13 @@ async def weather_get(message: Message):
CITY = newcity[0].strip() + "," + countrycode.strip()

url = f'https://api.openweathermap.org/data/2.5/weather?q={CITY}&appid={APPID}'
request = requests.get(url)
result = json.loads(request.text)
async with aiohttp.ClientSession() as ses:
async with ses.get(url) as res:
req_status = res.status
res_text = await res.text()
result = json.loads(res_text)

if request.status_code != 200:
if req_status != 200:
await message.edit(r"`Invalid country.. ¯\_(ツ)_/¯`", del_in=0)
return

Expand Down

0 comments on commit c62c70b

Please sign in to comment.