Try Using Own Webss API

This commit is contained in:
yasirarism 2022-12-12 18:35:31 +07:00 committed by GitHub
parent 6f2f14d427
commit a3efe8d991
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,79 +1,35 @@
import base64
import os
from asyncio import gather
from base64 import b64decode
from io import BytesIO
from pyrogram import filters
from logging import getLogger
from PIL import Image
from misskaty import app
from misskaty.helper.http import http
from misskaty.vars import COMMAND_HANDLER
from misskaty.core.decorator.errors import capture_err
from misskaty.helper.http import post
LOGGER = getLogger(__name__)
__MODULE__ = "WebSS"
__HELP__ = """
/webss [URL] [FULL_SIZE?, use (y|yes|true) to get full size image. (optional)] - Take A Screenshot Of A Webpage.
/webss [URL] - Take A Screenshot Of A Webpage.
"""
async def take_screenshot(url: str, full: bool = False):
url = url if url.startswith("http") else f"https://{url}"
payload = {
"url": url,
"width": 1920,
"height": 1080,
"scale": 1,
"format": "jpeg",
}
if full:
payload["full"] = True
data = await post(
"https://webscreenshot.vercel.app/api",
data=payload,
)
if "image" not in data:
return None
b = data["image"].replace("data:image/jpeg;base64,", "")
file = BytesIO(b64decode(b))
file.name = "webss.jpg"
return file
@app.on_message(filters.command(["webss"], COMMAND_HANDLER))
@capture_err
async def take_ss(_, message):
if len(message.command) < 2:
if len(message.command) == 1:
return await message.reply("Give A Url To Fetch Screenshot.")
if len(message.command) == 2:
url = message.text.split(None, 1)[1]
full = False
elif len(message.command) == 3:
url = message.text.split(None, 2)[1]
full = message.text.split(None, 2)[2].lower().strip() in [
"yes",
"y",
"1",
"true",
]
else:
return await message.reply("Invalid Command.")
url = message.command[1]
filename = f"imageToSave_{message.from_user.id}.png"
m = await message.reply("Capturing screenshot...")
try:
photo = await take_screenshot(url, full)
if not photo:
return await m.edit("Failed To Take Screenshot")
photo = (await http.get(f"https://yasirapi.eu.org/webss?url={url}")).json()
img = Image.open(BytesIO(base64.decodebytes(bytes(photo["result"], "utf-8"))))
img.save(filename)
m = await m.edit("Uploading...")
if not full:
# Full size images have problem with reply_photo, that's why
# we need to only use reply_photo if we're not using full size
await gather(*[message.reply_document(photo), message.reply_photo(photo)])
else:
await message.reply_document(photo)
await gather(*[message.reply_document(filename), message.reply_photo(filename)])
await m.delete()
os.remove(filename)
except Exception as e:
await m.edit(str(e))
await m.edit(f"Failed To Take Screenshot. {str(e)}")