2019-12-11 02:33:11 -05:00
|
|
|
"""
|
2021-03-28 09:50:51 -04:00
|
|
|
DXCC Prefix Lookup extension for qrm
|
2019-12-11 02:33:11 -05:00
|
|
|
---
|
2021-03-28 13:57:03 -04:00
|
|
|
Copyright (C) 2019-2020 classabbyamp, 0x5c (as lookup.py)
|
|
|
|
Copyright (C) 2021 classabbyamp, 0x5c
|
2019-12-11 02:33:11 -05:00
|
|
|
|
2021-06-26 20:23:55 -04:00
|
|
|
SPDX-License-Identifier: LiLiQ-Rplus-1.1
|
2019-12-11 02:33:11 -05:00
|
|
|
"""
|
|
|
|
|
2020-01-31 06:50:50 -05:00
|
|
|
|
2019-12-11 02:33:11 -05:00
|
|
|
import threading
|
2020-10-30 07:07:56 -04:00
|
|
|
from pathlib import Path
|
2019-12-11 02:33:11 -05:00
|
|
|
|
|
|
|
from ctyparser import BigCty
|
|
|
|
|
2020-02-05 07:09:08 -05:00
|
|
|
from discord.ext import commands, tasks
|
|
|
|
|
2019-12-11 02:33:11 -05:00
|
|
|
import common as cmn
|
|
|
|
|
|
|
|
|
2020-10-30 07:07:56 -04:00
|
|
|
cty_path = Path("./data/cty.json")
|
|
|
|
|
|
|
|
|
2021-03-28 09:50:51 -04:00
|
|
|
class DXCCCog(commands.Cog):
|
2019-12-11 02:33:11 -05:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
try:
|
2020-10-30 07:07:56 -04:00
|
|
|
self.cty = BigCty(cty_path)
|
2019-12-11 02:33:11 -05:00
|
|
|
except OSError:
|
|
|
|
self.cty = BigCty()
|
|
|
|
|
2021-03-28 02:38:36 -04:00
|
|
|
@commands.command(name="dxcc", aliases=["dx"], category=cmn.Cats.LOOKUP)
|
2019-12-11 02:33:11 -05:00
|
|
|
async def _dxcc_lookup(self, ctx: commands.Context, query: str):
|
2020-02-15 04:59:25 -05:00
|
|
|
"""Gets DXCC info about a callsign prefix."""
|
2020-02-25 20:36:21 -05:00
|
|
|
query = query.upper()
|
|
|
|
full_query = query
|
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = "DXCC Info for "
|
|
|
|
embed.description = f"*Last Updated: {self.cty.formatted_version}*"
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
while query:
|
|
|
|
if query in self.cty.keys():
|
|
|
|
data = self.cty[query]
|
|
|
|
embed.add_field(name="Entity", value=data["entity"])
|
|
|
|
embed.add_field(name="CQ Zone", value=data["cq"])
|
|
|
|
embed.add_field(name="ITU Zone", value=data["itu"])
|
|
|
|
embed.add_field(name="Continent", value=data["continent"])
|
|
|
|
embed.add_field(name="Time Zone",
|
|
|
|
value=f"+{data['tz']}" if data["tz"] > 0 else str(data["tz"]))
|
|
|
|
embed.title += query
|
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
break
|
2019-12-11 02:33:11 -05:00
|
|
|
else:
|
2020-02-25 20:36:21 -05:00
|
|
|
query = query[:-1]
|
|
|
|
else:
|
|
|
|
embed.title += full_query + " not found"
|
|
|
|
embed.colour = cmn.colours.bad
|
2019-12-11 02:33:11 -05:00
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
@tasks.loop(hours=24)
|
|
|
|
async def _update_cty(self):
|
2020-10-30 07:07:56 -04:00
|
|
|
update = threading.Thread(target=run_update, args=(self.cty, cty_path))
|
2019-12-11 02:33:11 -05:00
|
|
|
update.start()
|
|
|
|
|
|
|
|
|
|
|
|
def run_update(cty_obj, dump_loc):
|
|
|
|
update = cty_obj.update()
|
|
|
|
if update:
|
|
|
|
cty_obj.dump(dump_loc)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot: commands.Bot):
|
2021-03-28 09:50:51 -04:00
|
|
|
dxcccog = DXCCCog(bot)
|
|
|
|
bot.add_cog(dxcccog)
|
|
|
|
dxcccog._update_cty.start()
|