From b9f6c6cb0a1cce94d13fc816872a28fe010b78c0 Mon Sep 17 00:00:00 2001 From: Romain J Date: Fri, 23 Apr 2021 23:47:35 +0200 Subject: [PATCH] fix(commands:peeringdb|Network): fix false results --- tuxbot/cogs/Network/functions/utils.py | 39 ++++++++++++++++++++------ tuxbot/cogs/Network/network.py | 15 ++++++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/tuxbot/cogs/Network/functions/utils.py b/tuxbot/cogs/Network/functions/utils.py index b7f6993..bb21667 100644 --- a/tuxbot/cogs/Network/functions/utils.py +++ b/tuxbot/cogs/Network/functions/utils.py @@ -223,15 +223,36 @@ async def get_pydig_result( namespace="network", ) async def get_peeringdb_net_result(asn: str) -> dict: - try: - async with aiohttp.ClientSession() as cs: - async with cs.get( - f"https://peeringdb.com/api/net?asn={asn}", - timeout=aiohttp.ClientTimeout(total=21), - ) as s: - return await s.json() - except (asyncio.exceptions.TimeoutError,): - pass + # Q. why this and not ?asn= + # A. better do one request and save in cache than execute new + # request every time + @cached( + ttl=24 * 3600, + serializer=PickleSerializer(), + cache=Cache.MEMORY, + namespace="network", + ) + async def _local_cache() -> dict: + try: + async with aiohttp.ClientSession() as cs: + async with cs.get( + "https://peeringdb.com/api/net", + timeout=aiohttp.ClientTimeout(total=21), + ) as s: + return await s.json() + except asyncio.exceptions.TimeoutError: + pass + + return {"data": []} + + result = await _local_cache() + + if not result["data"]: + return result + + for data in result["data"]: + if data.get("asn", None) == int(asn): + return {"data": [data]} return {"data": []} diff --git a/tuxbot/cogs/Network/network.py b/tuxbot/cogs/Network/network.py index 9f5ef2c..55dcab1 100644 --- a/tuxbot/cogs/Network/network.py +++ b/tuxbot/cogs/Network/network.py @@ -8,7 +8,7 @@ import aiohttp import discord from aiohttp import ClientConnectorError, InvalidURL from jishaku.models import copy_context_with -from discord.ext import commands +from discord.ext import commands, tasks from ipinfo.exceptions import RequestQuotaExceededError from structured_config import ConfigFile from tuxbot.cogs.Network.functions.converters import ( @@ -62,6 +62,7 @@ class Network(commands.Cog): str(cogs_data_path("Network") / "config.yaml"), NetworkConfig, ).config + self._update_peering_db.start() # pylint: disable=no-member async def cog_command_error(self, ctx: ContextPlus, error): if isinstance( @@ -81,6 +82,16 @@ class Network(commands.Cog): async def cog_before_invoke(self, ctx: ContextPlus): await ctx.trigger_typing() + def cog_unload(self): + self._update_peering_db.cancel() # pylint: disable=no-member + + @tasks.loop(hours=24.0) + async def _update_peering_db(self): + await get_peeringdb_net_result(str(1)) + + logging.log(logging.INFO, "_update_peering_db") + self.bot.console.log("[Network]: _update_peering_db") + # ========================================================================= # ========================================================================= @@ -349,4 +360,4 @@ class Network(commands.Cog): data["created"], "%Y-%m-%dT%H:%M:%SZ" ) - await ctx.send(embed=e) + await ctx.send(f"https://www.peeringdb.com/net/{data['id']}", embed=e)