|
| 1 | +import importlib.util |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import random |
| 5 | +import string |
| 6 | +import urllib.parse |
| 7 | + |
| 8 | +from pyrogram import Client, Filters, Message |
| 9 | +from selenium.webdriver import Firefox |
| 10 | +from selenium.webdriver.firefox.options import Options |
| 11 | + |
| 12 | +from main import prefixes |
| 13 | +from methods.ArgumentOrReply import ArgumentOrReply |
| 14 | + |
| 15 | +_path = os.path.dirname(__file__) |
| 16 | +_selenium_path = os.path.join(_path, "../_selenium/_selenium.py") |
| 17 | + |
| 18 | +if not os.path.exists(_selenium_path): |
| 19 | + raise Exception("The _selenium plugin needs to be enabled to work") |
| 20 | + |
| 21 | +spec = importlib.util.spec_from_file_location("driverWrapper", _selenium_path) |
| 22 | +_selenium = importlib.util.module_from_spec(spec) |
| 23 | +spec.loader.exec_module(_selenium) |
| 24 | +driverWrapper = getattr(_selenium, "driverWrapper") |
| 25 | + |
| 26 | +CSS_SELECTOR = ".bg" |
| 27 | +OPTIONS = Options() |
| 28 | +OPTIONS.headless = True |
| 29 | + |
| 30 | + |
| 31 | +@Client.on_message(Filters.me & Filters.command("carbon", prefixes=prefixes)) |
| 32 | +def carbon(c: Client, msg: Message): |
| 33 | + code = ArgumentOrReply(msg, len("/carbon ")) |
| 34 | + if not code: |
| 35 | + msg.edit_text("Please reply to a message or use <code>/carbon My Text</code>") |
| 36 | + |
| 37 | + _params = { |
| 38 | + "bg": "rgba(57, 70, 79, 1)", |
| 39 | + "t": "seti", |
| 40 | + "wt": "none", |
| 41 | + "l": "auto", |
| 42 | + "ds": "true", |
| 43 | + "dsyoff": "20px", |
| 44 | + "dsblur": "68px", |
| 45 | + "wc": "true", |
| 46 | + "wa": "true", |
| 47 | + "pv": "56px", |
| 48 | + "ph": "56px", |
| 49 | + "ln": "false", |
| 50 | + "fl": "1", |
| 51 | + "fm": "Hack", |
| 52 | + "fs": "14px", |
| 53 | + "lh": "133%", |
| 54 | + "si": "false", |
| 55 | + "es": "2x", |
| 56 | + "wm": "false", |
| 57 | + "code": code |
| 58 | + } |
| 59 | + |
| 60 | + params = "&".join(["{}={}".format(i, urllib.parse.quote(j)) for i, j in _params.items()]) |
| 61 | + |
| 62 | + msg.edit_text("Initializing driver...") |
| 63 | + |
| 64 | + try: |
| 65 | + driver: Firefox = driverWrapper(options=OPTIONS) |
| 66 | + except Exception as e: |
| 67 | + logging.error(f"An exception occurred -> {type(e).__name__}: {e}") |
| 68 | + msg.edit_text("Something went wrong!") |
| 69 | + return 1 |
| 70 | + |
| 71 | + msg.edit_text("Reaching https://carbon.now.sh/...", disable_web_page_preview=True) |
| 72 | + |
| 73 | + driver.get("https://carbon.now.sh/?" + params) |
| 74 | + |
| 75 | + element = driver.find_element_by_css_selector(CSS_SELECTOR) |
| 76 | + |
| 77 | + if not element: |
| 78 | + msg.edit_text("Something went wrong!") |
| 79 | + return 1 |
| 80 | + |
| 81 | + if not os.path.exists("tmp"): |
| 82 | + os.mkdir("tmp") |
| 83 | + |
| 84 | + path = "tmp/" + "".join(random.choices(string.ascii_letters, k=40)) + ".png" |
| 85 | + open(path, "wb").write(element.screenshot_as_png) |
| 86 | + |
| 87 | + driver.quit() |
| 88 | + msg.edit_text("Sending photo...") |
| 89 | + |
| 90 | + if msg.reply_to_message: |
| 91 | + c.send_photo( |
| 92 | + msg.chat.id, |
| 93 | + path, |
| 94 | + reply_to_message_id=msg.reply_to_message.message_id |
| 95 | + ) |
| 96 | + else: |
| 97 | + c.send_photo( |
| 98 | + msg.chat.id, |
| 99 | + path |
| 100 | + ) |
| 101 | + |
| 102 | + msg.delete() |
0 commit comments