Skip to content

Commit

Permalink
[Screenshot] fix counter
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Sep 20, 2024
1 parent 50ab71c commit d479abc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 1 addition & 3 deletions screenshot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
SOFTWARE.
"""

import asyncio
import platform

from redbot.core.bot import Red
Expand All @@ -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)
13 changes: 12 additions & 1 deletion screenshot/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=(
Expand Down
14 changes: 10 additions & 4 deletions screenshot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down

0 comments on commit d479abc

Please sign in to comment.