each cog now handles its own http session
This commit is contained in:
parent
10ac529323
commit
5331a5e2e7
7 changed files with 42 additions and 33 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import random
|
||||
import aiohttp
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
|
||||
|
|
@ -7,13 +8,17 @@ class Anime(commands.Cog):
|
|||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.url = "https://api.waifu.im/search"
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
async def get_waifu(self, tags):
|
||||
params = {
|
||||
'included_tags': tags,
|
||||
'height': '>=1000'
|
||||
}
|
||||
async with self.bot.http_session.get(self.url, params=params) as resp:
|
||||
async with self.http_session.get(self.url, params=params) as resp:
|
||||
resp_data = await resp.json()
|
||||
try:
|
||||
image = random.choice(resp_data['images'])
|
||||
|
|
@ -27,7 +32,7 @@ class Anime(commands.Cog):
|
|||
return "Something went wrong"
|
||||
|
||||
async def get_anime_from_img(self, img_url):
|
||||
async with self.bot.http_session.get(f"https://api.trace.moe/search?anilistInfo&url={img_url}") as resp:
|
||||
async with self.http_session.get(f"https://api.trace.moe/search?anilistInfo&url={img_url}") as resp:
|
||||
resp_data = await resp.json()
|
||||
title = resp_data["result"][0]["anilist"]["title"]
|
||||
video = resp_data["result"][0]["video"]
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ class Meme(commands.Cog):
|
|||
self.bot = bot
|
||||
self.working_dir = "tmp/meme/"
|
||||
self.folder_setup()
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
def folder_setup(self):
|
||||
try:
|
||||
|
|
@ -31,7 +35,7 @@ class Meme(commands.Cog):
|
|||
url = "https://api.openai.com/v1/chat/completions"
|
||||
|
||||
try:
|
||||
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
|
||||
async with self.http_session.post(url, headers=headers, json=data) as resp:
|
||||
response_data = await resp.json()
|
||||
response = response_data['choices'][0]['message']['content']
|
||||
return response
|
||||
|
|
@ -45,7 +49,7 @@ class Meme(commands.Cog):
|
|||
)
|
||||
async def meme(self, ctx):
|
||||
async def generate_random_meme(topic):
|
||||
async with self.bot.http_session.get('https://api.imgflip.com/get_memes') as resp:
|
||||
async with self.http_session.get('https://api.imgflip.com/get_memes') as resp:
|
||||
response_data = await resp.json()
|
||||
response = response_data['data']['memes']
|
||||
memepics = [{'name':image['name'],'url':image['url'],'id':image['id']} for image in response]
|
||||
|
|
@ -81,7 +85,7 @@ class Meme(commands.Cog):
|
|||
URL = 'https://api.imgflip.com/caption_image'
|
||||
|
||||
try:
|
||||
async with self.bot.http_session.post(URL, params=params) as resp:
|
||||
async with self.http_session.post(URL, params=params) as resp:
|
||||
response = await resp.json()
|
||||
image_link = response['data']['url']
|
||||
except Exception as error:
|
||||
|
|
@ -89,7 +93,7 @@ class Meme(commands.Cog):
|
|||
try:
|
||||
#------------------------------------Saving Image Using Aiohttp---------------------------------#
|
||||
filename = memepics[id-1]['name']
|
||||
async with self.bot.http_session.get(image_link) as response:
|
||||
async with self.http_session.get(image_link) as response:
|
||||
folder = "tmp/meme/"
|
||||
filename = folder + topic + str(len(os.listdir(folder))) + ".jpg"
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import io
|
|||
import base64
|
||||
import time
|
||||
import html
|
||||
import aiohttp
|
||||
import asyncssh
|
||||
from PIL import Image, PngImagePlugin
|
||||
from discord.ext import commands, tasks
|
||||
|
|
@ -20,6 +21,10 @@ class PhixxyCom(commands.Cog):
|
|||
self.stable_diffusion_log = "data/stable_diffusion/stable_diffusion.log"
|
||||
self.phixxy_loop.start()
|
||||
self.blog_loop.start()
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
def folder_setup(self):
|
||||
try:
|
||||
|
|
@ -94,7 +99,7 @@ class PhixxyCom(commands.Cog):
|
|||
for image in image_list:
|
||||
filename = image.replace(" ", "").lower() + ".png"
|
||||
payload = {"prompt": image, "steps": 25}
|
||||
response = await self.bot.http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
||||
response = await self.http_session.post(url=f'{url}/sdapi/v1/txt2img', json=payload)
|
||||
r = await response.json()
|
||||
for i in r['images']:
|
||||
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import aiohttp
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
|
||||
|
|
@ -5,9 +6,13 @@ class Pokedex(commands.Cog):
|
|||
|
||||
def __init__(self, bot) -> None:
|
||||
self.bot = bot
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
async def get_json(self, url):
|
||||
async with self.bot.http_session.get(url) as resp:
|
||||
async with self.http_session.get(url) as resp:
|
||||
json_data = await resp.json()
|
||||
return json_data
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
#plugin file for sparkytron 3000
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
import random
|
||||
import os
|
||||
import json
|
||||
import math
|
||||
import time
|
||||
import aiohttp
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
|
||||
|
||||
class PokemonGame(commands.Cog):
|
||||
|
|
@ -15,9 +15,13 @@ class PokemonGame(commands.Cog):
|
|||
self.working_dir = "tmp/pokemon/"
|
||||
self.data_dir = "data/pokemon/"
|
||||
self.folder_setup()
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
async def get_json(self, url):
|
||||
async with self.bot.http_session.get(url) as resp:
|
||||
async with self.http_session.get(url) as resp:
|
||||
json_data = await resp.json()
|
||||
return json_data
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import time
|
||||
import os
|
||||
import aiohttp
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
|
|
@ -10,6 +11,10 @@ class TextToSpeech(commands.Cog):
|
|||
self.working_dir = "tmp/tts/"
|
||||
self.data_dir = "data/tts/"
|
||||
self.folder_setup()
|
||||
self.http_session = self.create_aiohttp_session()
|
||||
|
||||
def create_aiohttp_session(self):
|
||||
return aiohttp.ClientSession()
|
||||
|
||||
def folder_setup(self):
|
||||
try:
|
||||
|
|
@ -40,7 +45,7 @@ class TextToSpeech(commands.Cog):
|
|||
}
|
||||
filename = f"{time.time_ns()}.mp3"
|
||||
filepath = f"{self.data_dir}{filename}"
|
||||
response = await self.bot.http_session.post(url, json=data, headers=headers)
|
||||
response = await self.http_session.post(url, json=data, headers=headers)
|
||||
with open(filepath, 'wb') as f:
|
||||
async for chunk in response.content.iter_chunked(CHUNK_SIZE):
|
||||
if chunk:
|
||||
|
|
@ -63,7 +68,7 @@ class TextToSpeech(commands.Cog):
|
|||
}
|
||||
filename = f"{time.time_ns()}.mp3"
|
||||
filepath = f"{self.data_dir}{filename}"
|
||||
response = await self.bot.http_session.post(url, json=data, headers=headers)
|
||||
response = await self.http_session.post(url, json=data, headers=headers)
|
||||
with open(filepath, 'wb') as f:
|
||||
async for chunk in response.content.iter_chunked(CHUNK_SIZE):
|
||||
if chunk:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue