switch from request to aiohttp

This commit is contained in:
Philipp 2024-04-07 22:31:48 +02:00
parent 3c0b9664ff
commit 5473df0e5d
Signed by: Spaenny
GPG Key ID: 9EBD8439AFBAB750
2 changed files with 24 additions and 25 deletions

View File

@ -1,10 +1,9 @@
import json
import urllib
import requests
import io
import base64
import asyncio
import requests
import aiohttp
import re
from PIL import Image
@ -107,29 +106,29 @@ class Dalle(Plugin):
"prompt": prompt
}
response = requests.post("https://nexra.aryahcr.cc/api/image/complements", headers=headers, data=json.dumps(data))
async with aiohttp.request("POST", "https://nexra.aryahcr.cc/api/image/complements", headers=headers, data=json.dumps(data)) as response:
if response.status_code != 200:
self.log.warning(f"Unexpected status sending request to nexra.aryahcr.cc: {response.status_code}")
return
if response.status != 200:
self.log.warning(f"Unexpected status sending request to nexra.aryahcr.cc: {response.status_code}")
return
count = -1
response_text = await response.text()
for i in range(len(response_text)):
if count <= -1:
if response_text[i] == "{":
count = i
else:
break
count = -1
for i in range(len(response.text)):
if count <= -1:
if response.text[i] == "{":
count = i
else:
break
response_json = json.loads(response.text[count:])
response_json = json.loads(response_text[count:])
content = response_json['images'][0]
content = response_json['images'][0]
base64_str = content.replace("data:image/jpeg;base64,", "")
mem_file = io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8")))
pil_image = Image.open(mem_file)
pil_image.save(mem_file, format=pil_image.format)
mem_file.seek(0)
return mem_file
base64_str = content.replace("data:image/jpeg;base64,", "")
mem_file = io.BytesIO(base64.decodebytes(bytes(base64_str, "utf-8")))
pil_image = Image.open(mem_file)
pil_image.save(mem_file, format=pil_image.format)
mem_file.seek(0)
return mem_file

View File

@ -1,6 +1,6 @@
maubot: 0.1.0
id: sh.boehm.dalle
version: 0.0.029
version: 0.0.03
license: MIT
modules:
- dalle