WIP: aiohttp migration

This commit is contained in:
2023-03-02 16:39:59 +01:00
parent 2387823151
commit a913ba19c6
4 changed files with 44 additions and 36 deletions

View File

@@ -6,9 +6,8 @@ from uuid import uuid4
from PIL import Image
from classes.poster_client import PosterClient
from requests import get
from modules.api_client import authorize, move_pic, random_pic
from modules.api_client import authorize, move_pic, random_pic, http_session
from modules.database import col_sent, col_submitted
from modules.logger import logWrite
from modules.utils import configGet, locale
@@ -36,12 +35,12 @@ async def send_content(app: PosterClient) -> None:
await app.send_message(app.owner, locale("api_queue_error", "message", locale=configGet("locale")))
return
response = get(f'{configGet("address", "posting", "api")}/photos/{pic[0]}', headers={"Authorization": f"Bearer {token}"}, stream=True)
response = await http_session.get(f'{configGet("address", "posting", "api")}/photos/{pic[0]}', headers={"Authorization": f"Bearer {token}"}, stream=True)
if response.status_code != 200:
logWrite(locale("post_invalid_pic", "console", locale=configGet("locale")).format(response.status_code, str(response.json())))
if response.status != 200:
logWrite(locale("post_invalid_pic", "console", locale=configGet("locale")).format(response.status, str(response.json())))
if configGet("error", "reports"):
await app.send_message(app.owner, locale("post_invalid_pic", "message", locale=configGet("locale")).format(response.status_code, response.json()))
await app.send_message(app.owner, locale("post_invalid_pic", "message", locale=configGet("locale")).format(response.status, response.json()))
tmp_dir = str(uuid4())
@@ -50,7 +49,7 @@ async def send_content(app: PosterClient) -> None:
tmp_path = path.join(tmp_dir, pic[1])
with open(path.join(configGet("tmp", "locations"), tmp_path), 'wb') as out_file:
copyfileobj(response.raw, out_file)
out_file.write(await response.read())
logWrite(f'Candidate {pic[1]} ({pic[0]}) is {path.getsize(path.join(configGet("tmp", "locations"), tmp_path))} bytes big', debug=True)