From 970903c3c007ad25b2a63b32e483dc42fb232366 Mon Sep 17 00:00:00 2001 From: PeachyDelight Date: Sun, 19 Jun 2022 04:04:14 +0200 Subject: [PATCH] Reuse client and use client factory --- generators/textsynth.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/generators/textsynth.py b/generators/textsynth.py index 1db8114..7b82008 100644 --- a/generators/textsynth.py +++ b/generators/textsynth.py @@ -4,6 +4,8 @@ import sqlite3 from random import expovariate import typing import aiohttp +from utils import http_session_factory + async def make_sentence(cfg): # set default @@ -81,8 +83,8 @@ async def make_sentence(cfg): # using aiohttp post = None - while post is None: - async with aiohttp.ClientSession() as session: + async with http_session_factory() as session: + while post is None: async with session.post( "https://api.textsynth.com/v1/engines/{}/completions".format(cfg["textsynth_engine_id"]), headers={ @@ -103,14 +105,14 @@ async def make_sentence(cfg): continue post: str = data["text"] - # check wether the post only consists of mentions - # split by words - words = post.split() - # check if all words are mentions - if all(word.startswith("@") for word in words): - # generate a new sentence - post = None - continue + # check wether the post only consists of mentions + # split by words + words = post.split() + # check if all words are mentions + if all(word.startswith("@") for word in words): + # generate a new sentence + post = None + continue db.close()