improve(commands|Network>iplocalise): rewrite

This commit is contained in:
Romain J 2021-01-26 15:43:16 +01:00
parent d66bec65ae
commit dd09a53c0e
2 changed files with 52 additions and 54 deletions

View file

@ -65,3 +65,36 @@ async def get_ipinfo_result(
return (await handler.getDetails(ip_address)).all
except RequestQuotaExceededError:
return {}
def merge_ipinfo_ipwhois(ipinfo_result: dict, ipwhois_result: dict) -> dict:
output = {"belongs": "N/A", "rir": "N/A", "region": "N/A", "flag": "N/A"}
if ipinfo_result:
org = ipinfo_result.get("org", "")
asn = org.split()[0]
output["belongs"] = f"[{org}](https://bgp.he.net/{asn})"
output["rir"] = f"```{ipwhois_result.get('asn_registry', 'N/A')}```"
output["region"] = (
f"```{ipinfo_result.get('city', 'N/A')} - "
f"{ipinfo_result.get('region', 'N/A')} "
f"({ipinfo_result.get('country', 'N/A')})```"
)
output["flag"] = (
f"https://www.countryflags.io/{ipinfo_result['country']}"
f"/shiny/64.png"
)
elif ipwhois_result:
org = ipwhois_result.get("asn_description", "N/A")
asn = ipwhois_result.get("asn", "N/A")
asn_country = ipwhois_result.get("asn_country_code", "N/A")
output["belongs"] = f"{org} ([AS{asn}](https://bgp.he.net/{asn}))"
output["rir"] = f"```{ipwhois_result['asn_registry']}```"
output["region"] = f"```{asn_country}```"
output[
"flag"
] = f"https://www.countryflags.io/{asn_country}/shiny/64.png"
return output

View file

@ -31,6 +31,7 @@ from .functions.utils import (
get_hostname,
get_ipinfo_result,
get_ipwhois_result,
merge_ipinfo_ipwhois,
)
log = logging.getLogger("tuxbot.cogs.Network")
@ -85,6 +86,8 @@ class Network(commands.Cog, name="Network"):
None, functools.partial(get_ipwhois_result, ip_address)
)
merged_results = merge_ipinfo_ipwhois(ipinfo_result, ipwhois_result)
e = discord.Embed(
title=_(
"Information for ``{ip} ({ip_address})``", ctx, self.bot.config
@ -92,61 +95,23 @@ class Network(commands.Cog, name="Network"):
color=0x5858D7,
)
if ipinfo_result:
org = ipinfo_result.get("org", "")
asn = org.split()[0]
e.add_field(
name=_("Belongs to:", ctx, self.bot.config),
value=merged_results["belongs"],
inline=True,
)
e.add_field(
name="RIR :",
value=merged_results["rir"],
inline=True,
)
e.add_field(
name=_("Region:", ctx, self.bot.config),
value=merged_results["region"],
inline=False,
)
e.add_field(
name=_("Belongs to:", ctx, self.bot.config),
value=f"[{org}](https://bgp.he.net/{asn})",
inline=True,
)
if ipwhois_result:
e.add_field(
name="RIR :",
value=f"```{ipwhois_result['asn_registry']}```",
inline=True,
)
e.add_field(
name=_("Region:", ctx, self.bot.config),
value=f"```{ipinfo_result.get('city', 'N/A')} - "
f"{ipinfo_result.get('region', 'N/A')} "
f"({ipinfo_result.get('country', 'N/A')})```",
inline=False,
)
e.set_thumbnail(
url=f"https://www.countryflags.io/{ipinfo_result['country']}"
f"/shiny/64.png"
)
elif ipwhois_result:
org = ipwhois_result.get("asn_description", "N/A")
asn = ipwhois_result.get("asn", "N/A")
asn_country = ipwhois_result.get("asn_country_code", "N/A")
e.add_field(
name=_("Belongs to:", ctx, self.bot.config),
value=f"{org} ([AS{asn}](https://bgp.he.net/{asn}))",
inline=True,
)
e.add_field(
name="RIR :",
value=f"```{ipwhois_result['asn_registry']}```",
inline=True,
)
e.add_field(
name=_("Region:", ctx, self.bot.config),
value=f"```{asn_country}```",
inline=False,
)
e.set_thumbnail(
url=f"https://www.countryflags.io/{asn_country}/shiny/64.png"
)
e.set_thumbnail(url=merged_results["flag"])
e.set_footer(
text=_("Hostname: {hostname}", ctx, self.bot.config).format(