mirror of
https://github.com/Nick80835/microbot
synced 2025-08-31 14:38:04 +00:00
implement a unified aiohttp client
This commit is contained in:
@@ -5,6 +5,7 @@ from importlib import import_module, reload
|
||||
from os.path import basename, dirname, isfile
|
||||
from re import escape
|
||||
|
||||
from aiohttp import ClientSession
|
||||
from telethon import events
|
||||
from telethon.tl.types import DocumentAttributeFilename
|
||||
|
||||
@@ -20,6 +21,7 @@ class Loader():
|
||||
self.settings = settings
|
||||
self.command_handler = CommandHandler(client, logger, settings)
|
||||
self.help_dict = {}
|
||||
self.aioclient = ClientSession()
|
||||
self.botversion = "0.1.3"
|
||||
|
||||
def load_all_modules(self):
|
||||
|
@@ -74,8 +74,13 @@ class MicroBot():
|
||||
else:
|
||||
self.logger.info("Stopping client.")
|
||||
|
||||
await self.loader.aioclient.close()
|
||||
await self.client.disconnect()
|
||||
|
||||
|
||||
micro_bot = MicroBot()
|
||||
micro_bot.start_microbot()
|
||||
|
||||
try:
|
||||
micro_bot.start_microbot()
|
||||
except KeyboardInterrupt:
|
||||
micro_bot.client.loop.run_until_complete(micro_bot.stop_client())
|
||||
|
@@ -2,8 +2,6 @@
|
||||
|
||||
from random import choice
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -18,29 +16,23 @@ VALID_ENDS = (".mp4", ".jpg", ".jpeg", ".png", ".gif")
|
||||
async def fourchan(event):
|
||||
as_file = bool(event.pattern_match.group(1))
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(BOARD_URL.format(event.args)) as response:
|
||||
async with ldr.aioclient.get(BOARD_URL.format(event.args)) as response:
|
||||
if response.status == 200:
|
||||
board_response = await response.json()
|
||||
op_id = choice(choice(board_response)["threads"])["no"]
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
async with session.get(POST_URL.format(event.args, op_id)) as response:
|
||||
async with ldr.aioclient.get(POST_URL.format(event.args, op_id)) as response:
|
||||
if response.status == 200:
|
||||
post_response = await response.json()
|
||||
post_info = choice([[i["tim"], i["ext"], i["com"] if "com" in i else None] for i in post_response["posts"] if "tim" in i and i["ext"] in VALID_ENDS])
|
||||
post_file_url = CONTENT_URL.format(event.args, post_info[0], post_info[1])
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
await session.close()
|
||||
|
||||
if not response:
|
||||
await event.reply(f"`No results for board: `**{event.args}**")
|
||||
return
|
||||
|
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -19,54 +17,42 @@ GIFPARAM = {"mime_types": "gif"}
|
||||
|
||||
|
||||
async def neko_atsume(params):
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(CAT_URL, params=params, headers=CAT_HEADERS) as response:
|
||||
async with ldr.aioclient.get(CAT_URL, params=params, headers=CAT_HEADERS) as response:
|
||||
if response.status == 200:
|
||||
neko = await response.json()
|
||||
else:
|
||||
neko = response.status
|
||||
|
||||
await session.close()
|
||||
return neko
|
||||
|
||||
|
||||
async def inu_atsume(params):
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(DOG_URL, params=params, headers=DOG_HEADERS) as response:
|
||||
async with ldr.aioclient.get(DOG_URL, params=params, headers=DOG_HEADERS) as response:
|
||||
if response.status == 200:
|
||||
inu = await response.json()
|
||||
else:
|
||||
inu = response.status
|
||||
|
||||
await session.close()
|
||||
return inu
|
||||
|
||||
|
||||
async def shibe_inu_atsume():
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(SHIBE_URL, params=None, headers=None) as response:
|
||||
async with ldr.aioclient.get(SHIBE_URL, params=None, headers=None) as response:
|
||||
if response.status == 200:
|
||||
shibe_inu = await response.json()
|
||||
else:
|
||||
shibe_inu = response.status
|
||||
|
||||
await session.close()
|
||||
return shibe_inu
|
||||
|
||||
|
||||
async def tori_atsume():
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(BIRD_URL, params=None, headers=None) as response:
|
||||
async with ldr.aioclient.get(BIRD_URL, params=None, headers=None) as response:
|
||||
if response.status == 200:
|
||||
tori = await response.json()
|
||||
else:
|
||||
tori = response.status
|
||||
|
||||
await session.close()
|
||||
return tori
|
||||
|
||||
|
||||
|
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from requests import get
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -10,9 +8,9 @@ ldr = micro_bot.loader
|
||||
@ldr.add("corona")
|
||||
async def corona(event):
|
||||
if event.args:
|
||||
with get(f"https://corona.lmao.ninja/v2/countries/{event.args}") as response:
|
||||
if response.status_code == 200:
|
||||
response = response.json()
|
||||
async with ldr.aioclient.get(f"https://corona.lmao.ninja/v2/countries/{event.args}") as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status_code}**")
|
||||
return
|
||||
@@ -20,6 +18,12 @@ async def corona(event):
|
||||
response_text = f"`Stats for `**{response['country']}**\n\n`Cases: `**{response['cases']}** **({response['todayCases']} today)**\n`Deaths: `**{response['deaths']}** **({response['todayDeaths']} today)**\n`Recoveries: `**{response['recovered']}**"
|
||||
await event.reply(response_text)
|
||||
else:
|
||||
response = get("https://corona.lmao.ninja/v2/all").json()
|
||||
async with ldr.aioclient.get(f"https://corona.lmao.ninja/v2/all") as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status_code}**")
|
||||
return
|
||||
|
||||
response_text = f"`Global stats`\n\n`Cases: `**{response['cases']}** **({response['todayCases']} today)**\n`Deaths: `**{response['deaths']}** **({response['todayDeaths']} today)**\n`Recoveries: `**{response['recovered']}**"
|
||||
await event.reply(response_text)
|
||||
|
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -26,15 +24,11 @@ async def danbooru(event):
|
||||
"random": "true",
|
||||
"tags": f"{rating} {event.args}".strip().replace(" ", " ")}
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(DAN_URL, params=params) as response:
|
||||
async with ldr.aioclient.get(DAN_URL, params=params) as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
await session.close()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
if not response:
|
||||
|
@@ -6,7 +6,6 @@ from re import sub
|
||||
|
||||
from gtts import gTTS
|
||||
from PIL import Image
|
||||
from requests import get
|
||||
from speedtest import Speedtest
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
@@ -79,7 +78,13 @@ async def ip_lookup(event):
|
||||
await event.reply("`Provide an IP!`")
|
||||
return
|
||||
|
||||
lookup_json = get(f"http://ip-api.com/json/{ip}").json()
|
||||
async with ldr.aioclient.get(f"http://ip-api.com/json/{ip}") as response:
|
||||
if response.status == 200:
|
||||
lookup_json = await response.json()
|
||||
else:
|
||||
await event.reply(f"`An error occurred when looking for `**{ip}**`: `**{response.status}**")
|
||||
return
|
||||
|
||||
fixed_lookup = {}
|
||||
|
||||
for key, value in lookup_json.items():
|
||||
|
@@ -2,8 +2,6 @@
|
||||
|
||||
from random import choice
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -30,15 +28,11 @@ async def gelbooru(event):
|
||||
"json": 1,
|
||||
"tags": f"{rating} {event.args} sort:random".strip().replace(" ", " ")}
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(GEL_URL, params=params) as response:
|
||||
async with ldr.aioclient.get(GEL_URL, params=params) as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
await session.close()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
if not response:
|
||||
|
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -21,15 +19,11 @@ async def supernekoatsume(event):
|
||||
else:
|
||||
reply_to = event
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(NEKO_URL + nekotype) as response:
|
||||
async with ldr.aioclient.get(NEKO_URL + nekotype) as response:
|
||||
if response.status == 200:
|
||||
image_url = (await response.json())["url"]
|
||||
await session.close()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
try:
|
||||
|
@@ -1,7 +1,5 @@
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -26,15 +24,11 @@ async def sankaku(event):
|
||||
"limit": 5,
|
||||
"tags": f"order:random {rating} {event.args}".strip().replace(" ", " ")}
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(SAN_URL, params=params) as response:
|
||||
async with ldr.aioclient.get(SAN_URL, params=params) as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
await session.close()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code: `**{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
if not response:
|
||||
|
@@ -2,8 +2,6 @@
|
||||
|
||||
import io
|
||||
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from ubot.micro_bot import micro_bot
|
||||
|
||||
ldr = micro_bot.loader
|
||||
@@ -23,18 +21,13 @@ async def urban_dict(event):
|
||||
params = None
|
||||
url = UD_RANDOM_URL
|
||||
|
||||
session = ClientSession()
|
||||
|
||||
async with session.get(url, params=params) as response:
|
||||
async with ldr.aioclient.get(url, params=params) as response:
|
||||
if response.status == 200:
|
||||
response = await response.json()
|
||||
else:
|
||||
await event.reply(f"`An error occurred, response code:` **{response.status}**")
|
||||
await session.close()
|
||||
return
|
||||
|
||||
await session.close()
|
||||
|
||||
if response['list']:
|
||||
response_word = response['list'][0]
|
||||
else:
|
||||
|
Reference in New Issue
Block a user