Files
AutoShop-Djimbo-Simple/tgbot/services/api_hosting_text.py
T

76 lines
2.2 KiB
Python

# - *- coding: utf- 8 - *-
import asyncio
import json
from dataclasses import dataclass
from aiogram import Bot
from tgbot.database import Settingsx
from tgbot.services.api_telegraph import TelegraphAPI
from tgbot.utils.const_functions import send_errors
from tgbot.utils.misc.bot_logging import bot_logger
from tgbot.utils.misc.bot_models import ARS
# Model Hosting Text
@dataclass
class ModelHostingText:
telegraph: str = "telegraph"
# API для работы с текстовыми хостингами
class HostingAPI:
# Настройка клиента текстового хостинга
def __init__(
self,
arSession: ARS,
bot: Bot,
text_hosting: str,
):
self.arSession = arSession
self.bot = bot
self.text_hosting = text_hosting
# Инициализация данных
@classmethod
async def connect(cls, arSession: ARS, bot: Bot):
settings = await Settingsx().get()
return cls(
arSession=arSession,
bot=bot,
text_hosting=settings.misc_hosting_text, # telegraph
)
# Загрузка текста на хостинг
async def upload_text(self, text: str) -> str:
session = await self.arSession.get_session()
max_attempts = 10
for attempt in range(max_attempts):
status_success = True
return_link = "None"
############################################################
######################## TELEGRAPH #########################
if self.text_hosting == ModelHostingText.telegraph:
telegraph_api = await TelegraphAPI.connect(arSession=self.arSession)
status, response = await telegraph_api.upload_text(text)
if status:
return_link = response
else:
await send_errors(self.bot, 4044321, str(response))
status_success = False
if status_success:
return str(return_link)
self.text_hosting = ModelHostingText.telegraph
if attempt < max_attempts - 1:
await asyncio.sleep(3)
return "None"