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

Changed API in print cmd #298

Merged
merged 9 commits into from
Aug 25, 2021
101 changes: 87 additions & 14 deletions eduu/plugins/prints.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2018-2021 Amano Team

from io import BytesIO

from httpx import HTTPError
from pyrogram import Client, filters
from pyrogram.types import Message

Expand All @@ -14,18 +13,92 @@

@Client.on_message(filters.command("print", prefix))
@use_chat_lang()
async def prints(c: Client, m: Message, strings):
if len(m.command) == 1:
return await m.reply_text(
strings("print_usage"), reply_to_message_id=m.message_id
)
sent = await m.reply_text(strings("taking_screenshot"))
text = m.text.split(maxsplit=1)[1]
r = await http.get("https://webshot.amanoteam.com/print", params=dict(q=text))
bio = BytesIO(r.read())
bio.name = "screenshot.png"
await m.reply_photo(bio)
await sent.delete()
async def prints(c: Client, message: Message, strings):
msg = message.text
the_url = msg.split(" ", 1)
wrong = False

if len(the_url) == 1:
if message.reply_to_message:
the_url = message.reply_to_message.text
if len(the_url) == 1:
wrong = True
else:
the_url = the_url[1]
else:
wrong = True
else:
the_url = the_url[1]

if wrong:
await message.reply_text(strings("print_usage"))
return

try:
sent = await message.reply_text(strings("taking_screenshot"))
res_json = await cssworker_url(target_url=the_url)
except BaseException as e:
await message.reply(f"**Failed due to:** `{e}`")
return

if res_json:
# {"url":"image_url","response_time":"147ms"}
image_url = res_json["url"]
if image_url:
try:
await message.reply_photo(image_url)
await sent.delete()
except BaseException:
# if failed to send the message, it's not API's
# fault.
# most probably there are some other kind of problem,
# for example it failed to delete its message.
# or the bot doesn't have access to send media in the chat.
return
else:
await message.reply(
"couldn't get url value, most probably API is not accessible."
)
else:
await message.reply("Failed, because API is not responding, try it later.")


async def cssworker_url(target_url: str):
url = "https://htmlcsstoimage.com/demo_run"
my_headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://htmlcsstoimage.com/",
"Content-Type": "application/json",
"Origin": "https://htmlcsstoimage.com",
"Alt-Used": "htmlcsstoimage.com",
"Connection": "keep-alive",
}

# remove 'https' prefixes to avoid bugging out api
target_url = target_url.lstrip("https://")
target_url = target_url.rstrip("http://")

data = {
"html": "",
"console_mode": "",
"url": target_url,
"css": "",
"selector": "",
"ms_delay": "",
"render_when_ready": "false",
"viewport_height": "",
"viewport_width": "",
"google_fonts": "",
"device_scale": "",
}

try:
resp = await http.post(url, headers=my_headers, json=data)
return resp.json()
except HTTPError:
return None


commands.add_command("print", "tools")