Skip to content

Commit f79ce75

Browse files
committed
Imports Optimizations, Time (conversion) command and Time parser util.
1 parent ff54aec commit f79ce75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+6575
-288
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ repos:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
8+
- id: pretty-format-json
9+
args: ['--autofix', '--no-sort-keys', '--indent=4', '--top-keys=comma']
810
- repo: https://github.com/astral-sh/ruff-pre-commit
911
rev: v0.14.6
1012
hooks:

bot/apis/best_logs/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from typing import TYPE_CHECKING, TypeVar
55

66
from aiohttp import ClientResponse
7-
from aiohttp_client_cache import CachedSession
87
from yarl import URL
98

109
from bot.utils.singleton import Singleton
@@ -25,6 +24,8 @@
2524
)
2625

2726
if TYPE_CHECKING:
27+
from aiohttp_client_cache import CachedSession
28+
2829
from bot.bot import Context, Gorenmu
2930

3031
T = TypeVar("T")

bot/apis/color.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass
4+
from typing import TYPE_CHECKING
25

36
import loguru
4-
from aiohttp_client_cache import CachedSession
7+
8+
if TYPE_CHECKING:
9+
from aiohttp_client_cache import CachedSession
510

611

712
@dataclass

bot/apis/currency.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass
4+
from typing import TYPE_CHECKING
25

36
import loguru
4-
from aiohttp_client_cache import CachedSession
7+
8+
if TYPE_CHECKING:
9+
from aiohttp_client_cache import CachedSession
510

611

712
@dataclass

bot/apis/dictionary.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass
4+
from typing import TYPE_CHECKING
25

36
import loguru
4-
from aiohttp_client_cache import CachedSession
7+
8+
if TYPE_CHECKING:
9+
from aiohttp_client_cache import CachedSession
510

611

712
@dataclass

bot/apis/emotes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from datetime import timedelta
66
from typing import TYPE_CHECKING
77

8-
from aiohttp_client_cache import CachedSession
9-
108
if TYPE_CHECKING:
9+
from aiohttp_client_cache import CachedSession
10+
1111
from bot.bot import Gorenmu
1212
from bot.ext import TranslationBase
1313
from bot.models import User

bot/apis/ivrfi/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from aiohttp_client_cache import CachedSession
65
from yarl import URL
76

87
from bot.utils.singleton import Singleton
@@ -11,6 +10,8 @@
1110
from .parsers.user import UserElement
1211

1312
if TYPE_CHECKING:
13+
from aiohttp_client_cache import CachedSession
14+
1415
from bot.bot import Gorenmu
1516

1617

bot/apis/translate/base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
"""base translator class"""
23

34
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri" # NOQA
@@ -6,8 +7,7 @@
67
from typing import List, Optional, Union
78

89
import aiohttp
9-
from aiohttp_client_cache import CachedSession
10-
from bs4 import BeautifulSoup
10+
1111

1212
from .constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
1313
from .exceptions import (
@@ -20,6 +20,10 @@
2020
TranslationNotFound,
2121
)
2222

23+
from typing import TYPE_CHECKING
24+
25+
if TYPE_CHECKING:
26+
from aiohttp_client_cache import CachedSession
2327

2428
class GoogleTranslator:
2529
"""
@@ -42,6 +46,8 @@ def __init__(
4246
@param source: source language to translate from
4347
@param target: target language to translate to
4448
"""
49+
from bs4 import BeautifulSoup
50+
self.bs4 = BeautifulSoup
4551
if element_query is None:
4652
element_query = {"class": "t0"}
4753
self.session: aiohttp.ClientResponse | CachedSession = session
@@ -127,7 +133,7 @@ async def translate(self, text: str, **kwargs) -> str | None:
127133
if request_failed(status_code=response.status):
128134
raise RequestError()
129135

130-
soup = BeautifulSoup(await response.text(), "html.parser")
136+
soup = self.bs4(await response.text(), "html.parser")
131137

132138
element = soup.find(self._element_tag, self._element_query)
133139
response.close()

bot/bot.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
from typing import TYPE_CHECKING
88

99
import twitchio
10-
from aiocache.backends.memcached import MemcachedCache
11-
from aiocache.backends.memory import SimpleMemoryCache
12-
from aiocache.backends.redis import RedisCache
1310
from twitchio.ext import commands
1411
from twitchio.ext.commands import CommandErrorPayload
1512

@@ -22,10 +19,14 @@
2219
LifecycleHandler,
2320
TokensHandler,
2421
)
25-
from bot.utils import Cache, Config, MemCache, TimeTools
22+
from bot.utils import Cache, Config, MemCache # , TimeTools
2623

2724
if TYPE_CHECKING:
2825
# from bot.api import api, api_start
26+
from aiocache.backends.memcached import MemcachedCache
27+
from aiocache.backends.memory import SimpleMemoryCache
28+
from aiocache.backends.redis import RedisCache
29+
2930
from bot.ext import Context
3031

3132

@@ -53,7 +54,7 @@ def __init__(self, configs: Config, case_insensitive: bool, log: Logger, adapter
5354
self.CommandHandler: CommandHandler = CommandHandler(bot=self)
5455
self.LifecycleHandler: LifecycleHandler = LifecycleHandler(bot=self)
5556
self.ContextHandler: ContextHandler = ContextHandler(bot=self)
56-
self.TimeTools: TimeTools = TimeTools()
57+
# self.TimeTools: TimeTools = TimeTools()
5758

5859
async def add_token(self, token: str, refresh: str) -> twitchio.authentication.ValidateTokenPayload:
5960
return await self.TokensHandler.add_token(token, refresh)

bot/cogs/afk/commands/afk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from typing import TYPE_CHECKING
55

66
from bot.ext import Context, Response, commands
7-
from bot.ext.named_tuples import RAfkNamedTuple
87
from bot.models import Status
8+
from bot.types.named_tuples import RAfkNamedTuple
99

1010
from .translations import Translations
1111

@@ -69,7 +69,7 @@ async def return_from_afk(self, ctx: Context) -> Response | bool:
6969
return False
7070
status = translations.AFK.afks(ctx).get(user_status.alias)
7171
humanize = translations.SupportTools.TimeTools.Humanize(ctx)
72-
a_time = humanize.created_a_time(user_status.updated_at, ctx.user.timezone_)
72+
a_time = humanize.created_a_time(user_status.updated_at, ctx.user.tz)
7373
clock_emojis = {
7474
0.0: "🕛",
7575
0.5: "🕧",
@@ -96,7 +96,7 @@ async def return_from_afk(self, ctx: Context) -> Response | bool:
9696
11.0: "🕚",
9797
11.5: "🕦",
9898
}
99-
delta = datetime.now(ctx.user.timezone_) - user_status.updated_at.astimezone(ctx.user.timezone_)
99+
delta = datetime.now(ctx.user.tz) - user_status.updated_at.astimezone(ctx.user.tz)
100100
rounded_hours = round(((delta.total_seconds() / 3600) % 12) * 2) / 2
101101
clock_emoji = clock_emojis.get(rounded_hours, "🕛")
102102

0 commit comments

Comments
 (0)