refactor(hosting): remove alternative text hosting providers

This commit is contained in:
2026-07-15 16:07:15 +05:00
parent ec5ae395f3
commit 572dc8d1b4
3 changed files with 20 additions and 112 deletions
+14 -2
View File
@@ -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
-22
View File
@@ -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(
"<b>🖍 Изменение данных бота</b>",
reply_markup=await settings_finl(),
)
################################################################################
################################ ПРИНЯТИЕ ДАННЫХ ###############################
# Принятие FAQ
+6 -88
View File
@@ -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)