2019-10-15 17:37:13 -04:00
|
|
|
"""
|
2021-03-28 09:50:51 -04:00
|
|
|
Callsign Lookup extension for qrm
|
2019-10-15 17:37:13 -04:00
|
|
|
---
|
2021-03-28 13:57:03 -04:00
|
|
|
Copyright (C) 2019-2020 classabbyamp, 0x5c (as qrz.py)
|
|
|
|
Copyright (C) 2021 classabbyamp, 0x5c
|
2019-10-15 17:37:13 -04:00
|
|
|
|
2021-06-26 20:23:55 -04:00
|
|
|
SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
2019-10-15 17:37:13 -04:00
|
|
|
"""
|
2020-01-31 06:50:50 -05:00
|
|
|
|
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
from typing import Dict
|
2019-10-15 17:37:13 -04:00
|
|
|
|
|
|
|
import aiohttp
|
2023-01-01 04:10:45 -05:00
|
|
|
from callsignlookuptools import QrzAsyncClient, CallsignLookupError, CallsignData
|
2023-01-01 16:30:14 -05:00
|
|
|
from callsignlookuptools.common.dataclasses import Trustee
|
2019-10-15 17:37:13 -04:00
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
from discord.ext import commands
|
2020-02-05 07:09:08 -05:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
2020-02-05 07:09:08 -05:00
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
import data.options as opt
|
2019-12-07 17:13:06 -05:00
|
|
|
import data.keys as keys
|
2019-12-06 01:19:42 -05:00
|
|
|
|
2019-10-15 17:37:13 -04:00
|
|
|
|
|
|
|
class QRZCog(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
2021-03-16 22:04:05 -04:00
|
|
|
self.qrz = None
|
|
|
|
try:
|
|
|
|
if keys.qrz_user and keys.qrz_pass:
|
|
|
|
# seed the qrz object with the previous session key, in case it already works
|
2023-01-01 04:10:45 -05:00
|
|
|
session_key = ""
|
2021-03-16 22:04:05 -04:00
|
|
|
try:
|
|
|
|
with open("data/qrz_session") as qrz_file:
|
2023-01-01 04:10:45 -05:00
|
|
|
session_key = qrz_file.readline().strip()
|
2021-03-16 22:04:05 -04:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2023-01-01 04:10:45 -05:00
|
|
|
self.qrz = QrzAsyncClient(username=keys.qrz_user, password=keys.qrz_pass, useragent="discord-qrm2",
|
|
|
|
session_key=session_key,
|
|
|
|
session=aiohttp.ClientSession(connector=bot.qrm.connector))
|
2021-03-16 22:04:05 -04:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2019-10-15 17:37:13 -04:00
|
|
|
|
2021-03-28 02:38:36 -04:00
|
|
|
@commands.command(name="call", aliases=["qrz"], category=cmn.Cats.LOOKUP)
|
2020-01-04 14:40:08 -05:00
|
|
|
async def _qrz_lookup(self, ctx: commands.Context, callsign: str, *flags):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Looks up a callsign on [QRZ.com](https://www.qrz.com/). Add `--link` to only link the QRZ page."""
|
2020-01-04 14:40:08 -05:00
|
|
|
flags = [f.lower() for f in flags]
|
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
if self.qrz is None or "--link" in flags:
|
|
|
|
if ctx.invoked_with == "qrz":
|
|
|
|
await ctx.send("⚠️ **Deprecated Command Alias**\n"
|
|
|
|
f"This command has been renamed to `{ctx.prefix}call`!\n"
|
|
|
|
"This alias will be removed in the next version.")
|
2020-01-30 06:15:42 -05:00
|
|
|
await ctx.send(f"http://qrz.com/db/{callsign}")
|
2019-10-15 17:37:13 -04:00
|
|
|
return
|
2019-10-16 17:23:56 -04:00
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = f"QRZ Data for {callsign.upper()}"
|
|
|
|
|
|
|
|
if ctx.invoked_with == "qrz":
|
|
|
|
embed.description = ("⚠️ **Deprecated Command Alias**\n"
|
|
|
|
f"This command has been renamed to `{ctx.prefix}call`!\n"
|
|
|
|
"This alias will be removed in the next version.")
|
|
|
|
|
2020-02-25 20:36:21 -05:00
|
|
|
async with ctx.typing():
|
|
|
|
try:
|
2023-01-01 04:10:45 -05:00
|
|
|
data = await self.qrz.search(callsign)
|
|
|
|
except CallsignLookupError as e:
|
2021-03-16 22:04:05 -04:00
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
embed.description = str(e)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
|
2023-01-01 04:10:45 -05:00
|
|
|
embed.title = f"QRZ Data for {data.callsign}"
|
2020-02-25 20:36:21 -05:00
|
|
|
embed.colour = cmn.colours.good
|
2021-03-16 22:04:05 -04:00
|
|
|
embed.url = data.url
|
2023-01-01 16:30:14 -05:00
|
|
|
if data.image is not None and data.image.url is not None:
|
2021-03-16 22:04:05 -04:00
|
|
|
embed.set_thumbnail(url=data.image.url)
|
2020-02-25 20:36:21 -05:00
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
for title, val in qrz_process_info(data).items():
|
2023-01-01 16:30:14 -05:00
|
|
|
if val is not None and (val := str(val)):
|
2020-02-25 20:36:21 -05:00
|
|
|
embed.add_field(name=title, value=val, inline=True)
|
|
|
|
await ctx.send(embed=embed)
|
2019-10-15 17:37:13 -04:00
|
|
|
|
2019-10-18 11:26:06 -04:00
|
|
|
|
2023-01-01 04:10:45 -05:00
|
|
|
def qrz_process_info(data: CallsignData) -> Dict:
|
|
|
|
if data.name is not None:
|
2021-03-16 22:04:05 -04:00
|
|
|
if opt.qrz_only_nickname:
|
2023-01-01 04:10:45 -05:00
|
|
|
nm = data.name.name if data.name.name is not None else ""
|
|
|
|
if data.name.nickname is not None:
|
|
|
|
name = data.name.nickname + " " + nm
|
2021-03-16 22:04:05 -04:00
|
|
|
elif data.name.first:
|
2023-01-01 04:10:45 -05:00
|
|
|
name = data.name.first + " " + nm
|
2021-03-16 22:04:05 -04:00
|
|
|
else:
|
2023-01-01 04:10:45 -05:00
|
|
|
name = nm
|
2019-10-16 17:23:56 -04:00
|
|
|
else:
|
2023-01-01 16:30:14 -05:00
|
|
|
name = data.name
|
2019-10-16 17:23:56 -04:00
|
|
|
else:
|
|
|
|
name = None
|
2021-03-16 22:04:05 -04:00
|
|
|
|
2023-01-01 16:30:14 -05:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-03-16 22:04:05 -04:00
|
|
|
return {
|
|
|
|
"Name": name,
|
2023-01-01 04:10:45 -05:00
|
|
|
"Country": data.address.country if data.address is not None else None,
|
2023-01-01 16:30:14 -05:00
|
|
|
"Address": data.address,
|
|
|
|
"Grid Square": data.grid,
|
2023-01-01 04:10:45 -05:00
|
|
|
"County": data.county,
|
2023-01-01 16:30:14 -05:00
|
|
|
"CQ Zone": data.cq_zone,
|
|
|
|
"ITU Zone": data.itu_zone,
|
2023-01-01 04:10:45 -05:00
|
|
|
"IOTA Designator": data.iota,
|
|
|
|
"Expires": f"{data.expire_date:%Y-%m-%d}" if data.expire_date is not None else None,
|
2021-03-16 22:04:05 -04:00
|
|
|
"Aliases": ", ".join(data.aliases) if data.aliases else None,
|
2023-01-01 04:10:45 -05:00
|
|
|
"Previous Callsign": data.prev_call,
|
2023-01-01 16:30:14 -05:00
|
|
|
"License Class": data.lic_class,
|
|
|
|
"Trustee": data.trustee if data.trustee is not None and data.trustee != Trustee(None, None) else None,
|
|
|
|
"Born": data.born,
|
|
|
|
} | qsl
|
2019-10-16 17:23:56 -04:00
|
|
|
|
|
|
|
|
2019-10-15 17:37:13 -04:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(QRZCog(bot))
|