Path: blob/master/bot/helper/ext_utils/telegraph_helper.py
1643 views
from asyncio import sleep1from secrets import token_urlsafe2from telegraph.aio import Telegraph3from telegraph.exceptions import RetryAfterError45from ... import LOGGER678class TelegraphHelper:9def __init__(self, author_name=None, author_url=None):10self._telegraph = Telegraph(domain="graph.org")11self._author_name = author_name12self._author_url = author_url1314async def create_account(self):15LOGGER.info("Creating Telegraph Account")16try:17await self._telegraph.create_account(18short_name=token_urlsafe(8),19author_name=self._author_name,20author_url=self._author_url,21)22except Exception as e:23LOGGER.error(f"Failed to create Telegraph Account: {e}")2425async def create_page(self, title, content):26try:27return await self._telegraph.create_page(28title=title,29author_name=self._author_name,30author_url=self._author_url,31html_content=content,32)33except RetryAfterError as st:34LOGGER.warning(35f"Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds."36)37await sleep(st.retry_after)38return await self.create_page(title, content)3940async def edit_page(self, path, title, content):41try:42return await self._telegraph.edit_page(43path=path,44title=title,45author_name=self._author_name,46author_url=self._author_url,47html_content=content,48)49except RetryAfterError as st:50LOGGER.warning(51f"Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds."52)53await sleep(st.retry_after)54return await self.edit_page(path, title, content)5556async def edit_telegraph(self, path, telegraph_content):57nxt_page = 158prev_page = 059num_of_path = len(path)60for content in telegraph_content:61if nxt_page == 1:62content += (63f'<b><a href="https://telegra.ph/{path[nxt_page]}">Next</a></b>'64)65nxt_page += 166else:67if prev_page <= num_of_path:68content += f'<b><a href="https://telegra.ph/{path[prev_page]}">Prev</a></b>'69prev_page += 170if nxt_page < num_of_path:71content += f'<b> | <a href="https://telegra.ph/{path[nxt_page]}">Next</a></b>'72nxt_page += 173await self.edit_page(74path=path[prev_page],75title="Mirror-leech-bot Torrent Search",76content=content,77)78return798081telegraph = TelegraphHelper(82"Mirror-Leech-Telegram-Bot", "https://github.com/anasty17/mirror-leech-telegram-bot"83)8485print(__name__)868788