mirror of
https://github.com/miaowware/qrm2.git
synced 2025-07-07 18:35:15 -04:00
fix wasteful session usage
This commit is contained in:
parent
5bc7011f7d
commit
93b0bae2c1
@ -20,6 +20,7 @@ class QRZCog(commands.Cog):
|
|||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.gs = bot.get_cog("GlobalSettings")
|
self.gs = bot.get_cog("GlobalSettings")
|
||||||
|
self.session = aiohttp.ClientSession()
|
||||||
self._qrz_session_init.start()
|
self._qrz_session_init.start()
|
||||||
|
|
||||||
@commands.command(name="qrz", aliases=["call"])
|
@commands.command(name="qrz", aliases=["call"])
|
||||||
@ -29,13 +30,12 @@ class QRZCog(commands.Cog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await qrz_test_session(self.key)
|
await qrz_test_session(self.key, self.session)
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
await self.get_session()
|
await self.get_session()
|
||||||
|
|
||||||
url = f'http://xmldata.qrz.com/xml/current/?s={self.key};callsign={call}'
|
url = f'http://xmldata.qrz.com/xml/current/?s={self.key};callsign={call}'
|
||||||
async with aiohttp.ClientSession() as session:
|
async with self.session.get(url) as resp:
|
||||||
async with session.get(url) as resp:
|
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
||||||
resp_xml = await resp.text()
|
resp_xml = await resp.text()
|
||||||
@ -67,7 +67,7 @@ class QRZCog(commands.Cog):
|
|||||||
|
|
||||||
async def get_session(self):
|
async def get_session(self):
|
||||||
"""Session creation and caching."""
|
"""Session creation and caching."""
|
||||||
self.key = await qrz_login(self.gs.keys.qrz_user, self.gs.keys.qrz_pass)
|
self.key = await qrz_login(self.gs.keys.qrz_user, self.gs.keys.qrz_pass, self.session)
|
||||||
with open('data/qrz_session', 'w') as qrz_file:
|
with open('data/qrz_session', 'w') as qrz_file:
|
||||||
qrz_file.write(self.key)
|
qrz_file.write(self.key)
|
||||||
|
|
||||||
@ -77,14 +77,13 @@ class QRZCog(commands.Cog):
|
|||||||
try:
|
try:
|
||||||
with open('data/qrz_session') as qrz_file:
|
with open('data/qrz_session') as qrz_file:
|
||||||
self.key = qrz_file.readline().strip()
|
self.key = qrz_file.readline().strip()
|
||||||
await qrz_test_session(self.key)
|
await qrz_test_session(self.key, self.session)
|
||||||
except (FileNotFoundError, ConnectionError):
|
except (FileNotFoundError, ConnectionError):
|
||||||
await self.get_session()
|
await self.get_session()
|
||||||
|
|
||||||
|
|
||||||
async def qrz_login(user: str, passwd: str):
|
async def qrz_login(user: str, passwd: str, session: aiohttp.ClientSession):
|
||||||
url = f'http://xmldata.qrz.com/xml/current/?username={user};password={passwd};agent=qrmbot'
|
url = f'http://xmldata.qrz.com/xml/current/?username={user};password={passwd};agent=qrmbot'
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.get(url) as resp:
|
async with session.get(url) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
||||||
@ -99,9 +98,8 @@ async def qrz_login(user: str, passwd: str):
|
|||||||
return resp_data['Key']
|
return resp_data['Key']
|
||||||
|
|
||||||
|
|
||||||
async def qrz_test_session(key: str):
|
async def qrz_test_session(key: str, session: aiohttp.ClientSession):
|
||||||
url = f'http://xmldata.qrz.com/xml/current/?s={key}'
|
url = f'http://xmldata.qrz.com/xml/current/?s={key}'
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.get(url) as resp:
|
async with session.get(url) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
raise ConnectionError(f'Unable to connect to QRZ (HTTP Error {resp.status})')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user