exts/callsign: simplify stringification, fix data validation

This commit is contained in:
classabbyamp 2023-01-01 16:30:14 -05:00 committed by classabbyamp
parent 1b0b244f99
commit 1650cd50dc
1 changed files with 21 additions and 15 deletions

View File

@ -12,6 +12,7 @@ from typing import Dict
import aiohttp import aiohttp
from callsignlookuptools import QrzAsyncClient, CallsignLookupError, CallsignData from callsignlookuptools import QrzAsyncClient, CallsignLookupError, CallsignData
from callsignlookuptools.common.dataclasses import Trustee
from discord.ext import commands from discord.ext import commands
@ -73,11 +74,11 @@ class QRZCog(commands.Cog):
embed.title = f"QRZ Data for {data.callsign}" embed.title = f"QRZ Data for {data.callsign}"
embed.colour = cmn.colours.good embed.colour = cmn.colours.good
embed.url = data.url embed.url = data.url
if data.image is not None: if data.image is not None and data.image.url is not None:
embed.set_thumbnail(url=data.image.url) embed.set_thumbnail(url=data.image.url)
for title, val in qrz_process_info(data).items(): for title, val in qrz_process_info(data).items():
if val: if val is not None and (val := str(val)):
embed.add_field(name=title, value=val, inline=True) embed.add_field(name=title, value=val, inline=True)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@ -93,30 +94,35 @@ def qrz_process_info(data: CallsignData) -> Dict:
else: else:
name = nm name = nm
else: else:
name = str(data.name) name = data.name
else: else:
name = None name = None
qsl = dict()
if data.qsl is not None:
qsl = {
"eQSL?": data.qsl.eqsl.name.title(),
"Paper QSL?": data.qsl.mail.name.title(),
"LotW?": data.qsl.lotw.name.title(),
"QSL Info": data.qsl.info,
}
return { return {
"Name": name, "Name": name,
"Country": data.address.country if data.address is not None else None, "Country": data.address.country if data.address is not None else None,
"Address": str(data.address), "Address": data.address,
"Grid Square": str(data.grid), "Grid Square": data.grid,
"County": data.county, "County": data.county,
"CQ Zone": str(data.cq_zone), "CQ Zone": data.cq_zone,
"ITU Zone": str(data.itu_zone), "ITU Zone": data.itu_zone,
"IOTA Designator": data.iota, "IOTA Designator": data.iota,
"Expires": f"{data.expire_date:%Y-%m-%d}" if data.expire_date is not None else None, "Expires": f"{data.expire_date:%Y-%m-%d}" if data.expire_date is not None else None,
"Aliases": ", ".join(data.aliases) if data.aliases else None, "Aliases": ", ".join(data.aliases) if data.aliases else None,
"Previous Callsign": data.prev_call, "Previous Callsign": data.prev_call,
"License Class": str(data.lic_class), "License Class": data.lic_class,
"Trustee": str(data.trustee), "Trustee": data.trustee if data.trustee is not None and data.trustee != Trustee(None, None) else None,
"eQSL?": data.qsl.eqsl.name.title() if data.qsl is not None else None, "Born": data.born,
"Paper QSL?": data.qsl.mail.name.title() if data.qsl is not None else None, } | qsl
"LotW?": data.qsl.lotw.name.title() if data.qsl is not None else None,
"QSL Info": str(data.qsl.info) if data.qsl is not None else None,
"Born": f"{data.born:%Y-%m-%d}" if data.born is not None else None
}
def setup(bot): def setup(bot):