forked from FOSS/AutoShop-Djimbo
refactor(hosting): remove alternative text hosting providers
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import Any, Dict, Generic, List, Optional, Type, TypeVar
|
|||||||
from sqlalchemy import delete as sqlalchemy_delete
|
from sqlalchemy import delete as sqlalchemy_delete
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy import update as sqlalchemy_update
|
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
|
from tgbot.utils.misc.bot_logging import bot_logger
|
||||||
|
|
||||||
ModelTranslator = TypeVar("ModelTranslator", bound=Base)
|
ModelTranslator = TypeVar("ModelTranslator", bound=Base)
|
||||||
@@ -150,7 +150,19 @@ class BaseRepository(Generic[ModelTranslator, EntityTranslator]):
|
|||||||
# Применение миграций и создание дефолтных строк
|
# Применение миграций и создание дефолтных строк
|
||||||
async def prepare_database() -> None:
|
async def prepare_database() -> None:
|
||||||
database_path.parent.mkdir(parents=True, exist_ok=True)
|
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_payments import Paymentsx
|
||||||
from tgbot.database.db_settings import Settingsx
|
from tgbot.database.db_settings import Settingsx
|
||||||
|
|||||||
@@ -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
|
# Принятие FAQ
|
||||||
|
|||||||
@@ -16,19 +16,16 @@ from tgbot.utils.misc.bot_models import ARS
|
|||||||
@dataclass
|
@dataclass
|
||||||
class ModelHostingText:
|
class ModelHostingText:
|
||||||
telegraph: str = "telegraph"
|
telegraph: str = "telegraph"
|
||||||
pastie: str = "pastie"
|
|
||||||
friendpaste: str = "friendpaste"
|
|
||||||
snippet: str = "snippet"
|
|
||||||
|
|
||||||
|
|
||||||
# API для работы с текстовыми хостингами
|
# API для работы с текстовыми хостингами
|
||||||
class HostingAPI:
|
class HostingAPI:
|
||||||
# Настройка клиента текстового хостинга
|
# Настройка клиента текстового хостинга
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
arSession: ARS,
|
arSession: ARS,
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
text_hosting: str,
|
text_hosting: str,
|
||||||
):
|
):
|
||||||
self.arSession = arSession
|
self.arSession = arSession
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
@@ -42,7 +39,7 @@ class HostingAPI:
|
|||||||
return cls(
|
return cls(
|
||||||
arSession=arSession,
|
arSession=arSession,
|
||||||
bot=bot,
|
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))
|
await send_errors(self.bot, 4044321, str(response))
|
||||||
status_success = False
|
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:
|
if status_success:
|
||||||
return str(return_link)
|
return str(return_link)
|
||||||
|
|
||||||
# pastie -> friendpaste
|
self.text_hosting = ModelHostingText.telegraph
|
||||||
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
|
|
||||||
|
|
||||||
if attempt < max_attempts - 1:
|
if attempt < max_attempts - 1:
|
||||||
await asyncio.sleep(3)
|
await asyncio.sleep(3)
|
||||||
|
|||||||
Reference in New Issue
Block a user