This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, improve startup time, fix some bugs
- Loading branch information
Showing
4 changed files
with
105 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from abc import ABCMeta, abstractmethod | ||
from pathlib import Path | ||
|
||
from aiofile import async_open | ||
from aiohttp import ClientSession | ||
|
||
from .utils import sync_to_async | ||
|
||
|
||
class ABCResultHandler(metaclass=ABCMeta): | ||
__slots__ = () | ||
|
||
async def pre_run(self) -> None: # noqa: B027 | ||
pass | ||
|
||
@abstractmethod | ||
async def save(self, gift_url: str) -> None: | ||
pass | ||
|
||
|
||
class FileHandler(ABCResultHandler): | ||
__slots__ = ("file_path", "_ready_event") | ||
|
||
def __init__(self, file_path: str) -> None: | ||
self.file_path = file_path | ||
self._ready_event = asyncio.Event() | ||
|
||
async def pre_run(self) -> None: | ||
def inner() -> None: | ||
path = Path(self.file_path) | ||
if path.is_dir(): | ||
msg = "FileName must be a file, not a directory" | ||
raise ValueError(msg) | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
await sync_to_async(inner) | ||
self._ready_event.set() | ||
|
||
async def save(self, gift_url: str) -> None: | ||
await self._ready_event.wait() | ||
async with async_open(self.file_path, "a", encoding="utf-8") as f: | ||
await f.write(f"\n{gift_url}") | ||
|
||
|
||
class DiscordWebhookHandler(ABCResultHandler): | ||
__slots__ = ("session", "url") | ||
|
||
def __init__(self, session: ClientSession, url: str) -> None: | ||
self.session = session | ||
self.url = url | ||
|
||
async def save(self, gift_url: str) -> None: | ||
async with self.session.post( | ||
self.url, json={"content": f"@everyone {gift_url}"} | ||
): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from typing import Any, Callable, Coroutine, Set, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
background_tasks: Set[asyncio.Task[Any]] = set() | ||
|
||
|
||
def create_background_task(coro: Coroutine[Any, Any, Any]) -> None: | ||
task = asyncio.create_task(coro) | ||
background_tasks.add(task) | ||
task.add_done_callback(background_tasks.discard) | ||
|
||
|
||
def sync_to_async(func: Callable[[], T]) -> asyncio.Future[T]: | ||
loop = asyncio.get_running_loop() | ||
return loop.run_in_executor(None, func) |