Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace telegra.ph with te.legra.ph #319

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions plugins/misc/telegraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@
# All rights reserved.

""" telegraph uploader """

import os

TE_LEGRA_PH_DOMA_IN = os.environ.get(
"TE_LEGRA_PH_DOMA_IN",
"https://te.legra.ph"
)
TE_LEGRA_PH_ACCESS_TOKEN = int(
os.environ.get(
"TE_LEGRA_PH_ACCESS_TOKEN",
"0"
)
)
56 changes: 43 additions & 13 deletions plugins/misc/telegraph/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
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,
TE_LEGRA_PH_ACCESS_TOKEN
)
from userge import userge, Message, config, pool
from userge.utils import progress

Expand Down Expand Up @@ -69,7 +71,25 @@ 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", "<br>"))
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(create_access_token)(header)
await message.client.send_message(
config.LOG_CHANNEL_ID,
at
)
Comment on lines +74 to +87
Copy link
Collaborator

@Harsh-br0 Harsh-br0 Oct 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems spammy cuz it will send a token on each paste

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after receiving once, we can update env var 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess better solution would be someone experienced with mongoDB could PR .

t_url = await pool.run_in_thread(post_to_telegraph)(
at,
header,
text.replace("\n", "<br>")
)
jv_text = f"**[Here Your Telegra.ph Link!]({t_url})**"
await message.edit(text=jv_text, disable_web_page_preview=True)
return
Expand All @@ -90,20 +110,30 @@ 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:
def post_to_telegraph(at: str, a_title: str, content: str) -> str:
""" Create a Telegram Post using HTML Content """
post_client = TelegraphPoster(use_api=True)
telegraph = Telegraph()
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"
post_client.create_api_token(auth_name)
post_page = post_client.post(
title=a_title,
author=auth_name,
resp = telegraph.create_account(
short_name=a_title,
author_name=auth_name,
author_url="https://telegram.me/theUserge",
text=content
)
return post_page['url']
return resp["access_token"]
2 changes: 1 addition & 1 deletion plugins/misc/telegraph/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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: