Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SyberiaK committed Jul 4, 2024
1 parent 9805084 commit b1d11ee
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
3 changes: 3 additions & 0 deletions functions/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from bottypes import UserSession


__all__ = ['ignore_message_not_modified']


def ignore_message_not_modified(func):
"""Decorator to ignore annoying `pyrogram.errors.MessageNotModified`."""

Expand Down
11 changes: 4 additions & 7 deletions functions/locale.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from babel import Locale as BabelLocale, UnknownLocaleError

from l10n import Locale, locale as _loc, get_available_languages as _gal
from l10n import Locale, locale as _loc, get_available_languages


CIS_LANG_CODES = ('kk',)


__all__ = ['locale', 'get_refined_lang_code', 'get_available_languages']


def locale(lang: str = 'en') -> Locale:
"""Returns a Locale object based of user's language."""

Expand All @@ -25,9 +28,3 @@ def get_refined_lang_code(_locale: Locale) -> str:
lang_code = 'en'

return lang_code


def get_available_languages() -> dict[str, str]:
"""Returns a dictionary with lang codes as keys and lang names as values."""

return _gal()
3 changes: 3 additions & 0 deletions functions/utime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime as dt


__all__ = ['utcnow', 'utcfromtimestamp']


def utcnow() -> dt.datetime:
"""Timezone-aware version of deprecated ``datetime.datetime.utcnow()``."""

Expand Down
11 changes: 7 additions & 4 deletions game_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ async def update_depots():

for build_id, new_value in new_data.items():
old_value = cache.get(build_id)
if old_value is None or old_value == new_value:
if old_value is None:
if build_id == 'public_build_id':
game_version_data = await get_game_version_loop(cache.get('cs2_client_version'))
cache.update(game_version_data.asdict())
continue
if old_value == new_value:
continue

if build_id == 'dpr_build_id' and new_value == old_public_build_id:
await send_alert('dpr_build_sync_id', new_value)
Expand All @@ -170,23 +172,24 @@ async def update_depots():
logging.info('Successfully dumped game version data.')


async def get_game_version_loop(cs2_client_version: int) -> GameVersionData:
async def get_game_version_loop(cs2_client_version: int | None) -> GameVersionData:
timeout = 30 * 60
timeout_start = time.time()
with requests.Session() as session:
while time.time() < timeout_start + timeout:
data = await get_game_version(session, cs2_client_version)
if data:
return data

logging.warning('Failed to pull the game version data, retry in 45 seconds...')
await asyncio.sleep(45)
# xPaw: Zzz...
# because of this, we retry in an hour
logging.warning('Reached a timeout while trying to pull the game version data, retry in an hour...')
await asyncio.sleep(60 * 60)
await get_game_version_loop(cs2_client_version)


async def get_game_version(session: requests.Session, cs2_client_version: int) -> GameVersionData | None:
async def get_game_version(session: requests.Session, cs2_client_version: int | None) -> GameVersionData | None:
# noinspection PyBroadException
try:
data = GameVersion.request(session)
Expand Down
12 changes: 6 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio
import datetime as dt
import json
from json import JSONDecodeError
import traceback
from typing import TYPE_CHECKING
import logging
Expand All @@ -21,7 +21,7 @@
from bottypes import BotClient, BotLogger, ExtendedIKB, ExtendedIKM
import config
from db import db_session
from functions import info_formatters, utime
from functions import caching, info_formatters, utime
from functions.decorators import ignore_message_not_modified
from functions.locale import get_available_languages
import keyboards
Expand Down Expand Up @@ -301,15 +301,15 @@ async def send_dc_state(client: BotClient, session: UserSession, bot_message: Me
except Exception as e:
return await handle_exceptions_in_callback(client, session, bot_message, e)


# cat: Profile info


@bot.navmenu(LK.bot_profile_info, came_from=main_menu, ignore_message_not_modified=True)
async def profile_info(client: BotClient, session: UserSession, bot_message: Message):
with open(config.CORE_CACHE_FILE_PATH, encoding='utf-8') as f:
cache_file = json.load(f)
cache = caching.load_cache(config.CORE_CACHE_FILE_PATH)

if cache_file.get('webapi_state') != 'normal':
if States.get(cache.get('webapi_state')) != States.NORMAL:
return await send_about_maintenance(client, session, bot_message)

await bot_message.edit(session.locale.bot_choose_cmd,
Expand Down Expand Up @@ -420,7 +420,7 @@ async def user_game_stats_process(client: BotClient, session: UserSession, bot_m
html_content=stats_page_text,
author_name='@INCS2bot',
author_url='https://t.me/INCS2bot')
except json.JSONDecodeError:
except JSONDecodeError:
await user_input.delete()
return await user_game_stats(client, session, bot_message, last_error=session.locale.user_telegraph_error)

Expand Down
2 changes: 1 addition & 1 deletion plugins/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def inner(client: BotClient, session: UserSession, inline_query: InlineQue
f'\n'
f'↩️ inline_query',
disable_notification=True, parse_mode=ParseMode.DISABLED)

client.rstats.exceptions_caught += 1
return inner


Expand Down

0 comments on commit b1d11ee

Please sign in to comment.