2019-10-07 16:11:30 -04:00
|
|
|
"""
|
2019-12-07 17:26:55 -05:00
|
|
|
ae7q extension for qrm
|
2019-10-07 16:11:30 -04:00
|
|
|
---
|
2020-01-06 23:27:48 -05:00
|
|
|
Copyright (C) 2019-2020 Abigail Gold, 0x5c
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-12-08 15:35:58 -05:00
|
|
|
This file is part of discord-qrm2 and is released under the terms of the GNU
|
2019-10-07 16:11:30 -04:00
|
|
|
General Public License, version 2.
|
2019-10-08 18:57:37 -04:00
|
|
|
---
|
|
|
|
Test callsigns:
|
|
|
|
KN8U: active, restricted
|
|
|
|
AB2EE: expired, restricted
|
|
|
|
KE8FGB: assigned once, no restrictions
|
|
|
|
NA2AAA: unassigned, no records
|
2020-01-04 16:33:49 -05:00
|
|
|
KC4USA: reserved but has call history
|
|
|
|
WF4EMA: "
|
2019-10-07 16:11:30 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import discord.ext.commands as commands
|
|
|
|
|
2020-01-07 05:36:09 -05:00
|
|
|
import aiohttp
|
2019-10-07 16:11:30 -04:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
import common as cmn
|
|
|
|
|
2019-10-07 16:11:30 -04:00
|
|
|
|
|
|
|
class AE7QCog(commands.Cog):
|
2019-10-18 08:27:05 -04:00
|
|
|
def __init__(self, bot: commands.Bot):
|
2019-10-07 16:11:30 -04:00
|
|
|
self.bot = bot
|
2020-01-07 05:36:09 -05:00
|
|
|
self.session = aiohttp.ClientSession(connector=bot.qrm.connector)
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@commands.group(name="ae7q", aliases=["ae"], category=cmn.cat.lookup)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _ae7q_lookup(self, ctx: commands.Context):
|
2019-12-06 01:19:42 -05:00
|
|
|
'''Look up a callsign, FRN, or Licensee ID on [ae7q.com](http://ae7q.com/).'''
|
2019-10-07 16:11:30 -04:00
|
|
|
if ctx.invoked_subcommand is None:
|
2019-10-28 00:23:56 -04:00
|
|
|
await ctx.send_help(ctx.command)
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-12-06 01:19:42 -05:00
|
|
|
@_ae7q_lookup.command(name="call", category=cmn.cat.lookup)
|
2019-10-18 08:27:05 -04:00
|
|
|
async def _ae7q_call(self, ctx: commands.Context, callsign: str):
|
2019-12-06 01:19:42 -05:00
|
|
|
'''Look up the history for a callsign on [ae7q.com](http://ae7q.com/).'''
|
2019-10-07 16:11:30 -04:00
|
|
|
callsign = callsign.upper()
|
2019-10-08 18:57:37 -04:00
|
|
|
desc = ''
|
2019-10-07 16:11:30 -04:00
|
|
|
base_url = "http://ae7q.com/query/data/CallHistory.php?CALL="
|
2019-12-23 10:32:24 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-12-23 17:54:20 -05:00
|
|
|
async with self.session.get(base_url + callsign) as resp:
|
|
|
|
if resp.status != 200:
|
|
|
|
embed.title = "Error in AE7Q call command"
|
|
|
|
embed.description = 'Could not load AE7Q'
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
|
|
|
page = await resp.text()
|
2019-10-07 16:11:30 -04:00
|
|
|
|
|
|
|
soup = BeautifulSoup(page, features="html.parser")
|
2019-10-08 18:57:37 -04:00
|
|
|
tables = soup.select("table.Database")
|
|
|
|
|
|
|
|
for table in tables:
|
|
|
|
rows = table.find_all("tr")
|
|
|
|
if len(rows) > 1 and len(rows[0]) > 1:
|
|
|
|
break
|
2019-10-18 08:27:05 -04:00
|
|
|
if desc == '':
|
2019-10-08 18:57:37 -04:00
|
|
|
for row in rows:
|
|
|
|
desc += " ".join(row.getText().split())
|
|
|
|
desc += '\n'
|
|
|
|
desc = desc.replace(callsign, f'`{callsign}`')
|
|
|
|
rows = None
|
|
|
|
|
2020-01-04 16:33:49 -05:00
|
|
|
first_header = ''.join(rows[0].find_all("th")[0].strings)
|
|
|
|
|
|
|
|
if rows is None or first_header != 'Entity Name':
|
2019-12-16 03:49:34 -05:00
|
|
|
embed.title = f"AE7Q History for {callsign}"
|
|
|
|
embed.colour = cmn.colours.bad
|
|
|
|
embed.url = f"{base_url}{callsign}"
|
2019-10-08 18:57:37 -04:00
|
|
|
embed.description = desc
|
|
|
|
embed.description += f'\nNo records found for `{callsign}`'
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
return
|
2019-10-07 16:11:30 -04:00
|
|
|
|
|
|
|
table_contents = [] # store your table here
|
|
|
|
for tr in rows:
|
|
|
|
if rows.index(tr) == 0:
|
2020-01-04 16:33:49 -05:00
|
|
|
# first_header = ''.join(tr.find_all("th")[0].strings)
|
|
|
|
# if first_header == 'Entity Name':
|
|
|
|
# print('yooooo')
|
2019-10-07 16:11:30 -04:00
|
|
|
continue
|
2019-10-18 08:27:05 -04:00
|
|
|
row_cells = []
|
|
|
|
for td in tr.find_all('td'):
|
|
|
|
if td.getText().strip() != '':
|
|
|
|
row_cells.append(td.getText().strip())
|
|
|
|
else:
|
|
|
|
row_cells.append('-')
|
|
|
|
if 'colspan' in td.attrs and int(td.attrs['colspan']) > 1:
|
|
|
|
for i in range(int(td.attrs['colspan']) - 1):
|
|
|
|
row_cells.append(row_cells[-1])
|
|
|
|
for i, cell in enumerate(row_cells):
|
|
|
|
if cell == '"':
|
2020-01-04 15:40:40 -05:00
|
|
|
row_cells[i] = table_contents[-1][i]
|
2019-10-07 16:11:30 -04:00
|
|
|
if len(row_cells) > 1:
|
|
|
|
table_contents += [row_cells]
|
|
|
|
|
2019-12-16 03:49:34 -05:00
|
|
|
embed = cmn.embed_factory(ctx)
|
|
|
|
embed.title = f"AE7Q Records for {callsign}"
|
|
|
|
embed.colour = cmn.colours.good
|
|
|
|
embed.url = f"{base_url}{callsign}"
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-10-08 18:57:37 -04:00
|
|
|
for row in table_contents[0:3]:
|
|
|
|
header = f'**{row[0]}** ({row[1]})'
|
2019-12-25 02:48:59 -05:00
|
|
|
body = (f'Class: *{row[2]}*\n'
|
|
|
|
f'Region: *{row[3]}*\n'
|
|
|
|
f'Status: *{row[4]}*\n'
|
|
|
|
f'Granted: *{row[5]}*\n'
|
|
|
|
f'Effective: *{row[6]}*\n'
|
|
|
|
f'Cancelled: *{row[7]}*\n'
|
|
|
|
f'Expires: *{row[8]}*')
|
2019-10-08 18:57:37 -04:00
|
|
|
embed.add_field(name=header, value=body, inline=False)
|
|
|
|
|
|
|
|
embed.description = desc
|
|
|
|
if len(table_contents) > 3:
|
2019-12-25 02:48:59 -05:00
|
|
|
embed.description += f'\nRecords 1 to 3 of {len(table_contents)}. See ae7q.com for more...'
|
2019-10-07 16:11:30 -04:00
|
|
|
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
2019-10-08 18:57:37 -04:00
|
|
|
# TODO: write commands for other AE7Q response types?
|
|
|
|
# @_ae7q_lookup.command(name="trustee")
|
2019-12-06 01:19:42 -05:00
|
|
|
# async def _ae7q_trustee(self, ctx: commands.Context, callsign: str):
|
2019-10-08 18:57:37 -04:00
|
|
|
# pass
|
|
|
|
|
|
|
|
# @_ae7q_lookup.command(name="applications", aliases=['apps'])
|
2019-12-06 01:19:42 -05:00
|
|
|
# async def _ae7q_applications(self, ctx: commands.Context, callsign: str):
|
2019-10-08 18:57:37 -04:00
|
|
|
# pass
|
|
|
|
|
|
|
|
# @_ae7q_lookup.command(name="frn")
|
2019-12-06 01:19:42 -05:00
|
|
|
# async def _ae7q_frn(self, ctx: commands.Context, frn: str):
|
2019-10-08 18:57:37 -04:00
|
|
|
# base_url = "http://ae7q.com/query/data/FrnHistory.php?FRN="
|
|
|
|
# pass
|
2019-10-07 16:11:30 -04:00
|
|
|
|
2019-10-08 18:57:37 -04:00
|
|
|
# @_ae7q_lookup.command(name="licensee", aliases=["lic"])
|
2019-12-06 01:19:42 -05:00
|
|
|
# async def _ae7q_licensee(self, ctx: commands.Context, frn: str):
|
2019-10-08 18:57:37 -04:00
|
|
|
# base_url = "http://ae7q.com/query/data/LicenseeIdHistory.php?ID="
|
|
|
|
# pass
|
2019-10-07 16:11:30 -04:00
|
|
|
|
|
|
|
|
2019-10-18 08:27:05 -04:00
|
|
|
def setup(bot: commands.Bot):
|
2019-10-07 16:11:30 -04:00
|
|
|
bot.add_cog(AE7QCog(bot))
|