From 572dc8d1b467eecd7f964b0b34ff3b7d595617ed Mon Sep 17 00:00:00 2001 From: root Date: Wed, 15 Jul 2026 16:07:15 +0500 Subject: [PATCH] refactor(hosting): remove alternative text hosting providers --- tgbot/database/repository.py | 16 ++++- tgbot/routers/admin/admin_settings.py | 22 ------- tgbot/services/api_hosting_text.py | 94 ++------------------------- 3 files changed, 20 insertions(+), 112 deletions(-) diff --git a/tgbot/database/repository.py b/tgbot/database/repository.py index 38f119d..341b9d3 100644 --- a/tgbot/database/repository.py +++ b/tgbot/database/repository.py @@ -5,7 +5,7 @@ from typing import Any, Dict, Generic, List, Optional, Type, TypeVar from sqlalchemy import delete as sqlalchemy_delete from sqlalchemy import select from sqlalchemy import update as sqlalchemy_update -from tgbot.database.core import Base, database_path, session_scope +from tgbot.database.core import Base, database_path, engine, session_scope from tgbot.utils.misc.bot_logging import bot_logger ModelTranslator = TypeVar("ModelTranslator", bound=Base) @@ -150,7 +150,19 @@ class BaseRepository(Generic[ModelTranslator, EntityTranslator]): # Применение миграций и создание дефолтных строк async def prepare_database() -> None: database_path.parent.mkdir(parents=True, exist_ok=True) - # await run_migrations() + + # Импорт всех моделей до create_all, чтобы они зарегистрировались в Base.metadata + import tgbot.database.db_category # noqa: F401 + import tgbot.database.db_item # noqa: F401 + import tgbot.database.db_payments # noqa: F401 + import tgbot.database.db_position # noqa: F401 + import tgbot.database.db_purchases # noqa: F401 + import tgbot.database.db_refill # noqa: F401 + import tgbot.database.db_settings # noqa: F401 + import tgbot.database.db_users # noqa: F401 + + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.create_all) from tgbot.database.db_payments import Paymentsx from tgbot.database.db_settings import Settingsx diff --git a/tgbot/routers/admin/admin_settings.py b/tgbot/routers/admin/admin_settings.py index 4f88298..afe3d81 100644 --- a/tgbot/routers/admin/admin_settings.py +++ b/tgbot/routers/admin/admin_settings.py @@ -242,28 +242,6 @@ async def settings_edit_method_prod( ) -# Изменение текстового хостинга по умолчанию -@router.callback_query(F.data == "settings_edit_hosting_text") -async def settings_edit_hosting_text( - call: CallbackQuery, bot: Bot, state: FSM, arSession: ARS -): - get_settings = await Settingsx().get() - - if get_settings.misc_hosting_text == "telegraph": - await Settingsx().update(misc_hosting_text="pastie") - elif get_settings.misc_hosting_text == "pastie": - await Settingsx().update(misc_hosting_text="friendpaste") - elif get_settings.misc_hosting_text == "friendpaste": - await Settingsx().update(misc_hosting_text="snippet") - elif get_settings.misc_hosting_text == "snippet": - await Settingsx().update(misc_hosting_text="telegraph") - - await call.message.edit_text( - "🖍 Изменение данных бота", - reply_markup=await settings_finl(), - ) - - ################################################################################ ################################ ПРИНЯТИЕ ДАННЫХ ############################### # Принятие FAQ diff --git a/tgbot/services/api_hosting_text.py b/tgbot/services/api_hosting_text.py index f6b06ad..c87f23d 100644 --- a/tgbot/services/api_hosting_text.py +++ b/tgbot/services/api_hosting_text.py @@ -16,19 +16,16 @@ from tgbot.utils.misc.bot_models import ARS @dataclass class ModelHostingText: telegraph: str = "telegraph" - pastie: str = "pastie" - friendpaste: str = "friendpaste" - snippet: str = "snippet" # API для работы с текстовыми хостингами class HostingAPI: # Настройка клиента текстового хостинга def __init__( - self, - arSession: ARS, - bot: Bot, - text_hosting: str, + self, + arSession: ARS, + bot: Bot, + text_hosting: str, ): self.arSession = arSession self.bot = bot @@ -42,7 +39,7 @@ class HostingAPI: return cls( arSession=arSession, bot=bot, - text_hosting=settings.misc_hosting_text, # telegraph, pastie, friendpaste, snippet + text_hosting=settings.misc_hosting_text, # telegraph ) # Загрузка текста на хостинг @@ -67,89 +64,10 @@ class HostingAPI: await send_errors(self.bot, 4044321, str(response)) status_success = False - ############################################################ - ########################## PASTIE ########################## - elif self.text_hosting == ModelHostingText.pastie: - try: - response = await session.request( - method="post", - url="http://pastie.org/pastes/create/", - data={ - 'language': 'plaintext', - 'content': text, - }, - ) - except Exception as ex: - bot_logger.warning("Ошибка загрузки текста в Pastie", exc_info=True) - await send_errors(self.bot, 4719012, str(ex)) - status_success = False - else: - cache_link = response.url - - if "create" in str(cache_link) or str(cache_link) == "http://pastie.org/": - status_success = False - else: - return_link = cache_link - - ############################################################ - ######################## FRIENDPASTE ####################### - elif self.text_hosting == ModelHostingText.friendpaste: - try: - response = await session.request( - method="post", - url="https://www.friendpaste.com/", - json={ - 'language': 'text', - 'title': '', - 'snippet': text, - }, - ) - cache_link = json.loads((await response.read()).decode())['url'] - except Exception as ex: - bot_logger.warning("Ошибка загрузки текста в FriendPaste", exc_info=True) - await send_errors(self.bot, 8434711, str(ex)) - status_success = False - else: - return_link = cache_link - - ############################################################ - ######################### SNIPPET ########################## - elif self.text_hosting == ModelHostingText.snippet: - try: - response = await session.request( - method="post", - url="https://snippet.host/", - data={ - 'title': '', - 'content': text, - 'visibility': '1', - 'expires': 'never', - 'language': 'plain text' - }, - ) - cache_link = response.url - except Exception as ex: - bot_logger.warning("Ошибка загрузки текста в Snippet", exc_info=True) - await send_errors(self.bot, 8900012, str(ex)) - status_success = False - else: - return_link = cache_link - if status_success: return str(return_link) - # pastie -> friendpaste - if self.text_hosting == ModelHostingText.pastie: - self.text_hosting = ModelHostingText.friendpaste - # friendpaste -> snippet - elif self.text_hosting == ModelHostingText.friendpaste: - self.text_hosting = ModelHostingText.snippet - # snippet -> telegraph - elif self.text_hosting == ModelHostingText.snippet: - self.text_hosting = ModelHostingText.telegraph - # telegraph -> pastie - elif self.text_hosting == ModelHostingText.telegraph: - self.text_hosting = ModelHostingText.pastie + self.text_hosting = ModelHostingText.telegraph if attempt < max_attempts - 1: await asyncio.sleep(3)