From d479abce1d1a68d84ce116c922e7d8a10a560a3c Mon Sep 17 00:00:00 2001 From: Lemon Rose <78662983+japandotorg@users.noreply.github.com> Date: Fri, 20 Sep 2024 17:21:50 +0530 Subject: [PATCH] [Screenshot] fix counter --- screenshot/__init__.py | 4 +--- screenshot/common/utils.py | 13 ++++++++++++- screenshot/core.py | 14 ++++++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/screenshot/__init__.py b/screenshot/__init__.py index 93b88d8..d9cb03d 100644 --- a/screenshot/__init__.py +++ b/screenshot/__init__.py @@ -22,7 +22,6 @@ SOFTWARE. """ -import asyncio import platform from redbot.core.bot import Red @@ -32,8 +31,7 @@ async def setup(bot: Red) -> None: - cog: Screenshot = Screenshot(bot) - asyncio.create_task(cog.update_counter_api()) if platform.system().lower() not in ["windows", "linux"]: raise CogLoadError("This cog is only available for linux and windows devices right now.") + cog: Screenshot = Screenshot(bot) await bot.add_cog(cog) diff --git a/screenshot/common/utils.py b/screenshot/common/utils.py index 2d4932e..a6b36d7 100644 --- a/screenshot/common/utils.py +++ b/screenshot/common/utils.py @@ -22,8 +22,10 @@ SOFTWARE. """ +import functools +import hashlib import logging -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Callable import discord from redbot.core import commands @@ -41,6 +43,15 @@ log: logging.Logger = logging.getLogger("red.seina.screenshot.core") +def counter(func: Callable[["Screenshot"], str]) -> Callable[["Screenshot"], str]: + @functools.wraps(func) + def wrapper(self: "Screenshot") -> str: + string: str = func(self) + return hashlib.sha1(string.encode("utf-8")).hexdigest() + + return wrapper + + async def send_notification(cog: "Screenshot") -> None: await cog.bot.send_to_owners( content=( diff --git a/screenshot/core.py b/screenshot/core.py index f1d7337..f6a37bd 100644 --- a/screenshot/core.py +++ b/screenshot/core.py @@ -41,7 +41,7 @@ from .common.downloader import DriverManager from .common.exceptions import ProxyConnectFailedError from .common.filter import Filter -from .common.utils import URLConverter, send_notification +from .common.utils import URLConverter, send_notification, counter as counter_api log: logging.Logger = logging.getLogger("red.seina.screenshot.core") @@ -82,6 +82,8 @@ def __init__(self, bot: Red) -> None: self.driver: FirefoxManager = FirefoxManager(self) self.filter: Filter = Filter() + self.__task: asyncio.Task[None] = asyncio.create_task(self.update_counter_api()) + async def cog_load(self) -> None: if self.manager.firefox is None: await self.manager.download_firefox() @@ -94,17 +96,21 @@ async def cog_load(self) -> None: self.bg_task.start() # type: ignore async def cog_unload(self) -> None: + self.__task.cancel() self.bg_task.cancel() # type: ignore await self.session.close() with contextlib.suppress(BaseException): self.driver.clear_all_drivers() + @counter_api + def counter(self) -> str: + return self.__class__.__name__.lower() + async def update_counter_api(self) -> None: + await self.bot.wait_until_red_ready() if not await self.config.updated(): await self.session.get( - "https://api.counterapi.dev/v1/japandotorg/{}/up".format( - self.__class__.__name__.lower() - ) + "https://api.counterapi.dev/v1/japandotorg/{}/up".format(self.counter()) ) await self.config.updated.set(True)