From db62446d9c30660b9d318588388633d476e035c2 Mon Sep 17 00:00:00 2001 From: Abigail Gold Date: Mon, 11 Nov 2019 20:59:10 -0500 Subject: [PATCH] Prep for implementing ctyparse. UNTESTED! Some TODOs listed in the code related to threading. --- cogs/lookupcog.py | 88 ++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 51 deletions(-) diff --git a/cogs/lookupcog.py b/cogs/lookupcog.py index ae8a841..589b64d 100644 --- a/cogs/lookupcog.py +++ b/cogs/lookupcog.py @@ -12,16 +12,18 @@ from discord.ext import commands, tasks import json from datetime import datetime -from util import cty_json +from ctyparser import BigCty class LookupCog(commands.Cog): def __init__(self, bot: commmands.Bot): self.bot = bot self.gs = bot.get_cog("GlobalSettings") - self.CTY = None - self.CTY_list = None - self.up_cty_first_run = True + try: + self.CTY = BigCty('./data/cty.json') + except: # TODO: BARE EXCEPT! Figure out which one it is + self.CTY = BigCty() + self.CTY.update() # TODO: thread this @commands.command(name="sat") async def _sat_lookup(self, ctx: commands.Context, sat: str, grid1: str, grid2: str = None): @@ -29,72 +31,56 @@ class LookupCog(commands.Cog): Usage: `?sat sat_name grid1 grid2`''' now = datetime.utcnow().strftime('%Y-%m-%d%%20%H:%M') if grid2 is None or grid2 == '': - await ctx.send( - f'http://www.satmatch.com/satellite/{sat}/obs1/{grid1}' + - f'?search_start_time={now}&duration_hrs=24' - ) + await ctx.send(f'http://www.satmatch.com/satellite/{sat}/obs1/{grid1}' + f'?search_start_time={now}&duration_hrs=24') else: - await ctx.send( - f'http://www.satmatch.com/satellite/{sat}/obs1/{grid1}' + - f'/obs2/{grid2}?search_start_time={now}&duration_hrs=24' - ) + await ctx.send(f'http://www.satmatch.com/satellite/{sat}/obs1/{grid1}' + f'/obs2/{grid2}?search_start_time={now}&duration_hrs=24') @commands.command(name="dxcc", aliases=['dx']) - async def _dxcc_lookup(self, ctx: commands.Context, q: str): + async def _dxcc_lookup(self, ctx: commands.Context, query: str): '''Gets info about a prefix.''' with ctx.typing(): noMatch = True - qMatch = None - q = q.upper() - q0 = q - if q != 'LAST_UPDATED': - embed = discord.Embed(title=f'DXCC Info for {q0}') - embed.description = f'Prefix {q0} not found' + queryMatch = None + query = query.upper() + if query != 'LAST_UPDATED': + embed = discord.Embed(title=f'DXCC Info for {query}') + embed.description = f'Prefix {query} not found' embed.colour = self.gs.colours.bad while noMatch: - if q in self.CTY_list: - qMatch = q + if query in self.CTY.keys(): + queryMatch = query noMatch = False else: - q = q[:-1] - if len(q) == 0: + query = query[:-1] + if len(query) == 0: noMatch = False - if qMatch is not None: - d = self.CTY[qMatch] + if queryMatch is not None: + data = self.CTY[queryMatch] embed = embed.add_field(name="Entity", - value=d['entity']) - embed = embed.add_field(name="CQ Zone", - value=d['cq']) - embed = embed.add_field(name="ITU Zone", - value=d['itu']) - embed = embed.add_field(name="Continent", - value=d['continent']) - tz = d['tz'] - if tz > 0: - tz = '+' + str(tz) - embed = embed.add_field(name="Time Zone", value=tz) + value=data['entity'])\ + .add_field(name="CQ Zone", + value=data['cq'])\ + .add_field(name="ITU Zone", + value=data['itu'])\ + .add_field(name="Continent", + value=data['continent'])\ + .add_field(name="Time Zone", + value=f'+{data["tz"]}' if data['tz'] > 0 else str(data['tz'])) embed.description = '' embed.colour = self.gs.colours.good else: - updatedDate = self.CTY['last_updated'][0:4] + '-' - updatedDate += self.CTY['last_updated'][4:6] + '-' - updatedDate += self.CTY['last_updated'][6:8] - r = f'CTY.DAT last updated on {updatedDate}' - embed = discord.Embed(title=r, colour=self.gs.colours.neutral) + result = f'CTY.DAT last updated on {self.CTY.format_version()}' + embed = discord.Embed(title=result, colour=self.gs.colours.neutral) await ctx.send(embed=embed) @tasks.loop(hours=24) async def _update_cty(self): - print('Checking for CTY update...') - regen = cty_json.genCtyJson() - if regen or self.up_cty_first_run: - with open('resources/cty.json') as ctyfile: - print('Reloading CTY JSON data...') - self.CTY = json.load(ctyfile) - self.CTY_list = list(self.CTY.keys()) - self.CTY_list.sort() - self.CTY_list.sort(key=len, reverse=True) - self.up_cty_first_run = False + self.CTY.update() # TODO: Thread this! + + def cog_unload(self): + self.CTY.dump("./data/cty.json") def setup(bot: commands.Bot):