Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/helper/ext_utils/telegraph_helper.py
1643 views
1
from asyncio import sleep
2
from secrets import token_urlsafe
3
from telegraph.aio import Telegraph
4
from telegraph.exceptions import RetryAfterError
5
6
from ... import LOGGER
7
8
9
class TelegraphHelper:
10
def __init__(self, author_name=None, author_url=None):
11
self._telegraph = Telegraph(domain="graph.org")
12
self._author_name = author_name
13
self._author_url = author_url
14
15
async def create_account(self):
16
LOGGER.info("Creating Telegraph Account")
17
try:
18
await self._telegraph.create_account(
19
short_name=token_urlsafe(8),
20
author_name=self._author_name,
21
author_url=self._author_url,
22
)
23
except Exception as e:
24
LOGGER.error(f"Failed to create Telegraph Account: {e}")
25
26
async def create_page(self, title, content):
27
try:
28
return await self._telegraph.create_page(
29
title=title,
30
author_name=self._author_name,
31
author_url=self._author_url,
32
html_content=content,
33
)
34
except RetryAfterError as st:
35
LOGGER.warning(
36
f"Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds."
37
)
38
await sleep(st.retry_after)
39
return await self.create_page(title, content)
40
41
async def edit_page(self, path, title, content):
42
try:
43
return await self._telegraph.edit_page(
44
path=path,
45
title=title,
46
author_name=self._author_name,
47
author_url=self._author_url,
48
html_content=content,
49
)
50
except RetryAfterError as st:
51
LOGGER.warning(
52
f"Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds."
53
)
54
await sleep(st.retry_after)
55
return await self.edit_page(path, title, content)
56
57
async def edit_telegraph(self, path, telegraph_content):
58
nxt_page = 1
59
prev_page = 0
60
num_of_path = len(path)
61
for content in telegraph_content:
62
if nxt_page == 1:
63
content += (
64
f'<b><a href="https://telegra.ph/{path[nxt_page]}">Next</a></b>'
65
)
66
nxt_page += 1
67
else:
68
if prev_page <= num_of_path:
69
content += f'<b><a href="https://telegra.ph/{path[prev_page]}">Prev</a></b>'
70
prev_page += 1
71
if nxt_page < num_of_path:
72
content += f'<b> | <a href="https://telegra.ph/{path[nxt_page]}">Next</a></b>'
73
nxt_page += 1
74
await self.edit_page(
75
path=path[prev_page],
76
title="Mirror-leech-bot Torrent Search",
77
content=content,
78
)
79
return
80
81
82
telegraph = TelegraphHelper(
83
"Mirror-Leech-Telegram-Bot", "https://github.com/anasty17/mirror-leech-telegram-bot"
84
)
85
86
print(__name__)
87
88