From 1447c3391721631d36b1e5ed3d03bdabeb776004 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Sat, 1 Oct 2022 12:20:33 +0530 Subject: [PATCH 1/4] Fixes #315 --- plugins/misc/telegraph/__init__.py | 7 +++++++ plugins/misc/telegraph/__main__.py | 23 ++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plugins/misc/telegraph/__init__.py b/plugins/misc/telegraph/__init__.py index 359045828..a33acca99 100644 --- a/plugins/misc/telegraph/__init__.py +++ b/plugins/misc/telegraph/__init__.py @@ -7,3 +7,10 @@ # All rights reserved. """ telegraph uploader """ + +import os + +TE_LEGRA_PH_DOMA_IN = os.environ.get( + "TE_LEGRA_PH_DOMA_IN", + "https://te.legra.ph" +) diff --git a/plugins/misc/telegraph/__main__.py b/plugins/misc/telegraph/__main__.py index ab5ef7d9a..27057612b 100644 --- a/plugins/misc/telegraph/__main__.py +++ b/plugins/misc/telegraph/__main__.py @@ -11,9 +11,8 @@ import aiofiles from PIL import Image from aiofiles import os -from html_telegraph_poster import TelegraphPoster -from telegraph import upload_file - +from telegraph import upload_file, Telegraph +from ..telegraph import TE_LEGRA_PH_DOMA_IN from userge import userge, Message, config, pool from userge.utils import progress @@ -90,20 +89,22 @@ async def telegraph_(message: Message): except Exception as t_e: await message.err(str(t_e)) else: - await message.edit(f"**[Here Your Telegra.ph Link!](https://telegra.ph{response[0]})**") + await message.edit(f"**[Here Your Telegra.ph Link!]({TE_LEGRA_PH_DOMA_IN}{response[0]})**") finally: await os.remove(dl_loc) def post_to_telegraph(a_title: str, content: str) -> str: """ Create a Telegram Post using HTML Content """ - post_client = TelegraphPoster(use_api=True) + telegraph = Telegraph() auth_name = "@TheUserge" - post_client.create_api_token(auth_name) - post_page = post_client.post( - title=a_title, - author=auth_name, + post_client = telegraph.create_account( + short_name=a_title, + author_name=auth_name, author_url="https://telegram.me/theUserge", - text=content ) - return post_page['url'] + response = telegraph.create_page( + title_of_page, + html_content=content + ) + return TE_LEGRA_PH_DOMA_IN + response['path'] From 18ff93e5951a03b30e2934dbfcf5201ff5e6e065 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Sat, 1 Oct 2022 12:24:42 +0530 Subject: [PATCH 2/4] fixes --- plugins/misc/telegraph/__main__.py | 4 ++-- plugins/misc/telegraph/config.ini | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/misc/telegraph/__main__.py b/plugins/misc/telegraph/__main__.py index 27057612b..4308c0654 100644 --- a/plugins/misc/telegraph/__main__.py +++ b/plugins/misc/telegraph/__main__.py @@ -98,13 +98,13 @@ def post_to_telegraph(a_title: str, content: str) -> str: """ Create a Telegram Post using HTML Content """ telegraph = Telegraph() auth_name = "@TheUserge" - post_client = telegraph.create_account( + telegraph.create_account( short_name=a_title, author_name=auth_name, author_url="https://telegram.me/theUserge", ) response = telegraph.create_page( - title_of_page, + a_title, html_content=content ) return TE_LEGRA_PH_DOMA_IN + response['path'] diff --git a/plugins/misc/telegraph/config.ini b/plugins/misc/telegraph/config.ini index d70fd5519..2f32a3841 100644 --- a/plugins/misc/telegraph/config.ini +++ b/plugins/misc/telegraph/config.ini @@ -5,5 +5,5 @@ max_core: 0 client_type: envs: bins: -packages: aiofiles, Pillow==9.0.0, telegraph, html-telegraph-poster +packages: aiofiles, Pillow==9.0.0, telegraph==1.4.1 depends: \ No newline at end of file From 311b78848c5e2bcf57cced354cc136c00596c88c Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Sat, 1 Oct 2022 12:33:44 +0530 Subject: [PATCH 3/4] re use same access token --- plugins/misc/telegraph/__init__.py | 6 ++++ plugins/misc/telegraph/__main__.py | 47 +++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/plugins/misc/telegraph/__init__.py b/plugins/misc/telegraph/__init__.py index a33acca99..c70640d32 100644 --- a/plugins/misc/telegraph/__init__.py +++ b/plugins/misc/telegraph/__init__.py @@ -14,3 +14,9 @@ "TE_LEGRA_PH_DOMA_IN", "https://te.legra.ph" ) +TE_LEGRA_PH_ACCESS_TOKEN = int( + os.environ.get( + "TE_LEGRA_PH_ACCESS_TOKEN", + "0" + ) +) diff --git a/plugins/misc/telegraph/__main__.py b/plugins/misc/telegraph/__main__.py index 4308c0654..63a355382 100644 --- a/plugins/misc/telegraph/__main__.py +++ b/plugins/misc/telegraph/__main__.py @@ -12,7 +12,10 @@ from PIL import Image from aiofiles import os from telegraph import upload_file, Telegraph -from ..telegraph import TE_LEGRA_PH_DOMA_IN +from ..telegraph import ( + TE_LEGRA_PH_DOMA_IN, + TE_LEGRA_PH_ACCESS_TOKEN +) from userge import userge, Message, config, pool from userge.utils import progress @@ -68,7 +71,27 @@ async def telegraph_(message: Message): else: text = content header = "Pasted content by @theuserge" - t_url = await pool.run_in_thread(post_to_telegraph)(header, text.replace("\n", "
")) + at = None + if TE_LEGRA_PH_ACCESS_TOKEN != 0: + at = ( + await message.client.get_messages( + config.LOG_CHANNEL_ID, + TE_LEGRA_PH_ACCESS_TOKEN + ) + ).text + else: + at = await pool.run_in_thread(pcreate_access_token)(header) + TE_LEGRA_PH_ACCESS_TOKEN = ( + await message.client.send_message( + config.LOG_CHANNEL_ID, + at + ) + ).id + t_url = await pool.run_in_thread(post_to_telegraph)( + access_token, + header, + text.replace("\n", "
") + ) jv_text = f"**[Here Your Telegra.ph Link!]({t_url})**" await message.edit(text=jv_text, disable_web_page_preview=True) return @@ -94,17 +117,25 @@ async def telegraph_(message: Message): await os.remove(dl_loc) -def post_to_telegraph(a_title: str, content: str) -> str: +def post_to_telegraph(at: str, a_title: str, content: str) -> str: """ Create a Telegram Post using HTML Content """ telegraph = Telegraph() - auth_name = "@TheUserge" - telegraph.create_account( - short_name=a_title, - author_name=auth_name, - author_url="https://telegram.me/theUserge", + telegraph = Telegraph( + access_token=at ) response = telegraph.create_page( a_title, html_content=content ) return TE_LEGRA_PH_DOMA_IN + response['path'] + + +def create_access_token(a_title: str) -> str: + telegraph = Telegraph() + auth_name = "@TheUserge" + resp = telegraph.create_account( + short_name=a_title, + author_name=auth_name, + author_url="https://telegram.me/theUserge", + ) + return resp["access_token"] From 8291c0def66872f874ad025ccf69a4ab9a59d0c2 Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Sat, 1 Oct 2022 12:37:25 +0530 Subject: [PATCH 4/4] test --- plugins/misc/telegraph/__main__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/misc/telegraph/__main__.py b/plugins/misc/telegraph/__main__.py index 63a355382..7a7480f03 100644 --- a/plugins/misc/telegraph/__main__.py +++ b/plugins/misc/telegraph/__main__.py @@ -80,15 +80,13 @@ async def telegraph_(message: Message): ) ).text else: - at = await pool.run_in_thread(pcreate_access_token)(header) - TE_LEGRA_PH_ACCESS_TOKEN = ( - await message.client.send_message( - config.LOG_CHANNEL_ID, - at - ) - ).id + at = await pool.run_in_thread(create_access_token)(header) + await message.client.send_message( + config.LOG_CHANNEL_ID, + at + ) t_url = await pool.run_in_thread(post_to_telegraph)( - access_token, + at, header, text.replace("\n", "
") )