From dd09a53c0e5ef5c48d5dbb6961e85755bc3a804a Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 26 Jan 2021 15:43:16 +0100 Subject: [PATCH] improve(commands|Network>iplocalise): rewrite --- tuxbot/cogs/Network/functions/utils.py | 33 ++++++++++++ tuxbot/cogs/Network/network.py | 73 +++++++------------------- 2 files changed, 52 insertions(+), 54 deletions(-) diff --git a/tuxbot/cogs/Network/functions/utils.py b/tuxbot/cogs/Network/functions/utils.py index dba9730..eb12199 100644 --- a/tuxbot/cogs/Network/functions/utils.py +++ b/tuxbot/cogs/Network/functions/utils.py @@ -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 diff --git a/tuxbot/cogs/Network/network.py b/tuxbot/cogs/Network/network.py index 7604540..9a90ac0 100644 --- a/tuxbot/cogs/Network/network.py +++ b/tuxbot/cogs/Network/network.py @@ -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(